Jump to content

Problem with conditional variable value

Recommended Posts

Very simple example... but don't understand why.

 <xsl:variable name="i" select="1" />      <xsl:variable name="a" select="35" />      <xsl:variable name="b" select="'fn'" />      <xsl:variable name="c">        <xsl:if test="$i < $a">          <xsl:value-of select="$b" />        </xsl:if>        <xsl:if test="$i = $a">'test'</xsl:if></xsl:variable>

At the end, value is a node instead of just the text 'fn'.

If I use

xsl:variable name="c" select="$b" />

Then the value is correct (just 'fn').


Any help?

Link to post
Share on other sites

I found the answer. Variables created that way (not using select) are considered Reference Tree Fragment (http://msdn.microsoft.com/en-us/library/4wwc9138.aspx).

It's still possible to use the variable content the normal way, although the object will be a RTF.

Now, If you have a list of nodes, you have to transform the variable to a NodeSet and then call the nodes() function like


Then it will be just like creating a variable with the select argument.

Link to post
Share on other sites

It is called a "result tree fragment". Also note that XSLT 2.0 exists since 2007 and that there you can have temporary trees instead of result tree fragments. Furthermore XSLT 2.0 has an "if (condition) then expression1 else expression2" expression.

So with an XSLT 2.0 processor like Saxon 9 you can avoid such problems.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...