Reputation: 188
I am trying to extract the value of the messageIdentifier in the following XML request:
<?xml version="1.0"?>
<ordersRequest>
<requestHeader>
<verb>get</verb>
<transaction>OrderRequest</transaction>
<version>1.0</version>
<consumer>Web</consumer>
<messageIdentifier>123456789</messageIdentifier>
</requestHeader>
</ordersRequest>
The intention is to pass the XPath information to the stylesheet so that the same XSL can be used for any other request (paymentRequest/requestHeader/messageIdentifier) and for any other node under requestHeader (paymentRequest/requestHeader/consumer). I came up with the following XSL for it:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:param name="xpath" select="/" />
<xsl:template match="/">
<xsl:value-of select="concat('/*/requestHeader/',$xpath)" />
</xsl:template>
</xsl:stylesheet>
The output here comes out as /*/requestHeader/messageIdentifier
. When I apply the above stylesheet with the the following edit <xsl:value-of select="/*/messageHeader/messageIdentifier" />
, I get the value 123456789
in the output. Why is there a difference in the output. Please help.
Upvotes: 0
Views: 733
Reputation: 167516
For a simple child element selection you could use
<xsl:param name="element-name" select="'messageIdentifier'"/>
and then
<xsl:value-of select="/*/requestHeader/*[local-name() = $element-name]"/>
If you really want to execute XPath expressions dynamically then Marcus Rickert is right, you need XSLT 3.0 and xsl:evaluate
or an extension function or element in earlier version which is then processor dependent or you need to generate XSLT code with one stylesheet and execute that in a second step.
Upvotes: 1