This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: How I can include javscript code ?
- From: Jeni Tennison <jeni at jenitennison dot com>
- To: "Charles Knell" <cknell at onebox dot com>
- Cc: xsl-list at lists dot mulberrytech dot com
- Date: Wed, 10 Jul 2002 15:37:36 +0100
- Subject: Re: [xsl] How I can include javscript code ?
- Organization: Jeni Tennison Consulting Ltd
- References: <20020710141817.EMEQ16740.mta04.onebox.com@onebox.com>
- Reply-to: xsl-list at lists dot mulberrytech dot com
Hi Charles,
> And if you don't want to type the <![CDATA[ ]]> tags in manually,
> you can use the cdata-section-elements attribute of the <xsl:output>
> element like this:
>
> <xsl:output cdata-section-elements="script">
Using a CDATA section in the stylesheet and getting a CDATA section in
the result are unrelated.
A CDATA section in the stylesheet is interpreted in just the same way
as a text node in which all the less-than signs and ampersands had
been escaped. So you could use:
<xsl:template match="/">
<html>
<head>
<script language="JavaScript">
function twiZone(Node){
if(Node < 1){
alert("This one.");
}
}
</script>
</head>
<body onload="twiZone(3)">
</body>
</html>
</xsl:template>
and the stylesheet will appear in exactly the same way to the XSLT
processor as if you used:
<xsl:template match="/">
<html>
<head>
<script language="JavaScript">
<![CDATA[
function twiZone(Node){
if(Node < 1){
alert("This one.");
}
}
]]>
</script>
</head>
<body onload="twiZone(3)">
</body>
</html>
</xsl:template>
The same goes for CDATA sections in the source document -- they're
simply not part of XSLT's data model.
Using the cdata-section-elements attribute on xsl:output, on the other
hand, affects how text nodes are output -- whether the less-than signs
and ampersands that they contain are escaped individually or whether
the entire text node is wrapped in a CDATA section. In either of the
above, if the script element's text nodes should be wrapped in CDATA
sections, you'll get:
<script language="JavaScript">
<![CDATA[
function twiZone(Node){
if(Node < 1){
alert("This one.");
}
}
]]>
</script>
otherwise you'll get:
<script language="JavaScript">
function twiZone(Node){
if(Node < 1){
alert("This one.");
}
}
</script>
The HTML output method doesn't wrap the script (or style) element's
content in a CDATA section, and doesn't escape the less-than signs or
ampersands; this is to ensure compatibility with legacy HTML
processors.
Cheers,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list