A Few XSL Tips

July 8, 2004

Of many, these XSL tips are fresh from the farm, hot off the press and otherwise happening right before my eyes.

Commenting in the code
Do you want to see the comments within the resulting XML/HTML? The answer here will determine which comment indicator to use in the XSL. For comments that are invisible in the resulting XML/HTML, use <!– a comment –><!– a comment –><!– a comment –>.  For comments that are visible in the XML/HTML, use <xsl:comment>a comment</xsl:comment>.

Building an HREF
Embedding <xsl:value-of> within an anchor tag like this, <a href=”someURL?param=<xsl:value-of select =”sometag” />” >, will not function. Another common attempt is using character replacements like this, &lt;a href%3D&quot;http%3A%2F%2Fresqtek%2Enet%3Fparam%3D1&quot;&lt;. While this might work, it’s messy and illegible. Make life easier. Use <xsl:attribute>.

Examples:
<img class=”itemimage” ><xsl:attribute name=”src” ><xsl:value-of select=”$imageurl” disable-output-escaping=”yes”/></xsl:attribute><xsl:attribute name=”alttext”>alternative text</xsl:attribute></img>

OR

<a><xsl:attribute name=”href” ><xsl:value-of select=”$url” disable-output-escaping=”yes”/></xsl:attribute><xsl:attribute name=”border”>0</xsl:attribute></a>

XSL Tranformation Failed due to the following error:  Expression Expected   <– errormsg
This error message typically provides no additional information.  No line number, no source text, nothing. My research uncovered that the problem lies in the declaration of <xsl:param> tags. Although documentation indicates that you can use a “select” attribute with these tags, in practice (at least with MSXML 4), using select=”” (without a value) will cause the above error. Specifying a value such as select=”a value” will function correctly.  But, if you want to be explicit in your declaration of an empty string as the default for a param, then you are out of luck. Bottom line: Omit select=””.

Happy coding!

Facebooktwittermail