Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get xpath from xsl variable

Tags:

xslt

xpath

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?

like image 900
Schwartser Avatar asked Jan 14 '09 16:01

Schwartser


People also ask

What is XPath variable in XSLT?

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.

What is the difference between XPath and XML?

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.

Is it possible to evaluate a string as an XPath expression?

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 …

What is the difference between variable and attribute in XSLT?

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'" />


2 Answers

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.

like image 114
Dimitre Novatchev Avatar answered Sep 20 '22 10:09

Dimitre Novatchev


<xsl:variable name="myVariable" select="msxsl:node-set($myVar)"/>

You can avoid msxsl namespace by moving variable contents to the source xml.

like image 33
Azat Razetdinov Avatar answered Sep 21 '22 10:09

Azat Razetdinov