0

I am trying to use a variable that has xpath in for-each. But it is giving me an error that Expression must evaluate to a node-set.

NodeName is defined as

<xsl:variable name="NodeName" select="name(.)"/>

<xsl:variable name="SyncPath" 
              select="concat('/combinedxml/com.csc_PolicySyncRs/',$NodeName)"/>

and here is for-each loop

<xsl:for-each select="$SyncPath/*">

user433023
  • 237
  • 1
  • 4
  • 13
  • 1
    Could you please provide nicely-formatted XSLT fragment you're referring to? – oiavorskyi Apr 08 '11 at 14:51
  • Could you show how $NodeName is being defined? – oiavorskyi Apr 08 '11 at 14:59
  • 1
    This is a duplicate of http://stackoverflow.com/questions/4377811/using-dynamic-xpath-in-xslt or http://stackoverflow.com/questions/4682151/xslvariable-as-xpath-value-for-other-xsl-tag or http://stackoverflow.com/questions/1551526/is-it-possible-to-use-a-dynamic-xpath-expression-in-a-xslt-style-sheet or one of http://www.google.com/search?q=site%3Astackoverflow.com+xslt+dynamic+xpath –  Apr 08 '11 at 15:10
  • 1
    For this case use: `` –  Apr 08 '11 at 15:13
  • @Alejandro: Sorry, I saw your comment after submitting my answer. I prefer to answer such questions -- don't have enough time to find a duplicate. And actually, redundancy is not always a bad thing. – Dimitre Novatchev Apr 09 '11 at 04:35
  • @Dimitre: No problem. I'd just commented because I didn't want to left the OP without a simple answer. –  Apr 09 '11 at 13:31

3 Answers3

2

I will take wild guess and assume you're interested in converting variable to node set in order to use it later in XPath. This could be done using extension function exsl:node-set(). The documentation have examples of usage.

Quote:

This use case shows the result of using exsl:node-set() to convert a result tree fragment to a node-set.

source

<doc>
   <one />
   <two />
   <three />
   <four />
</doc>

stylesheet

<!--  Test exslt:node-set applied to a result tree fragment  -->
<xsl:variable name="tree">
   <a>
      <b>
         <c>
            <d />
         </c>
      </b>
   </a>
</xsl:variable>
<xsl:template match="/">
   <out>
      <xsl:value-of select="count(exslt:node-set(//*))" />
   </out>
</xsl:template>

result

<out xmlns:exslt="http://exslt.org/common">5</out>
oiavorskyi
  • 2,893
  • 1
  • 20
  • 23
1

Use:

<xsl:variable name="SyncPath"
    select="/combinedxml/com.csc_PolicySyncRs/*[name()=$NodeName]"/>
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
0

XSLT 1.0 (and indeed 2.0) has no standard facility for constructing an XPath expression as a string and then evaluating it. Many processors have an extension function to do this, variously called dyn:evaluate, saxon:evaluate, etc.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164