Unescape during XSLT transform
I'm transforming an XML document using XSLT into XHTML, using Saxon, XSLT 2.0 compliant.
Within my XML documents I have nodes like so (truncated here for brevity):
<script type="text/javascript">
document.write('<script>')
</script>
What I want to be able to do is unesacape the escaped chara开发者_运维百科cters so that <
becomes <
and >
becomes >
, ideally only when they occur within the script nodes.
The final output would then be:
<script type="text/javascript">
document.write('<script>')
</script>
Is this possible, and any suggestions as to how?
With the html
serialization method, script
content doesn't get escaped.
From http://www.w3.org/TR/xslt#section-HTML-Output-Method
The
html
output method should not perform escaping for the content of thescript
andstyle
elements
Update
As Dr. @Michael Kay have commented, if you are generating XHTML (and sending with correct MIME type) for browsers wich understand XHTML, then you don't need to worry about unescaping. Also, it should be pointed out that inline script is not considered good practice.
If you still want to generate XHTML following guidelines for legacy browsers, with xml
serialization method, you can declare script
content as CDATA section.
From http://www.w3.org/TR/xslt#section-XML-Output-Method
The
cdata-section-elements
attribute contains a whitespace-separated list of QNames. Each QName is expanded into an expanded-name using the namespace declarations in effect on thexsl:output
element in which the QName occurs; if there is a default namespace, it is used for QNames that do not have a prefix. The expansion is performed before the merging of multiplexsl:output
elements into a single effectivexsl:output
element. If the expanded-name of the parent of a text node is a member of the list, then the text node should be output as a CDATA section
As example:
<xsl:output cdata-section-elements="xhtml:script xhtml:style"
xmlns:xhtml="http://www.w3.org/1999/xhtml"/>
Yes it's possible: http://www.w3.org/TR/xslt#disable-output-escaping
精彩评论