2

I have following question, basically i just would like to create a custom array, something like - Yellow | Red | Green

And after loop through this items using for-each and printing the values, i created something like this:

        <xsl:variable name="Colors">
           <m>Yellow</m>
           <m>Red</m>
           <m>Green</m>
        </xsl:variable>

And then i try to loop on it:

        <xsl:for-each select="$Colors">

            <xsl:value-of select ="current()" />                
        </xsl:for-each>

But i get this error: Exrpession must evalutate to a node-set $Colors

Any idea, what could be wrong?

1 Answer 1

4

With XSLT 1.0 your variable contains a result tree fragment (RTF) and you can't do anything with it but use xsl:copy-of or xsl:value-of. Fortunately most XSLT 1.0 processor support exsl:node-set or similar to convert a result tree fragment to a node set so you can use <xsl:for-each select="exsl:node-set($Colors)/m">...</xsl:for-each>, where you then need to declare xmlns:exsl="http://exslt.org/common" in your stylesheet.

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

3 Comments

If you use <xsl:for-each select="exsl:node-set($Colors)/m"><xsl:value-of select="."/></xsl:for-each> then the three values should be output. If that does not work then there might be a default namespace declaration in scope in your stylesheet. You would need to change the variable declaration to <xsl:variable name="Colors" xmlns=""><m>Yellow</m>...</xsl:variable> to make sure the m elements are in no namespace and can be selected as m in XPath.
just another question, regarding this, for clarification: <m> is necessary to use to building an array or i can create an array differently?
You are not building an array as XSLT/XPath does not have any such data type. In XSLT 1.0 you are creating a result tree fragment which you then convert to a node-set. To structure your result tree fragment you indeed need to use elements (or more generally nodes). With XSLT 2.0 there is more flexibility as there you could define a sequence of strings with <xsl:variable name="colors" select="'Yellow', 'Red', 'Green'"/>.

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.