xsl-list
[Top] [All Lists]

Re: cdata-section-elements - not wrapping real xml in cdata markers

2003-11-10 14:43:01
Abie,

You wrote:
I am trying to certain elements in my output xml wrapped in cdata markers, so I'm trying to use cdata-section-elements="myElement". well it turns out that when the text in the element is not valid xml, it gets wrapped in the cdata markers just fine. but when the "text" in the element is valid xml, it does not get wrapped up. this is a problem for me, b/c then it means that when in the next stage my output xml is processed, doing an "xsl:value-of" on myElement will not yield the text that I want to be there.

I guess it's happening b/c I only told the processor that the text of myElement itself should be cdata, but once there's valid xml inside (ie. more elements), then it's a new element. (correct me if I'm wrong.)

so the question is, is there a way to wrap *everything* inside of an element in cdata markers, including child xml?

I think the problem here is that you're trying to slip back and forth across the line between XML and serialized-text-representing-XML -- but that's a line you aren't supposed to slip across so readily.

Take this example XML:

<warning>This is <emph>my</emph> string: lay off!</warning>

The value of this node is the string "This is my string: lay off!".

But it sounds like you want it to be "This is <emph>my</emph> string: lay off!"

The way of representing this latter string in XML is:

   "This is &_lt;emph>my&_lt;/emph> string: lay off!"

or "This is &_lt;emph&_gt;my&_lt;/emph&_gt; string: lay off!"

or "<![CDATA[This is <emph>my</emph> string: lay off!]]>"

or "This is <![CDATA[<]]>emph>my<![CDATA[<]]>/emph> string: lay off!"

or any of an indefinite number of possibilities. (Note: underscores inserted to fool mailers.)

If your stylesheet is building the node tree:

--warning
  --text()--"This is "
  --emph
    --text()--"my"
  --text()--" string: lay off!"

How should the serializer know that the <warning> node should be expressed as XML, but the <emph> node should be written in "escaped" form?

The cdata-elements feature is there to allow using CDATA marked sections as syntax sugar for the more cumbersome &_lt; notation. It doesn't magically turn a node tree into an escaped string serialization thereof, so it won't do this.

I think what you need is to write out the "elements" inside the string you want to preserve in escaped form. You can do this with templates easily enough. This means they won't look like elements in your stylesheet -- because you don't want elements in your output. For example (here are a couple of templates I have lying around that will do this):

<xsl:template name="write-starttag">
  <xsl:text>&lt;</xsl:text>
  <xsl:value-of select="local-name()"/>
  <xsl:for-each select="@*">
    <xsl:call-template name="write-attribute"/>
  </xsl:for-each>
  <xsl:text>></xsl:text>
</xsl:template>

<xsl:template name="write-endtag">
  <xsl:text>&lt;/</xsl:text>
  <xsl:value-of select="local-name()"/>
  <xsl:text></xsl:text>
</xsl:template>

<xsl:template name="write-attribute">
  <xsl:text> </xsl:text>
  <xsl:value-of select="local-name()"/>
  <xsl:text>="</xsl:text>
  <xsl:value-of select="."/>
  <xsl:text>"</xsl:text>
</xsl:template>

<xsl:template match="*" mode="escape-xml">
  <xsl:call-template name="write-starttag"/>
  <xsl:apply-templates/>
  <xsl:call-template name="write-endtag"/>
</xsl:template>

<xsl:template match="warning">
  <xsl:apply-templates mode="escape-xml"/>
</xsl:template>

If you use this kind of technique to write pseudo-XML to your output, that's what you'll get; and you are free to have it be expressed in CDATA marked sections or with delimiters escaped ad hoc ... either way, it doesn't matter. It's a moot point (or would the Brits say it's not a moot point?), a red herring, (6 * 1 || $dozen/2), etc.

Of course, being pseudo-XML this stuff won't parse -- but it sounds like you don't want it to.

Cheers,
Wendell


======================================================================
Wendell Piez                            
mailto:wapiez(_at_)mulberrytech(_dot_)com
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================


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