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

Discussion in 'XML' started by troppfigo@excite.it, Mar 5, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Peter Flynn Guest

    Re: Can I un-CDATA my CDATA section and elaborate a transformationfor the contained data?

    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: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
    --
    XML FAQ: http://xml.silmaril.ie/
     
    Peter Flynn, Mar 5, 2006
    #2
    1. Advertising

  3. Re: Can I un-CDATA my CDATA section and elaborate a transformationfor the contained data?

    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.
     
    Joe Kesselman, Mar 6, 2006
    #3
  4. Re: Can I un-CDATA my CDATA section and elaborate a transformationfor the contained data?

    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
     
    Joe Kesselman, Mar 6, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Davison
    Replies:
    1
    Views:
    597
    Hal Rosser
    Jul 7, 2004
  2. Tjerk Wolterink

    CDATA in an xsl transformation

    Tjerk Wolterink, Jan 7, 2005, in forum: XML
    Replies:
    8
    Views:
    1,240
    Peter Flynn
    Jan 8, 2005
  3. prophecy8445
    Replies:
    0
    Views:
    1,043
    prophecy8445
    Nov 25, 2007
  4. Gilles DRIDI
    Replies:
    3
    Views:
    736
    James Kanze
    Mar 9, 2008
  5. cwdjrxyz
    Replies:
    4
    Views:
    189
    Randy Webb
    May 14, 2006
Loading...

Share This Page