Re: [xsl] Copying

Subject: Re: [xsl] Copying
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 29 Nov 2001 17:32:33 +0000
Hi Jose,

> So, I want to serialize the content of the element to HTML and
> including in the result tree...

There are three ways that I know of for doing this. The purest way is
to write a set of templates that serializes the nodes, for example:

<xsl:template match="*" mode="serialize">
  &lt;<xsl:value-of select="name()" />>
    <xsl:apply-templates mode="serialize" />
  &lt;/<xsl:value-of select="name()" />>

(but expanded to add attributes and so on).

The second option is to use diable-output-escaping to wrap a CDATA
section around the XHTML that you want to serialize, as follows:

<xsl:template match="atag">
  <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
  <xsl:copy-of select="." />
  <xsl:text disable-output-escaping="yes">]]></xsl:text>

(This might not work in all processors - disable-output-escaping is
usually something to avoid.)

Finally, you can create an extension function that does it for you. If
you're using MSXML, for example, then you can define a serialize
template in 'my' namespace with:

<msxsl:script implements-prefix="my" language="javascript">
  function serialize(nodes) {
    var XMLstring;
    for (var i = 0; i < nodes.length; i++) {
      XMLstring += nodes.item(i).xml;
    return XMLstring;

And then serialize the content of the atag element with:

<xsl:template match="atag">
  <xsl:value-of select="my:serialize(node())" />

You can probably come up with similar extension functions for other
processors, depending on the APIs that they support.

I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread