RE: [xsl] AW: Accessing values in a node-set using XPATH

Subject: RE: [xsl] AW: Accessing values in a node-set using XPATH
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Tue, 11 Nov 2008 11:21:47 -0000
Firstly, please don't use this:

<xsl:variable name="x">
  <xsl:value-of select="y"/>

when you really want

<xsl:variable name="x" select="y"/>

If you're paid for your coding by the line, you will earn three times as
much writing it the first way, but in every other respect the second
alternative is vastly preferable. If y is a string, you want the string, not
a new temporary document containing a document node whose child is a text
node whose string value is the string.

> <xsl:template name="print">
>    <xsl:param name="pos"/>
>    <xsl:variable name="xpath">
>       <xsl:value-of
> select="document('config-szenario.xml')/config/Line/field[posi
> tion() = $pos]"/>
>    </xsl:variable>
>    <xsl:if test="$xpath">

Here's another reason why you should use the select="y" form. This test will
always succeed: $xpath is a document node and the effective boolean value of
a node is always true, regardless of its content.

> However is there a possibility to perform this also on a
> nodeset? I would like to pass a variable (e.g. comparing) and
> then call dyn:evaluate like shown below. However it raises an
> error. It this somehow possible?
> <xsl:value-of select="dyn:evaluate($comparing/$xpath)"/>

Different implementations of dyn:evaluate() may differ in detail on this.
The safest bet is probably to assume that you can't pass variables into the
dynamic XPath expression, but you can pass the context node. So in XSLT 2.0
you could write

<xsl:value-of select="$comparing/dyn:evaluate($xpath)"/>

while in 1.0 it would have to be

<xsl:for-each select="$comparing">
   <xsl:value-of select="dyn:evaluate($xpath)"/>

Michael Kay

Current Thread