Wednesday, May 29, 2013

Datapower XI50 - Applying XPath on CDATA section

Problem 

The CDATA section in a XML is treated as text data and we cannot apply Xpaths on the content of the CDATA section. This causes problems when we have an XML under CDATA section which we want to manipulate using XPath on Datapower XI50 appliance

Solution

The CDATA section under an XML would need to stripped off and added to another XML container. Before we add it, we would need to apply dp:parse() datapower extension function to convert String to Node. Now we can apply any XPath expression for the resulting XML

Sample of the XSLT to strip the CDATA and apply it into another XML is as follows

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:in="http://www.openuri.org/"
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"  
    xmlns:dp="http://www.datapower.com/extensions"   
    extension-element-prefixes="dp"
    exclude-result-prefixes="dp"
    version="1.0">
  <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
  <xsl:template match="/">
  <soapenv:Envelope>
    <soapenv:Header />
         <soapenv:Body>
          <root>
             <!--Optional:-->
             <child>
                <xsl:variable name="inputRequest">
                     <xsl:copy-of select="@*"/>  
                    <xsl:value-of select="." disable-output-escaping="yes" />
                </xsl:variable>
                <xsl:copy-of select="dp:parse($inputRequest)" />
             </child>
          </root>
        </soapenv:Body>
    </soapenv:Envelope>    
</xsl:template>
</xsl:stylesheet>


The resulting XML would be

<root>
    <child>
        <!-- CONTENTS of XML under CDATA SECTION -->
    </child>
</root>

Now we can apply any XPath on this XML to transform it.

No comments:

Post a Comment