Get xpath from xsl variable

xpathxslt

I am trying to get some xpath from xsl variable using xsl ver 1.0 .
That’s my variable:

  <xsl:variable name ="myVar">
        <RefData RefTag="test1" bbb="false" />
        <RefData RefTag="test2" bbb="false" />
        <RefData RefTag="test3" bbb="false" />
        <RefData RefTag="test4" bbb="true"  />
        <RefData RefTag="test5" bbb="false" />
        <RefData RefTag="test6" bbb="false" />
  </xsl:variable>

I am trying to get bbb attribure value using the RefTag value:

<xsl:if test="$myVar/RefData[@RefTag = 'test3']/@bbb">

this is not working.

VS XSL Debugger returns an error:
"To use a result tree fragment in a path expression, first convert it to a node-set using the msxsl:node-set() function."

I don't understand how to use msxsl:node-set() function, and anyway I prefer not to use msxsl namesapce.

Can anyone help here?

Best Answer

One solution that doesn't need the xxx:node-set() extension is the following:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <!--                                           -->
    <xsl:variable name ="myVar">
        <RefData RefTag="test1" bbb="false" />
        <RefData RefTag="test2" bbb="false" />
        <RefData RefTag="test3" bbb="false" />
        <RefData RefTag="test4" bbb="true"  />
        <RefData RefTag="test5" bbb="false" />
        <RefData RefTag="test6" bbb="false" />
    </xsl:variable>
 <!--                                           -->
    <xsl:variable name="vrefVar" select=
     "document('')/*/xsl:variable[@name='myVar']"
     />
 <!--                                           -->
    <xsl:template match="/">
      <xsl:value-of select="$vrefVar/*[@RefTag='test3']/@bbb"/>
    </xsl:template>
</xsl:stylesheet>

When the above transformation is applied on any XML document (not used), the wanted result is produced:

false

Do note the use of the XSLT document() function in order to access the required <xsl:variable> simply as an element in an xml document.

Related Topic