Template language with XPath support for source code generation?

S

Stefan Behnel

Hi!

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.

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.

Thanks,
Stefan
 
B

bruno at modulix

Stefan said:
Hi!

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.

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)

My 2 cents
 
S

Stefan Behnel

bruno said:
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.

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
 
K

kent37

Stefan said:
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.

I have done a lot of text file generation in Jython and Java using
dom4j as the data model and Velocity as the template engine. It's a
very powerful combination.

dom4j elements have XPath accessors so you can say for example
myElement.selectNodes('some/xpath'). Velocity supports introspection on
its data so in a template I can say
#foreach $item ($parent.selectNodes("some/xpath"))
(my syntax may be a little off, this is from memory)

ISTM you should be able to do the same thing from CPython with a dom
model that has XPath accessors and a template engine that supports
calling methods on its data. I think lxml and Cheetah, among others,
would work this way.

Kent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top