[antlr-interest] [stringtemplate-interest] Generating text from models

John D. Mitchell jdmitchell at gmail.com
Fri Dec 5 16:34:41 PST 2008


On Friday 2008.12.05, at 12:33 , Terence Parr wrote:
[...]
>> In addition to just querying attributes from the context, you can
>> also pass parameters to (query) functions. And this is mandatory in
>> our case. Our input data is XML and the templates emit portions from
>> the XML by referring to XML nodes through XPATH. So in a template
>> you might find something like
>>
>> DoIt() first parameter: <%= Value( "//Method[@name='DoIt']/
>> Parameters[0]" ) %>.
>>
>> Where Value( string xpathExpr ) is such a query operation to get the
>> referenced data from the XML. The problem is that we have a lot of
>> templates that all need different data portions so hardcoding those
>> queries in form of getters is not an option.
>>
>> Can ST do this as well?
>
> Well, it depends on what the arguments are.  if it's an attribute or
> string of some kind, you can use the format option and a renderer.
>
> Any time I need to pass an argument back into the model from the
> template, I ask myself whether I am allowing the model to leak into
> the template. If it looks the program, it probably is. That is not
> something you want to happen to your template. In your case, It's hard
> to tell. But, xpath stuff to pull specific nodes from a data structure
> makes me a bit nervous unless you are just descending through the
> various nodes. One could argue that the template should simply be
> displaying data, not encoding how to jump around and ask about tree.
> One test is to ask whether or not you could use that template with
> another data structure. just something to think about.

In addition to Ter's very good points, I can add that in terms of the  
XML'ish nature of this question that switching from a hacked template  
approach to using XQuery is a very good option.

I.e., processing XML and generating X(HT)ML is much more "natural" and  
easier using XQuery than trying to deal with the latest (weirdo :-)  
JSP/ASP/etc. abomination.

To be clear, I still like the enforced separation of the ST approach  
in general but for a lot of XML hacking, XQuery allows for the  
relatively controlled intermingling of processing, structure, and  
hacking that seems to be very prevalent in XML processing (think:  
multi-stage pipelines that aren't clearly thought of as pipelines).   
We use XQuery on the backend of http://markmail.org/ and it generates  
XHTML, JSON, RSS, etc. for the various types of data streams.

Hope this helps,
John



More information about the antlr-interest mailing list