How to get XML from body of soap using XSLT

It happens sometime you want to extract XML body from SOAP message. In order to do that use below mentioned XSLT.

 

<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<xsl:stylesheet version=”1.0″
xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”
xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/”
xmlns=”http://www.voxbone.com/VoxAPI”>
 <xsl:template match=”/”>
        <xsl:apply-templates select=”soap:Envelope/soap:Body/*”/>
    </xsl:template>
    <xsl:template match=”@*|node()”>
        <xsl:copy>
            <xsl:apply-templates select=”@*|node()”/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Sample soap message

<soap:Envelope xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/” xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”>
   <soap:Body>
      <GetCountriesListResponse>
         <ArrayOfCountries>
            <Country>
               <CountryID>10</CountryID>
               <CountryName>ARGENTINA</CountryName>
               <CodeA2>AR</CodeA2>
               <CountryCode>54</CountryCode>
               <HasStates>false</HasStates>
            </Country>
            <Country>
               <CountryID>13</CountryID>
               <CountryName>AUSTRALIA</CountryName>
               <CodeA2>AU</CodeA2>
               <CountryCode>61</CountryCode>
               <HasStates>false</HasStates>
            </Country>
            <Country>
               <CountryID>21</CountryID>
               <CountryName>BELGIUM</CountryName>
               <CodeA2>BE</CodeA2>
               <CountryCode>32</CountryCode>
               <HasStates>false</HasStates>
            </Country>
         </ArrayOfCountries>
      </GetCountriesListResponse>
   </soap:Body>
</soap:Envelope>

OUTPUT

<?xml version=”1.0″?>
<GetCountriesListResponse>
<ArrayOfCountries>
<Country>
<CountryID>10</CountryID>
<CountryName>ARGENTINA</CountryName>
<CodeA2>AR</CodeA2>
<CountryCode>54</CountryCode>
<HasStates>false</HasStates>
</Country>
<Country>
<CountryID>13</CountryID>
<CountryName>AUSTRALIA</CountryName>
<CodeA2>AU</CodeA2>
<CountryCode>61</CountryCode>
<HasStates>false</HasStates>
</Country>
<Country>
<CountryID>21</CountryID>
<CountryName>BELGIUM</CountryName>
<CodeA2>BE</CodeA2>
<CountryCode>32</CountryCode>
<HasStates>false</HasStates>
</Country>
</ArrayOfCountries>
</GetCountriesListResponse>

This entry was posted in HowTo, xml and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *