Can I un-CDATA my CDATA section and elaborate a transformation for the contained data?

T

troppfigo

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>

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?
 
P

Peter Flynn

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, &lt;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:eek:utput 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:eek:utput 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
 
J

Joe Kesselman

As Peter said, Embedding mark up in a CDATA section is bad practice. XML
Namespaces, or special wrapper elements, are the currently recommended
approach for distinguishing when one markup language is embedded in another.
 
J

Joe Kesselman

If you really insist on doing it: You need to re-parse at least that
section of the document. Double-scanning it with XSLT is one way;
writing an extension function or a dedicated processor is another;
trying to write an XML parser in XSLT is a third. There was a recent
discussion that involved solving essentially the same problem; see the
thread with the subject "Please! Help me with this problem (Its urgent
for my project)".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top