加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml – XSLT转换期间的Unescape

发布时间:2020-12-16 22:54:04 所属栏目:百科 来源:网络整理
导读:我正在使用XSLT将 XML文档转换为XHTML,使用Saxon,符合XSLT 2.0. 在我的XML文档中,我有类似的节点(为简洁起见,这里是截断的): script type="text/javascript" document.write('lt;scriptgt;') /script 我想要做的是unesacape转义的字符,以便 lt;变得和 gt;理
我正在使用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"/>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读