This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: How do you change markup to text in XSLT?
- To: "Andrew Kane" <akane at opentext dot com>
- Subject: Re: [xsl] How do you change markup to text in XSLT?
- From: Jeni Tennison <mail at jenitennison dot com>
- Date: Fri, 7 Sep 2001 09:56:20 +0100
- CC: xsl-list at lists dot mulberrytech dot com
- Organization: Jeni Tennison Consulting Ltd
- References: <MFENIKFAMDMOGMBPIAEEGEPMCCAA.akane@opentext.com>
- Reply-To: xsl-list at lists dot mulberrytech dot com
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">
> <<xsl:value-of select="name()"/>>
> <xsl:apply-templates mode="out"/>
> </<xsl:value-of select="name()"/>>
> </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
generate:
<xsl:template match="*" mode="out">
<xsl:text disable-output-escaping="yes"><![CDATA[</xsl:text>
<xsl:copy-of select="." />
<xsl:text disable-output-escaping="yes">]]></xsl:text>
</xsl:template>
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">
<![CDATA[
function serialize(nodes) {
var XMLstring;
for (var i = 0; i < nodes.length; i++) {
XMLstring += nodes.item(i).xml;
}
return XMLstring;
}
]]>
</msxsl:script>
You can then create an XML serialisation of a node with:
<xsl:template match="*" mode="out">
<xsl:value-of select="my:serialize(.)" />
</xsl:template>
I hope that helps,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list