0

I have something like the following code::

<xsl:variable name="sample">
     <xsl:copy-of select="//foo">
     <xsl:copy-of select="//bar">
</xsl>

And in a template I’d like to use:

<xsl:for-each select="$sample/*">
    <!-- do something -->
</xsl:for-each>

However, $sample/* does not seem to be a valid xpath expression or return a node-set and I can't quite figure out, how to fix it. I also tried just plain $sample, but that isn't a node-set either :/ Any ideas, what I’m doing wrong?

2 Answers 2

4

Are you using XSLT 1? You can't apply xpath expressions to variables in XSLT 1. You can in XSLT 2 however.

Depending on your XSLT processor you may have access to a custom extension such as EXSLT's node-set function.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that’s it. The exsl:node-set function works like a charm :)
0

There's actually two other solutions available next to switching to an XSLT 2.0 processor.
One is applying a processor dependent function as Kevan says, for instance msxsl:node-set() or xalan:nodeset() or exsl:node-set().
The other is to make use of the fact that xsl:variable select attribute includes a conversion of a result tree fragment such as your sample variable to a node-set; hence defining a new variable with select attribute equal to the one to be addressed, will do the trick:

<xsl:variable name="temp" select= "$sample"/>
<xsl:for-each select="$temp/*">
    <!-- do something -->
</xsl:for-each>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.