Xslt, javascript and unescaped html objects

I have a little problem with xslt, js and html objects, for example. in the template:

<script type="text/javascript">
    <xsl:value-of select="/some/node"/>
    for (var i = 0; i &lt; 5; i++) {
        //            ^^^ js error
    }
</script>

<script type="text/javascript">
    <xsl:value-of select="/some/node"/>
    for (var i = 0; i < 5; i++) {
        //            ^ xslt error
    }
</script>

<script type="text/javascript">
    <xsl:value-of select="/some/node"/>
    // <![CDATA[
    for (var i = 0; i < 5; i++) {
        //            ^ becomes &lt;
    }
    // ]]>
</script>


<script type="text/javascript">
    <xsl:value-of select="/some/node"/>
    for (var i = 0; i <xsl:value-of disable-output-escaping="yes" select="string('&lt;')"/> 5; i++) {
        // works of course
    }
</script>

Does anyone have an idea where my problem might come from? I always thought that the xslt processor would leave the contents of the <script /> unescaped element when using the html output method ...

i run libxslt2 version 1.1.24 on OSX that was installed using macportsports ...

+1
source share
5 answers

OK. long story, short answer:

, libxslt xslt html ... :

<script type="text/javascript">
    <xsl:value-of select="/some/node"/>
    <xsl:text disable-output-escaping="yes">
        // ^ does the trick ...
        for (var i = 0; i < 5; i++) {
            //            ^ works
        }
    </xsl:text>
</script>
+11

, xslt 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.
For example, a literal result element written in the stylesheet as
    <script>if (a &lt; b) foo()</script>
or
    <script><![CDATA[if (a < b) foo()]]></script>
should be output as
    <script>if (a < b) foo()</script>

XSLT- , .

, , , '<' '&' .js.

+4

CDATA ; . disable-output-escaping?

UPDATE: Xalan, disable-output-escaping , , no, XSL :

  • CDATA block:

    for (var i = 0; i `&lt;` foo.length; i++) {
    }
    
  • CDATA block:

    <![CDATA[
    
    for (var i = 0; i < foo.length; i++) { … }
    
    ]]>
    
+1

CDATA

+1

xsl:output - html, CDATA . xsl:output xml, > .

To work around this problem, you can define a script element to not behave using the xsl: output element. you can also force the output method to use xml or html

<xsl:output method="xml" cdata-section-elements="script" />
...
<script type="text/javascript" language="javascript">
<![CDATA[
  for (var i = 0; i &lt; foo.length; i++) { … }
]]>
</script>
0
source

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


All Articles