This is the solution to embed ‘>’ and ‘<’ in ‘Javascript’ of ‘XSL’ file
I am feeling sick of embedding Javascript
code in XSL
, particularity working with >
and <
. All in All, I do not know why XSL
deals with <script>
and <xsl:script>
especially. If using <![CDATA[]]>
,<xsl:text disable-output-escaping="yes">
, >
and >
will output as >
and <
or throw an error. If using >
and <
, it outut >
, <
exactly the same, which is special only (or at leaat) in between <script>
or <xsl:script>
. Having failed googling, eventually I found the solution in our project: escaping the <
and >
signs for <script>
tag instead of escaping in Javascript code for them.
Javascipt - <script>
- Js Template
1
2
3
4
5
6
7
8
9
10
<xsl:template name="javascript">
<xsl:param name="code" />
<xsl:text disable-output-escaping="yes">
<script type="text/javascript"> /* <![CDATA[ */
</xsl:text>
<xsl:value-of select="$code" disable-output-escaping="yes" />
<xsl:text disable-output-escaping="yes">
/* ]]> */ </script>
</xsl:text>
</xsl:template>
- Call “JavaScript” Template
1
2
3
4
5
6
7
8
<xsl:template match="/">
<xsl:call-template name="javascript">
<xsl:with-param name="code">
<![CDATA[ function loop(){ for(let i=0; i++; i<10){ console.log(i) }
} ]]>
</xsl:with-param>
</xsl:call-template>
</xsl:template>
Output from brower:
CSS - <style>
Thus, we can do the same thing on embedding css code.
1
2
3
4
5
6
7
8
9
<xsl:template name="style">
<xsl:text disable-output-escaping="yes">
<style> /* <![CDATA[ */
</xsl:text>
<xsl:text disable-output-escaping="yes"> </xsl:text>
<xsl:text disable-output-escaping="yes">
/* ]]> */ </style>
</xsl:text>
</xsl:template>
XSL
is powerful, but some tricks are really painful.