xml - 在 XSLT 转换期间取消转义

标签 xml xslt xslt-2.0

我正在使用符合 XSLT 2.0 的 Saxon 将使用 XSLT 的 XML 文档转换为 XHTML。

在我的 XML 文档中,我有这样的节点(为简洁起见,此处被截断):

  <script type="text/javascript"> 
  document.write('&lt;script&gt;')
  </script> 

我想要做的是取消转义转义字符,以便 &lt;变成 <&gt;变成 > ,理想情况下仅当它们出现在脚本节点中时。

最终输出将是:

  <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 the script and style 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 the xsl: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 multiple xsl:output elements into a single effective xsl: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/

相关文章:

java - 如何使用 Java 按顺序读取相同的标记名值

xml - 来自 node.js 的 XSD 验证功能?

javascript - 如何在 xslt 中包含 javaScript 文件

xml - Xslt:如何拆分字符串并将类添加到两个单词

python - 如何从给定的 URL 下载此 XML 文件

xml - 是否可以使 Tokenizer 命名空间感知(使用 Splitter 时)

xml - 使 XSL 样式表与分页 XML 一起使用

xslt - xsl :variable xsl:copy-of select

xml - 从 XSLT 2 中的元素值中的语义层次结构创建父子元素

xml - XSLT:如何用所有 sibling 的串联替换第一个 sibling ?