Some Simple Updates To A DataPower XSLT stylesheet.

In the last post, we saw how an XML Firewall in loopback mode could be used to return a valid SOAP response to a service message.  The stylesheet used simply returned a static SOAP response that was independent of the input parameters.  The stylesheet returns a response containing the sum of two numbers based upon this WSDL.  In this post, we’ll modify the original stylesheet to return a response that is the sum of the two input parameters: arg0 and arg1.

The original stylesheet looks like the following:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:dp="http://www.datapower.com/extensions"
    xmlns:dpfunc="http://www.datapower.com/extensions/functions"
    xmlns:dpconfig="http://www.datapower.com/param/config"
    xmlns:func="http://exslt.org/functions"
    extension-element-prefixes="dp func"
    exclude-result-prefixes="dp dpfunc dpconfig func"> 
   
    <xsl:template match="/"> 
          <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.rcbj.com/">
            <soapenv:Header/>
            <soapenv:Body>
                <ws:sumResponse>
                    <return>6</return>
                </ws:sumResponse>
            </soapenv:Body>
        </soapenv:Envelope>
    </xsl:template>
</xsl:stylesheet>

We can extract the the input parameters, arg0 & arg1, when the following XPath expressions, respectively:

/*[local-name()= ‘Envelope’]/*[local-name()=’Body’]/*[local-name()=’sum’]/*[local-name()=’arg0′]/text()

/*[local-name()= ‘Envelope’]/*[local-name()=’Body’]/*[local-name()=’sum’]/*[local-name()=’arg1′]/text()

The following stylesheet elements will put the extracted values in variables called arg0 and arg1, respectively:

<xsl:variable name="arg1" select="/*[local-name()= ‘Envelope’]/*[local-name()=’Body’]/*[local-name()=’sum’]/*[local-name()=’arg0′]/text()"/>

<xsl:variable name="arg2" select="/*[local-name()= ‘Envelope’]/*[local-name()=’Body’]/*[local-name()=’sum’]/*[local-name()=’arg1′]/text()"/>

The sum of these two variables can be placed in a variable called sum with the following:

<xsl:variable name="sum" select="$arg1 + $arg2"/>

We can log the extracted values from the input message with the following:

<xsl:message dp:priority="’warn’">arg0: <xsl:value-of select="$arg0"/></xsl:message>

<xsl:message dp:priority="’warn’">arg1: <xsl:value-of select="$arg1"/></xsl:message>

We can reference the value of the sum variable with the following stylesheet element:

<xsl:value-of select="$sum"/>

Putting all of this together, we have:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:dp="http://www.datapower.com/extensions"
    xmlns:dpfunc="http://www.datapower.com/extensions/functions"
    xmlns:dpconfig="http://www.datapower.com/param/config"
    xmlns:func="http://exslt.org/functions"
    extension-element-prefixes="dp func"
    exclude-result-prefixes="dp dpfunc dpconfig func"> 
   
    <xsl:template match="/">
    <xsl:variable name="arg1" select="/*[local-name()= ‘Envelope’]/*[local-name()=’Body’]/*[local-name()=’sum’]/*[local-name()=’arg0′]/text()"/>
    <xsl:message dp:priority="’warn’">arg0: <xsl:value-of select="$arg0"/></xsl:message>
    <xsl:variable name="arg2" select="/*[local-name()= ‘Envelope’]/*[local-name()=’Body’]/*[local-name()=’sum’]/*[local-name()=’arg1′]/text()"/>
     <xsl:message dp:priority="’warn’">arg1: <xsl:value-of select="$arg1"/></xsl:message>
    <xsl:variable name="sum" select="$arg1 + $arg2"/>
        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.rcbj.com/">
            <soapenv:Header/>
            <soapenv:Body>
                <ws:sumResponse>
                    <return><xsl:value-of select="$sum"/></return>
                </ws:sumResponse>
            </soapenv:Body>
        </soapenv:Envelope>
    </xsl:template>
</xsl:stylesheet>

For the following request message:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.rcbj.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <ws:sum>
         <arg0>3</arg0>
         <arg1>4</arg1>
      </ws:sum>
   </soapenv:Body>
</soapenv:Envelope>

 

This stylesheet will return a response containing:

<soapenv:Envelope xmlns:ws="http://ws.rcbj.com/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <ws:sumResponse>
         <return>7</return>
      </ws:sumResponse>
   </soapenv:Body>
</soapenv:Envelope>

 

So, we have made our service simulator a little smarter.