![]() |
![]() | ![]() | ![]() | Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - Can I un-CDATA my CDATA section and elaborate a transformation for the contained data? >Thread Next - Re: Can I un-CDATA my CDATA section and elaborate a transformation for the contained data? Re: Can I un-CDATA my CDATA section and elaborate a transformation for the contained data?To: NULL Date: 3/5/2006 8:58:00 PM troppfigo@e... wrote: > I have this example of xml > > <?xml version="1.0"?> > <xml> > <![CDATA[ > <metadata> > <title>Embedded Markup</title> > <body>Someone told to me...</body> > </metadata> > ]]> > </xml> This is usually very poor design. The content of a CDATA section is just text: by putting the CDATA markup round it you are explicitly telling the XML parser that it must no longer be regarded as markup, so as far as the software is concerned, <metadata> and all the rest of the content is just a bunch of characters with no special meaning. See http://xml.silmaril.ie/authors/cdata > I want to extract the contained data from <body> tag using an xslt > transformation. > I want to obtain this > > <html> > Someone told to me... > </html> > > > it is possible to make this operation? > Can you post some example code? You must remove the CDATA code first. Then your XML software will be able to treat the markup as markup, and access the elements properly (and tell whoever generated it that they are making it impossible to process as XML otherwise). As it currently stands, you'd need to process the file twice. This first piece of XSLT will remove the CDATA markup (provided you use a processor that supports disable-output-escaping -- support for it is not obligatory, so only some software will do it properly): <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml"/> <xsl:template match="xml"> <xml> <xsl:value-of disable-output-escaping="yes" select="."/> </xml> </xsl:template> </xsl:stylesheet> This produces: <?xml version="1.0"?> <xml> <metadata> <title>Embedded Markup</title> <body>Someone told to me...</body> </metadata> </xml> Now it's real markup, so you can process it with another stylesheet, eg: <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html"/> <xsl:template match="xml"> <html> <head> <title>Test</title> </head> <body> <xsl:apply-templates select="metadata/body"/> </body> </html> </xsl:template> <xsl:template match="body"> <p> <xsl:apply-templates/> </p> </xsl:template> </xsl:stylesheet> to produce what you appear to mean. ///Peter -- XML FAQ: http://xml.silmaril.ie/ | ![]() | ![]() | ![]() |
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | |||||
|
