在我的XML文档中,我有类似的节点(为简洁起见,这里是截断的):
<script type="text/javascript"> document.write('<script>') </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
thescript
andstyle
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
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"/>