0

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.

1
  • 1
    What you need is to evaluate an XPath expression which is given as a string. As far as I know this is only possible in XSLT 3.0. What you could do is generate the XSLT using your XPath and then execute the generated XSLT. Commented Oct 12, 2014 at 14:29

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.