Re: [xsl] Can I put a variable to be interpreted in an xml node?

Subject: Re: [xsl] Can I put a variable to be interpreted in an xml node?
From: "M. David Peterson" <m.david@xxxxxxxxxx>
Date: Wed, 14 Jul 2004 09:47:58 -0600
Hi Bart,

The fact is that I can redesign the source document, but I've no clue how.

I see a perfect opportunity to help you design your application in a way that conforms well to the ideal XSLT development and subsequent transformation scenario so if you dont mind me jumping in to the middle of this thread here are a few pointers that will help you design your XML and XSLT in a way that is efficient as far as performance is concerned as well as easily maintainable and manageable.

A few pointers and then some examples of what these pointers mean...

- optimize your XML to contain only data that is marked up in a way that allows the XSLT processor to either use the element names to match to a template and/or contains the desired markup for your output stream that wont be changing and can simply be deep copied using xsl:copy-of.
- Remove any sort of logic that requires non-xml processing to determine the value of the output. Think of it like this:

This XML:

<data><![CDATA[Please send comments to the <a href="{$web master}">web master</a> for this site]]></data>

Would require extensive customized conditional logic and string processing that would then have to be matched against a data file to determine the proper replacement output.

This same data could just as easily be written like this:

<data>Please send comments to the <a href="web master">web master</a> for this site</data>

And by using standard recursive template matching and a simple XML "lookup table" could be processed as simply as:

<xsl:template match="a">
<xsl:variable name="href" select="@href"/>
<xsl:attribute name="href"><xsl:value-of select="document('href_values.xml')/links/href[@match_user = $href]/@href_value"/></xsl:attribute>
<xsl:value-of select="."/>

You could actually optimize this further by creating a variable at the beginning of your stylesheet that held the 'href_values.xml' file that could then be referenced anywhere in the stylesheet using the $ prefix appended to the chosen variable name using standard XPath to access the desired values in the same way the values were accessed using the document function followed by the proper XPath.

Hopefully this will be enough information to help you design your xml and stylesheets in a way that can easily be processed and managed. Think how nice it will be when you hire a new web master and the only required change is to access the href_values.xml file and update the value associated with all web master links on the site to the new web masters email, personal page, or blog.

Best of luck!


I need to put a sentence in multiple languages in
the XML document and one of the words should be a link, as follows:

Forgot your password? Fill in <a href="{$link}">this form</a> to retrieve your password.



On 14 Jul 2004 at 9:28, Michael Kay wrote:

I'm new to this list, and this is my first question. I have a two-lingual website (dutch & french). The texts are manually put in
a seperate XML file. Now in one case the text contains a variable,
like in this example:

<tagname><![CDATA[You can find the link <a href="{$link}">here</a>]]></tagname>

Obviously when I pull this tag into the document using <value-of the {$link} is not interpreted. How can I solve this problem? I need to use CDATA because the tag can contain html code like bold, paragraph tags etc.

It's much better to use well-formed markup in the source document. By putting tags in CDATA, you are telling the system "this isn't markup, it's ordinary text", which patently isn't true, and has the effect that it makes the internal structure inaccessible to the XPath and XSLT processors.

If you can't redesign the source document, then try to find a
processor that supports the disable-output-escaping option, and use
<xsl:value-of with disable-output-escaping="yes"/>. This is not a good
solution, because it means you rely on the result tree being
serialized as part of the transformation, but it may be your only
escape route from a poor document design.

Michael Kay

The contents of this email and any attachments are confidential.
It is intended for the named recipient(s) only.
If you have received this email in error please notify the system manager or
the sender immediately. Do not disclose the contents to anyone nor make copies of this mail.

Current Thread