Search code examples
xmlxsltcdata

How to get CDATA from xml node using xsl ?


I am trying to get the CDATA content of an XML node using XSL. The node currently looks like this:

<node id="1" text="Book Information" ><![CDATA[This is sample text]]></node>

I need the This is sample text piece. Does anyone have any idea about this?

Thanks in advance.


Solution

  • Some other easy steps to achieve this;
    Used W3cschools editor to try out.
    Sample XML File :

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!-- Edited by XMLSpy® -->
    <catalog>
        <cd>
            <disk id="title"><![CDATA[Sample xml]]></disk >
            <disk id="artist"><![CDATA[Vijay]]></disk >
        </cd>
    </catalog>
    


    Sample XSL file :

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!-- Edited by XMLSpy® -->
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
      <html>
      <body>
        <h2>My CD Collection</h2>
        <table border="1">
          <tr bgcolor="#9acd32">
            <th>Title</th>
            <th>Artist</th>
          </tr>
    <xsl:for-each select="catalog/cd">
    
          <tr>
           <td><xsl:value-of select="/catalog/cd/disk[@id='title']"/></td>
           <td><xsl:value-of select="/catalog/cd/disk[@id='artist']"/></td>
           </tr>
    </xsl:for-each>
        </table>
      </body>
      </html>
    </xsl:template>
    </xsl:stylesheet>
    


    Final result is;
    alt text