5

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?

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

Comments

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

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

Comments

-1

Assuming this XML:

<test1>
  <RefData RefTag="test1"/>
  <RefData RefTag="test2"/>
  <RefData RefTag="test3"/>
  <RefData RefTag="test4"/>
  <RefData RefTag="test5"/>
  <RefData RefTag="test6"/>
</test1>

Something like this could work:

<xsl:template match="/">
  <xsl:apply-templates select="test1/RefData"/>
</xsl:template>

  <xsl:template match="RefData">
    <xsl:variable name="myVar">
      <xsl:choose>
        <xsl:when test="@RefTag = 'test1'">false</xsl:when>
        <xsl:when test="@RefTag = 'test2'">false</xsl:when>
        <xsl:when test="@RefTag = 'test3'">false</xsl:when>
        <xsl:when test="@RefTag = 'test4'">true</xsl:when>
        <xsl:when test="@RefTag = 'test5'">false</xsl:when>
        <xsl:when test="@RefTag = 'test6'">false</xsl:when>
        <xsl:otherwise>true</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <!--<text x="{$myVar}"/>-->
  </xsl:template>

1 Comment

I think that you didn't understand the question. this data is not in the input xml so I can't assume the xml abouve. I need to use xsl:variable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.