Saturday, November 24, 2012

Extracting CDATA section using XSLT

In a XML message, we pass some data which we might not want to be parsed by xml parsers.
Characters like "<>" are illegal in XML elements.  To save such characters we use CDATA section in our xml message.
For instance in the following xml message, we use CDATA block to pass <metadata> to other end without parsing "<,>" signs.

    <ser:getArtifactContentResponse    xmlns:ser="">
   <metadata xmlns="">
        <description>scann doc&lt;/description>

To extract the CDATA section,( here it contains xml message) we could use simple xslt script.

<xsl:stylesheet xmlns:xsl=""
    version="1.0" xmlns:ns="">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" />
    <xsl:template match="/">
        <xsl:value-of select="//ns:getArtifactContentResponse/ns:return/text()" disable-output-escaping="yes"/>
The output would be ;

   <metadata xmlns="">
        <description>scann doc</description>


  1. Hi, nice description.Thanks for your help..


  2. This comment has been removed by the author.

  3. How symbols like "&" can handled in the CDATA section transformation?

    Means the input within cdata section is "&" and the output for it should be "&amp;". Please suggest