Unescape when transforming XSLT

I am converting an XML document using XSLT to XHTML using Saxon compatible with XSLT 2.0.

In my XML documents, I have the following nodes (shortened here for brevity):

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

What I want to do is unesacape escaped characters, so &lt; becomes < and &gt; becomes > , ideally, only when they occur in script nodes.

The end result will be:

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

Is this possible, and any suggestions as to how?

+4
source share
2 answers

Using the html serialization method, the contents of the script cannot be escaped.

From http://www.w3.org/TR/xslt#section-HTML-Output-Method

The html output method should not escap the content of script and style elements

Update

As Dr. @Michael Kay noted, if you create XHTML (and send with the correct MIME type) for browsers that understand XHTML, you need not worry about unescaping. In addition, it should be noted that the inline script is not considered good practice.

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

From http://www.w3.org/TR/xslt#section-XML-Output-Method

The cdata-section-elements attribute contains a list separated by spaces from QNames. Each QName is expanded by an expanded name using the declaration namespace, acting on xsl:output , in which the QName occurs; if there is a default namespace, it is used for QNames that do not have a prefix. Extension of several xsl:output elements executed before merging into a single effective xsl:output element. If the expanded parent name of the text node is a member of the list, then the text node should be displayed as a CDATA section

As an example:

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

Source: https://habr.com/ru/post/1335571/


All Articles