473,320 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 2231
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Agathe | last post by:
Bonjour, Je souhaite insérer dans une table MySQL des données provenant d'un fichier texte grâce à un script PHP. Mon fichier porte l'extension "txt" et les données sont séparées par des ";'. ...
6
by: Raymond H. | last post by:
Bonjour, Je n'arrive pas à savoir comment lire via vb4 l'adresse d'un favoris dans le dossier des favoris où Internet Explorer place ses favoris. Par exemple, comment fait-on pour afficher l'url...
1
by: richard | last post by:
bonjour je me connecte à une base de données interbase/firebird en utilisant, KinterbasDB http://kinterbasdb.sourceforge.net/ pour ceux que ca interesse mais je pense que le probleme est le...
3
by: pascal Joseph | last post by:
J'ai un formulaire avec un seul champ text appelé "unite" et un bouton. En javascript j'aimerai utiliser un script qui interdise les valeurs de type "char" et soit supérieur à 0 J'ai trouvé...
5
by: Chris | last post by:
Bonjour, Plusieurs fichiers PHP d'un programme open source de compteur de visites viennent de se faire hacker sur mon serveur (hébergement mutualisé chez un fournisseur d'accès). Le hacker a...
3
by: Jorge Gallardo | last post by:
Hola de nuevo a todos... Agradecido a todos los que me habeis solucionado problemas anteriores... Pero como no es novedad, me surge otro. Recientemente buscando, adquiri un codigo para juntar...
1
by: Alex | last post by:
Ciao a tutti, sto sviluppando un applicazione windows, in breve all'interno dello stesso namespace ho un form con una datagrid e un thread che effettua dei controlli e "dovrebbe" caricare i dati...
15
by: Ciudad Tecnópolis | last post by:
Hola, primero que todo mil disculpas por postear una pregunta no relacionada al tema pero se que será muy útil para todos! Actualmente estoy presentando un desarrollo en .NET para una compañía y...
3
by: nano9 | last post by:
Hola gente quisiera que alguien me pudiera ayudar con un problemilla que tengo, resulta que estoy programando en ASP con C# y estoy usando un cadbgrid que se comporta parecido a un datagrid o...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.