lone_wolf
lone_wolf

Reputation: 1

WSO2 XSLT Mediator not resolving xpath

hopefully someone can point out the blindingly obvious to me - I want to perform a few simple translations on a soap payload before I send to a backend service - crucially, I need access to all the soap-headers delivered in the original payload. My naive(?) thinking was to simply set the source attribute for the <xslt> mediator to "/" as Im aware by default it will start from the first child of the <body>, and I really need access to the headers. WSO2 returns a "The evaluation of the XPath expression / did not result in an OMNode" error.

Are there one or more WSO2/xpath interworking quirks that Ive missed in the literature? Any pointers gratefully recieved, thanks

Upvotes: 0

Views: 1026

Answers (2)

Indika Sampath
Indika Sampath

Reputation: 1063

As Tishan mentioned you can use $header synapse xpath variable to access soap headers in message context. But it's bit tricky when it comes to xslt mediator. You cannot directly access message context values inside xslt stylesheet. But it is possible to pass these values as parameters and use in transformation. Let's see how we can achieve this.

Below is how account.xslt file looks like. Please note that there are two parameters called PARAM_SSN and PARAM_ACCT_NO used to assign value for <ssn></ssn> and <accountNumber></accountNumber>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="PARAM_SSN"></xsl:param>
    <xsl:param name="PARAM_ACCT_NO"></xsl:param>
    <xsl:template match="/">
        <account xmlns="http://services.samples">
            <ssn>
                <xsl:value-of select="$PARAM_SSN"></xsl:value-of>
            </ssn>
            <accountNumber>
                <xsl:value-of select="$PARAM_ACCT_NO"></xsl:value-of>
            </accountNumber>
            <accountHolder>
                <xsl:value-of select="//name"></xsl:value-of>
            </accountHolder>
        </account>
    </xsl:template>
</xsl:stylesheet>

Above file saved in registry of WSO2 ESB under /_system/governance/transform/account.xslt

Next is sample proxy which do the transformation with account.xslt

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="TransformExample"
       transports="https,http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <xslt key="gov:/transform/account.xslt">
            <property name="PARAM_SSN" expression="$header/seccode"/>
            <property name="PARAM_ACCT_NO" expression="$trp:acctNo"/>
         </xslt>
         <log level="custom">
            <property name="Transformed Payload" expression="$body"/>
         </log>
      </inSequence>
      <outSequence>
         <send/>
      </outSequence>
   </target>
   <description/>
</proxy>

Here you could see that inside <xslt> mediator, I am passing values for two parameters define in xslt by accessing message context. Value for PARAM_SSN taken from soap header and value for PARAM_ACCT_NO taken from transport header. This proxy service called by soapUI with below payload.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header>
    <seccode>987654321</seccode>
   </soapenv:Header>
   <soapenv:Body>
    <request>
        <name>Indika Sampath</name>
    </request>
   </soapenv:Body>
</soapenv:Envelope>

Also I am sending acctNo as transport header along with request. Once this hit the proxy, you could see transformed output log as below in console.

[2016-01-09 07:19:02,146]  INFO - LogMediator Transformed Payload = <soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <account xmlns="http://services.samples"><ssn>987654321</ssn><accountNumber>123456789</accountNumber><accountHolder>Indika Sampath</accountHolder></account>
   </soapenv:Body>

Hope this would resolve your problem.

Cheers!

Upvotes: 1

Tishan
Tishan

Reputation: 890

You can access SOAP headers using Synapse Xpath Variable $header in your source xpath (Ex:$header/wsa:To).
Hope this helps!!

Upvotes: 0

Related Questions