This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

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


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
generate:

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]