Araneo Araneo - 1 month ago 22
Java Question

How to pass parameter in xsl apply-templates from Camel route

I want to pass xPath from Camel route to XSLT transformation and use it in

apply-templates select
. According to Camel documentation I have set parameter as Header and declare it in XSLT transformation. I am using xslt 2.0

I can see te value of xPath, but I does not work in
apply-templates select
. See the examples for detailed information:

route.java

from("direct:myRoute")
.setHeader("myParam", constant("/myxPath/example")
.to(XSLT_PATH);


xslt.xml

<xsl:param name="myParam"/>
<xsl:template match="/">
<xsl:element name="DummyElement">
<xsl:element name="ChildElement">
</xsl:element>
<xsl:value-of select="$myParam"/> - this will return provided xpath (/myxPath/example)
<xsl:apply-templates select="$myParam" - this doesn't work
mode="mymode"/>
</xsl:element>
</xsl:template>


I have done also test with variable and it also does not working:

<xsl:param name="myParam"/>
<xsl:variable name="myVariable" select="$myParam"/> - change here
<xsl:template match="/">
<xsl:element name="DummyElement">
<xsl:element name="ChildElement">
</xsl:element>
<xsl:apply-templates select="$myVariable" - change here
mode="mymode"/>
</xsl:element>
</xsl:template>


but when I provide xpath in param select all is working properly:

<xsl:param name="myParam" select="/myxPath/example"/> -change here
<xsl:variable name="myVariable" select="$myParam"/>
<xsl:template match="/">
<xsl:element name="DummyElement">
<xsl:element name="ChildElement">
</xsl:element>
<xsl:apply-templates select="$myVariable"
mode="mymode"/>
</xsl:element>
</xsl:template>


I looked at the topics which already exist, but I didn't get the solution.
Thanks in advance!

Answer

By default all headers from Camel message are available in XSLT. In my case I needed to put new header into my message:

.setHeader("myParam", constant("/myxPath/example"))

And I was correctly getting myParam in XSLT. However according to '@Martin Honnen' and @Orabîg comments, Camel is passing these parameters as strings, so in XSLT I've received:

<xsl:param name="myParam" select="'/myxPath/example'"/> - note additional single quotation mark 

In XSLT 2.0 is not possible to convert xsl:param values from string to xpath directly.