Quick Tip – Shorthand Variable References in XSLT

Here’s a quick tip – next time you’re writing some XSL and want to include a variable within the attribute value of a node, don’t waste time & code doing something like this:

<xsl:variable name="VarName" select="SomeData"/>
...
<MyElement>
  <xsl:attribute name="AttrName">
    <xsl:value-of select="$VarName"/>
  </xsl:attribute>
</MyElement>

Instead, you can use this shorthand notation for referencing a variable directly within an element attribute:

<xsl:variable name="VarName" select="SomeData"/>
...
<MyElement AttrName="{$VarName}"/>

Enjoy!

Comments are closed.