Template language with XPath support for source code generation?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stefan Behnel

    #1

    Template language with XPath support for source code generation?

    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
  • bruno at modulix

    #2
    Re: Template language with XPath support for source code generation?

    Stefan Behnel wrote:[color=blue]
    > 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.[/color]

    Zope's implementation can be used freestanding AFAIK. There's also
    SimpleTal that is totally independant from Zope.

    [color=blue]
    > 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.[/color]

    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


    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in 'onurb@xiludom. gro'.split('@')])"

    Comment

    • Stefan Behnel

      #3
      Re: Template language with XPath support for source code generation?

      bruno at modulix wrote:[color=blue]
      > Stefan Behnel wrote:[color=green]
      >> 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.[/color]
      >
      > Zope's implementation can be used freestanding AFAIK. There's also
      > SimpleTal that is totally independant from Zope.[/color]

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

      [color=blue][color=green]
      >> 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.[/color]
      >
      > 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)[/color]

      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

      Comment

      • kent37@tds.net

        #4
        Re: Template language with XPath support for source code generation?

        Stefan Behnel wrote:[color=blue]
        > 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.[/color]

        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.selec tNodes('some/xpath'). Velocity supports introspection on
        its data so in a template I can say
        #foreach $item ($parent.select Nodes("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

        Comment

        Working...