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?
XSLT variable is defined as special tags used to declare a local or global variable that we make use of to store any values. The declared variables are referenced within an Xpath expression. Once it is set we cannot overwrite or update the variables. The scope of the element is done by the element that contains it.
XML is to data in a database as XPath is to SQL. This table contains this and other useful analogies between these two technologies. XML is a representation of data with simple notations to use. The notations consist of elements and attributes.
This is not natively possible in XSLT 1.0, but you can use an extension library such as dyn: The dyn:evaluate function evaluates a string as an XPath expression. Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. Provide details and share your research! But avoid …
The variables used in other programming language is completely different from the one of XSLT and they are variable. Here the attribute variable is case-sensitive. This variable is immediately referenced with XPATH expression using the name of the variable with the $ symbol instead of a Specified node. <xsl:variable name="animal" select="'cat'" />
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.
<xsl:variable name="myVariable" select="msxsl:node-set($myVar)"/>
You can avoid msxsl namespace by moving variable contents to the source xml.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With