templatesxsltvelocity

Idiom for templating similar to other templating engines like Velocity?


I have an XSLT(2.0) file; which takes an input XML data file and creates DDL/SQL Statements. It works just fine. But it is a bit difficult to maintain, as it contains a lot of formatting information in 'concat' statements like this:

<xsl:value-of select="concat('CREATE USER ',$username,' IDENTIFIED BY ',$password,';',$nl)"/>

What I would prefer to do would be to encode my SQL Statements in a manner like this instead:

<some-enclosing-elements>[...]CREATE USER <username/>, identified by <password/>; [literally a newline here][...]</some-enclosing-elements>

I would perhaps keep this format above in the XML data file itself in a 'lookup' table at the top of the either the XSLT or the data document iself (I can't work out which yet).

Is there a standard idiom that would allow this kind of templating ? Any ideas ?

By the way; the data document contains many different users to create of course


Solution

  • The AVT approach is just a little bit too devious for my taste. I tend to rely on the implicit concatenation done (in 2.0) by xsl:value-of:

    <xsl:value-of select="'CREATE USER', $username, 'identified by', $password"/>
    

    Another approach which I have used in applications where this kind of text templating is significant is to essentially write my own templating engine within XSLT; have a "message file" containing message templates in the form

    <message nr="1">CREATE USER <p:user/> IDENTIFIED BY <p:password/></message>
    

    and then write template rules to expand the messages by substituting the parameters.