Re: [xsl] How do you change markup to text in XSLT?

Subject: Re: [xsl] How do you change markup to text in XSLT?
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Fri, 7 Sep 2001 09:56:20 +0100
Hi Andrew,

> The only generic way I can see right now is to reconstruct the XML
> using templates, something like:
> <xsl:template match="*" mode="out">
>   &lt;<xsl:value-of select="name()"/>&gt;
>     <xsl:apply-templates mode="out"/>
>   &lt;/<xsl:value-of select="name()"/>&gt;
> </xsl:template>
> With added functionality for outputting what you want (attributes,
> comment nodes, command nodes, etc).
> This is a horrible way to do it, is there another better/easier way?

If you're generating XML and you want the serialised version to be
placed in element content, then there's a nasty hack - you can use
disable-output-escaping to add a CDATA section around the XML that you

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

The usual warnings apply - not all processors support
disable-output-escaping and you have to watch out for the sequence ]]>
in the XML that you're copying.

If you're using MSXML, another option is to create your own
serialising extension function, something like:

<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;

You can then create an XML serialisation of a node with:

<xsl:template match="*" mode="out">
  <xsl:value-of select="my:serialize(.)" />

I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread