xsl-list
[Top] [All Lists]

RE: Are variables allowed in XPATHs?

2003-02-03 02:50:46
I must say, I find this restriction somewhat 
amazing.  I assume it's intended to reduce the 
complexity of writing XSL processors?

Actually I stated the restriction wrongly. Some patterns in XSLT can
contain variable references, but the ones used in the match attribute of
xsl:template and xsl:key can't.

The restriction was a misguided attempt to eliminate the possibility of
circularities, in constructs like:

<xsl:variable name="x">
  <xsl:apply-templates/>
</xsl:variable>

<xsl:template match="*[$x]">
  ...
</xsl:template>

To circumvent the restriction, you need to use an xsl:choose inside the
template:

<xsl:template match="*">
  <xsl:choose>
   <xsl:when test="position()=$x">
...

The restriction has been lifted in XSLT 2.0.

Michael Kay
Software AG
home: Michael(_dot_)H(_dot_)Kay(_at_)ntlworld(_dot_)com
work: Michael(_dot_)Kay(_at_)softwareag(_dot_)com 


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list