You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Yuri de Wit <yu...@metaserver.com> on 2002/05/01 20:07:16 UTC

Parsing CDATA into a document

Is it possible in XSLT to parse a CDATA node into a document node (assuming
that there is a valid document in the CDATA node)?

For instance, in the following source document I would like to output the
CDATA content of INPUT as XML on the result document.

            <INPUT name="PurchaseOrder" type="???">
			<![CDATA[<?xml version="1.0" encoding="UTF-8"?>
				<data>
				    <PurchaseOrder>
				        <contact type="string" />
				        <address type="string" />
				        <items>
				            <name type="string" />
				            <price type="double" />
				            <quantity type="int" />
				        </items>
				        <customer type="string" />
				    </PurchaseOrder>
				</data>]]>
		</INPUT>

The result should look like the following document:

            <INPUT name="PurchaseOrder" type="???">
				<data>
				    <PurchaseOrder>
				        <contact type="string" />
				        <address type="string" />
				        <items>
				            <name type="string" />
				            <price type="double" />
				            <quantity type="int" />
				        </items>
				        <customer type="string" />
				    </PurchaseOrder>
				</data>
		</INPUT>

Re: Parsing CDATA into a document

Posted by Peter Davis <pe...@pdavis.cx>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wednesday 01 May 2002 11:07, Yuri de Wit wrote:
> Is it possible in XSLT to parse a CDATA node into a document node (assuming
> that there is a valid document in the CDATA node)?

No.

The best you can do is 

<xsl:apply-templates select="INPUT">
<xsl:template match="INPUT">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:value-of select="." disable-output-escaping="yes"/>
  </xsl:copy>
</xsl:template>

But this doesn't do validation of the output, and you will end up with an 
illegal '<?xml version="1.0" encoding="UTF-8"?>' at the top of the <INPUT>.

You really really need to reconsider how and why it is necessary to include 
the other XML as CDATA.  You should really try to find another way to do it; 
there are so many issues that will come up down the road.  But if you can't 
change it, then good luck!

- -- 
Peter Davis
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE80FWcNSZCJx7tYycRAplLAJ9zohRewt+zv5e9madZ7NHm5t6MBwCeLC2u
R7qrBAsFWVVvI0aHqQUK9wc=
=cIPc
-----END PGP SIGNATURE-----


Re: Parsing CDATA into a document

Posted by Elliotte Rusty Harold <el...@metalab.unc.edu>.
At 2:07 PM -0400 5/1/02, Yuri de Wit wrote:
>Is it possible in XSLT to parse a CDATA node into a document node (assuming
>that there is a valid document in the CDATA node)?
>

No, most importantly because XSLT has no clue what's CDATA and what 
isn't (and secondly because it can't parse strings). Don't use CDATA 
sections as structure. They're syntax sugar, nothing more.

Exercise: go through your documents and replace all CDATA sections 
with escaped text that uses &lt; and &amp;. If anything breaks, 
you're doing something wrong.
-- 

+-----------------------+------------------------+-------------------+
| Elliotte Rusty Harold | elharo@metalab.unc.edu | Writer/Programmer |
+-----------------------+------------------------+-------------------+
|          The XML Bible, 2nd Edition (Hungry Minds, 2001)           |
|             http://www.cafeconleche.org/books/bible2/              |
|   http://www.amazon.com/exec/obidos/ISBN=0764547607/cafeaulaitA/   |
+----------------------------------+---------------------------------+
|  Read Cafe au Lait for Java News:  http://www.cafeaulait.org/      |
|  Read Cafe con Leche for XML News: http://www.cafeconleche.org/    |
+----------------------------------+---------------------------------+