xml – XSLT转换期间的Unescape

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

在我的XML文档中,我有类似的节点(为简洁起见,这里是截断的):

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

我想要做的是unesacape转义的字符,以便& lt;变得<和& gt;理想情况下,只有当它们出现在脚本节点中时才成为>.

最终的输出将是:

<script type="text/javascript"> 
  document.write('<script>')
  </script>

这是可能的,以及如何提出任何建议?

解决方法

使用html序列化方法,脚本内容不会被转义.

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序列化方法将脚本内容声明为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"/>

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念