469,602 Members | 1,818 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

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

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?

Mar 5 '06 #1
4 2073
tr*******@excite.it 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, &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: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/
Mar 5 '06 #2
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.
Mar 6 '06 #3
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)".

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Mar 6 '06 #4
In article <fJ*******************@news000.worldonline.dk>,
Soren Kuula <do******@dongfang.dk> wrote:

% tr*******@excite.it 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>

[...]

% How about simply deleting (use sed or awk under unix, or write a small
% Perl / Java / Whatever program) "<![CDATA[" and "]]>" if it's valid XML
% in between them, and then proceed as with any other doc.?

Keeping in mind that names starting with "xml" are reserved.
--

Patrick TJ McPhee
North York Canada
pt**@interlog.com
Mar 12 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by pascal Joseph | last post: by
5 posts views Thread by Chris | last post: by
3 posts views Thread by Jorge Gallardo | last post: by
1 post views Thread by Alex | last post: by
3 posts views Thread by nano9 | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.