XSLT Tricks

Curly Brackets and Dollar Signs

When you want to generate the expression ${property.key} with XSLT and you simply write this in the template, XSLT will interpret the curly brackets as an XPath Expression and will print out the value it calculates. It will fail silently and the output just shows $ having evaluated {property.key} to an empty string.

The trick is to use double curly brackets as shown in the listing below. It will evaluate the expression in the first curley brackets pair which is an curly brackets expression getting returned as a string.

<!-- evaluates to $ -->
<xsl:template match="...">

<!-- evaluates to ${property.key} -->
<xsl:template match="...">

Adding types to variables

You can add types to variables in your stylesheet. This allows for better error messages as these types are checked during execution.

<!-- variable with dynamic type -->
<xsl:template match="...">
     <xsl:variable name="var1" select="..." />

<!-- variable with static type string -->

<xsl:template match="...">
     <xsl:variable name="var1" select="..." as="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" />

More information and other cool tricks for making XSLT2 safer can be found at http://www.ibm.com/developerworks/library/x-safexslt/


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s