开发者

Unescape during XSLT transform

开发者 https://www.devze.com 2023-02-05 04:58 出处:网络
I\'m transforming an XML document using XSLT into XHTML, using Saxon, XSLT 2.0 compliant. Within my XML documents I have nodes like so (truncated here for brevity):

I'm transforming an XML document using XSLT into XHTML, using Saxon, XSLT 2.0 compliant.

Within my XML documents I have nodes like so (truncated here for brevity):

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

What I want to be able to do is unesacape the escaped chara开发者_运维百科cters so that &lt; becomes < and &gt; becomes >, ideally only when they occur within the script nodes.

The final output would then be:

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

Is this possible, and any suggestions as to how?


With the html serialization method, script content doesn't get escaped.

From 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

Update

As Dr. @Michael Kay have commented, if you are generating XHTML (and sending with correct MIME type) for browsers wich understand XHTML, then you don't need to worry about unescaping. Also, it should be pointed out that inline script is not considered good practice.

If you still want to generate XHTML following guidelines for legacy browsers, with xml serialization method, you can declare script content as CDATA section.

From 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

As example:

<xsl:output cdata-section-elements="xhtml:script xhtml:style"
            xmlns:xhtml="http://www.w3.org/1999/xhtml"/>


Yes it's possible: http://www.w3.org/TR/xslt#disable-output-escaping

0

精彩评论

暂无评论...
验证码 换一张
取 消