Is there a way to transform cdata sections of an XML document using an XSL template?

Take this XML.

Is there a way to transform the content of a CDATA section using XSLT?

+2


source to share


2 answers


Read this article - CDATA Sections

SUMMARY: In an XSLT stylesheet, the CDATA section is just a utility to stop you from having to run '<' etc. The target you are targeting is to copy something you have in your XML source right into your HTML Output. The xsl: copy-of element is there for just that purpose. xsl: copy will give an exact copy of what you choose, including attributes and content.

XML document.



<?xml version="1.0" encoding="iso-8859-1"?>
<know>
   <title/>
   <topic title="" href="">
     <![CDATA[
         Text
        ]]>
    </p>    
   </topic>
</know>

      

xsl Document.

<xsl:stylesheet  
   version="1.0" 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="html" indent="yes"/>

  <xsl:template  match="know">
     <xsl:value-of select="title"/>
     <xsl:for-each select="topic">
        <xsl:value-of select="@title"/> 
             <xsl:value-of select="." disable-output-escaping="yes"/> 
     </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

      

+3


source


XSLT treats CDATA sections as plain text, so you can treat them the same way as text nodes. Note that XSLT does not keep CDATA sections separate from surrounding text. Thus, if you have

<foo>bar <![CDATA[baz]]> qux</foo>

      



The original tree will be

  • Document
    • Element: foo
      • Text: "bar baz qux"
+1


source







All Articles