Template language with XPath support for source code generation?

Stefan Behnel stefan.behnel-n05pAM at web.de
Fri Jan 13 04:29:39 EST 2006


bruno at modulix wrote:
> Stefan Behnel wrote:
>> I need to generate source code (mainly Java) from a domain specific XML
>> language, preferably from within a Python environment (since that's where the
>> XML is generated).
>>
>> I tried using XSLT, but I found that I need a template system that supports
>> Python interaction. I know, lxml's XSLT support is /somewhat/ getting there,
>> but even with that, XSLT is so clumsy when it comes to code generation
>> (looping constructs and if/else above all), that it would take me tons of XSLT
>> code to write what I want.
>>
>> I've been looking through Python templating systems all over the place, but I
>> just can't find one that supports XPath - which is by far the best thing to
>> have when you generate stuff from XML. TAL might be able to get me part of the
>> way (at least, it supports some kind of Path expressions, though only for
>> object access), but the only available implementation is part of Zope and I
>> can't make my code depend on Zope only for a template system.
> 
> Zope's implementation can be used freestanding AFAIK. There's also
> SimpleTal that is totally independant from Zope.

Thanks for the quick reply. I didn't know about SimpleTAL.


>> My problem is that I want to write as little Python code as possible to make
>> the templates (almost) stand alone and thus readable without the backend code.
>> I can transform the XML language to a more usable XML format beforehand, no
>> problem, but I then need to access the result from the template - and that's
>> almost impossible without XPath.
>>
>> Does anyone have an idea what I could use? Any hints are helpful.
> 
> Perhaps a TAL + elementTree combo could do ? (first parse the source XML
> with elementTree, then pass the resulting tree as the context of the
> ZPT/SimpleTal template)

SimpleTAL has ElementTree integration. Wouldn't work with lxml, though, as it
uses internals of ElementTree. Problem is that SimpleTAL requires the tree as
a context object, which is difficult to produce from lxml. It should work when
relying on Python code for access, although that removes some of the beauty.
I'll have to see where that gets me...

Thanks,
Stefan



More information about the Python-list mailing list