XSL Common mistakes

Published: Tuesday, 24 February 2004

Common mistakes or errors that I’ve made when using XSLT.

XPath: Forgetting quotes around a literal string

When giving a default parameter or a constant value, make sure that you remember the single quotes around it. Otherwise the processor will go and look up for the element.

For example, the following parameter was meant to give a directory prefix to our filename.

<xsl:with-param name="prefix" select="../"/>

But this was treated as an XPath expression that looked at the parent node. Instead we should’ve used

<xsl:with-param name="prefix" select="'../'"/>

The XSL processor will correctly not throw any error, so it may be difficult to spot where the error occurs.

Using the value attribute instead of the select attribute.

I must’ve been looking at too much HTML. When using parameters, I would give the two attributes as name and value, instead of name and select.

This will correctly set the variable colour to ‘red’,

<xsl:variable name="colour" select="'red'"/>

But this will not set colour at all, and leave it empty

<xsl:variable name="colour" value="'red'"/>

Solutions to this are to check for the value attribute in the XSL document. For some reason my XSL parser, Saxon, doesn’t complain when I add unknown attributes into the XSL elements.