Re: [xsl] cdata?

Subject: Re: [xsl] cdata?
From: Trevor Nash <tcn@xxxxxxxxxxxxx>
Date: Sat, 17 Nov 2001 10:48:43 +0000
Michael,

>I mean that I seem to have trouble accessing my cdata "This is my" and
>"text." from something such as this common lil structure:
>
><p> This is my <b> bold </b> text. </p>
>
>Am I not supposed to format XML that way or am I just trying to access the
>text in some incorrect way?
>
That is perfect XML.  When we see 'cdata' referred to on this list it
usually means someone is using the <![CDATA[...]]> construct, and it's
usually bad news.  To avoid confusion it is customary to refer to
'text nodes' or just 'text' even though cdata (character data) is also
correct.

The above XML is modelled in XSLT by a node tree:
   an element 'p' with three children
        1: a text node containing ' This is my '
        2: an element 'b'
        3: a text node containing ' text. '

The 'b' element in turn has one child, a text node containing ' bold
'.

This is quite different from:
<p><![CDATA[ This is my <b> bold </b> text. ]]></p>
Here we have a 'p' element with a single text child containing
' This is my <b> bold </b> text. '

The most common misunderstanding which gives rise to this sort of
question is confusion between xsl:value-of and xsl:copy-of.  Given the
XML you wrote (without the CDATA) then inside a template

 <xsl:template match="p">

 <xsl:value-of select="."/> will give you
   " This is my  bold  text. "
 which is just the *text* content of the current node and its
children.
If you want the whole structure (with the 'b' element), use:
 <xsl:copy-of select="."/>

 Maybe you want only the text directly under the current element, so
you might try
  <xsl:value-of select="text()" />
  But this gives you just " This is my " because value-of only takes
the first node out of  node set.  While
  <xsl:copy-of select="text()" />
will give you:
 " This is my  text. "

If that doesn't help, you will have to tell us more precisely what you
are trying to do and what is going wrong.

Regards,
Trevor Nash
--
Traditional training & distance learning,
Consultancy by email

Melvaig Software Engineering Limited
voice:     +44 (0) 1445 771 271 
email:     tcn@xxxxxxxxxxxxx

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread