我正在使用符合 XSLT 2.0 的 Saxon 将使用 XSLT 的 XML 文档转换为 XHTML。
在我的 XML 文档中,我有这样的节点(为简洁起见,此处被截断):
<script type="text/javascript">
document.write('<script>')
</script>
我想要做的是取消转义转义字符,以便 <
变成 <
和 >
变成 >
,理想情况下仅当它们出现在脚本节点中时。
最终输出将是:
<script type="text/javascript">
document.write('<script>')
</script>
这可能吗,有什么建议吗?
最佳答案
使用 html
序列化方法,script
内容不会被转义。
来自 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
更新
正如@Michael Kay 博士评论的那样,如果您正在为理解 XHTML 的浏览器生成 XHTML(并使用正确的 MIME 类型发送),那么您无需担心转义。另外,应该指出内联脚本不是好的做法。
如果您仍想按照遗留浏览器的准则生成 XHTML,使用 xml
序列化方法,您可以将 script
内容声明为 CDATA 部分。
来自 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
例如:
<xsl:output cdata-section-elements="xhtml:script xhtml:style"
xmlns:xhtml="http://www.w3.org/1999/xhtml"/>
关于xml - 在 XSLT 转换期间取消转义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4693220/