In XSLT, how do you select/copy part of the document as text only?
In XSLT, how do you select/copy part of the document as text only?
The change I want to make is to开发者_C百科 take part of the tree (tags and values both) and output it as text only (preferably with brackets HTML-encoded).
I tried to put a CDATA around my copy-of, but it simply put the copy-of command inside my document.
Edit: See comment below
Try this, but note that it won't handle nested CDATA correctly:
<xsl:text disable-output-escaping="yes"><</xsl:text>
<xsl:text>![CDATA[</xsl:text>
<xsl:copy-of select="..." />
<xsl:text>]]</xsl:text>
<xsl:text disable-output-escaping="yes">></xsl:text>
That's a strange requirement.
Since XSLT works on a parsed document model, you cannot do this reliably. In particular, the distinction between equivalent notations will necessarily be lost. Equivalent notations include things like <tag></tag>
versus <tag/>
, or é
versus é
.
That said, a general approach that might work would be using the mode
attribute of xsl:template
and xsl:apply-template
to switch to a mode that explicitly render all elements as text. In effect you would be writing a XML serializer in XSLT.
One issue though is that you would have to double-escape special characters such as <>"'
when present in attribute values and text nodes. And XSLT is quite inefficient at this sort of string munging.
Another issue would be rendering namespace prefixes reasonably. You can almost certainly do it, but that be quite horrible.
精彩评论