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 comment
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 comment
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 comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...