0

I have a question on how to dynamically set the xpath expression in apply-templates select=?

<xsl:template match="CDS">
        <xsl:result-document href="{$fileName}">
            <xsl:copy>
                <xsl:apply-templates select="$xpathCondition"/>
            </xsl:copy>

        </xsl:result-document>
    </xsl:template>

This $xpathCondition am trying to set from java from properties file and setting to param in xsl.

        transformer.setParameter("fileName", "Test.xml");
        transformer.setParameter("xpathCondition", "CD[contains(Title/text(),'TEST')]");

$fileName is working as expected. But $xpathCondition is not working as expected.

  • 2
    Related: [dynamic xpath in xslt?](http://stackoverflow.com/questions/4630023/dynamic-xpath-in-xslt) – har07 May 03 '16 at 07:22

2 Answers2

1

There's no standard way of parsing a string as a dynamic XPath expression and executing it until you get to the xsl:evaluate instruction in XSLT 3.0. You really need to tell us which version you are using - the fact that you use xsl:result-document tells us that it's 2.0 or later, but beyond that we are guessing.

Many XSLT processors have an extension function called xx:eval() or similar.

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

The problem can be tackled in XSLT 3.0 using static parameters and shadow attributes. You can write:

<xsl:param name="xpathCondition" static="yes"/>

and then:

<xsl:apply-templates _select="{$xpathCondition}"/>

(Note the underscore in _select)

With 2.0 (or indeed 1.0) you can simulate this approach by doing a transformation on the stylesheet before executing it.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Am trying to use this solution. I changed XSLT Version to 3.0, Still facing the below exception : `Error at xsl:apply-templates on line 16 of Transform.xslt: XPST0003 XPath syntax error at char 0 on line 16 in {}: Unexpected token "" in path expression` – John Carter May 04 '16 at 07:37
  • In java am setting the parameter as `transformer.setParameter("xpathCondition", "CD[contains(Title/text(),'TEST')]");` – John Carter May 04 '16 at 07:39
  • Static parameters need to be provided at compile time, not at run-time. You'll therefore need to use the s9api interface rather than JAXP, calling XsltCompiler.setParameter(). – Michael Kay May 04 '16 at 09:11