1

I am using a variable say xpathvar in the XSLT whose value will be supplied at the time of call to XSLT. How can i achieve this? My XSLT file looks like -

<xsl:param name="xpathvar"/>
<xsl:param name="keyxpath"/>
 <xsl:template match="/">
  <listofResults>
    <xsl:for-each select="$xpathvar">
    <keyvalues><xsl:value-of select="xalan:evaluate(substring-before($keyxpath,'||'))"/></keyvalues>
    <keyvalues><xsl:value-of select="xalan:evaluate(substring-after($keyxpath,'||'))"/></keyvalues>
    </xsl:for-each>
  </listofResults>
 </xsl:template>
</xsl:stylesheet>

If I mention the variable in the for-each, it throws error. Please guide how can I achieve this.

Thanks!

Mark McLaren
  • 11,470
  • 2
  • 48
  • 79
Ritika
  • 13
  • 1
  • 3
  • please, provide a complete source XML document (but as minimal as possible) and describe what output results are required -- depending on this it may be possible to use pure XSLT 1.0. – Dimitre Novatchev Dec 09 '10 at 04:37

3 Answers3

0

I dont think this can be done like this you must use xpath so example would be

<template match="Node[name=$xpathvar]" />


<xsl:for-each select="*[name()=$xpathvar]">
</xsl:for-each>

I think it does not let you use the variable directly because it is not a nodeset.

Treemonkey
  • 2,133
  • 11
  • 24
  • If I apply this, it does not identify the xpath of variable. My XML is Permanent 361686 RATS ASE 17-DEC-80 800 361687 RATS ASE 17-DEC-80 800 and the output after applying the content given above is Permanent 361686 RATS ASE 17-DEC-80 800 361687 RATS ASE 17-DEC-80 800 Any pointers will be highly appreciated – Ritika Dec 08 '10 at 05:15
0

If I mention the variable in the for-each, it throws error

In XSLT 1.0 (it looks you are using Xalan), you can iterate over node sets, only. So $xpathvar should be an instance of node set data type.

In XSLT 2.0 you can iterate over sequence (including scalar values).

Also, if the string containing the "dynamic" XPath expression is simple enough (only QName test step, maybe positional predicates) this could be done (as is already answered in SO) with standar XSLT.

0

According to Global xsl:param containing xpath expression string it can't be done purely with plain old XSLT, you need to use the evaluate extension see: Xalan evaluate expression

Mark McLaren
  • 11,470
  • 2
  • 48
  • 79
  • I wouldn't give so strong answer. Take a look at this "walker" template implementation for a XPath subset: http://stackoverflow.com/questions/3530707/in-xslt-how-come-i-cant-set-the-select-attribute-of-a-value-of-using-xslattrib –  Dec 07 '10 at 15:25