Re: including xsl variables inside of a javascript function

Subject: Re: including xsl variables inside of a javascript function
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Sat, 22 Jul 2000 11:49:16 +0100
Heather,

>	I have a javascript table imbedded inside of "<![CDATA[" "]]>".
>Right now the table includes static data.  I want the table data to be from
>an xml file.  Since the javascript must be inside of the CDATA tags, I can't
>just put "<xsl:value-of select="whatever"/>" as the value of a javascript
>variable.

The purpose of using the CDATA section in the stylesheet is to make life
easier for you so that you don't have to escape all those <s and &s and so
on.  The CDATA section in your stylesheet is simply marking out bits of the
information within the stylesheet that shouldn't be parsed as XML; using a
CDATA section in your stylesheet doesn't create a CDATA section in your
output.

So, to insert some stuff that *should* be interpreted as XML (like
<xsl:value-of select="$user" /> to insert the name of the user), you just
need to close the CDATA section, put in the element, and then open the
CDATA section again:

<xsl:template match="/">
...
<xsl:variable name="user" select="Login/UserInfo/UserName"/>
<SCRIPT language="JavaScript">
<![CDATA[
  ...
    rowA = new Object()
    rowA.data1 = ']]><xsl:value-of select="$user" /><![CDATA['
    rowA.data2 = 'needs'
    rowA.data3 = 'help'
    myTable.append_row(rowA)
  ...
]]>
</SCRIPT>
...
</xsl:template>

If there's very little escaping that you would have to do anyway, and lots
of information to be inserted into the script (which looks to be the case
from your example), then it might be simpler to get rid of the CDATA
section altogether:

<xsl:template match="/">
...
<xsl:variable name="user" select="Login/UserInfo/UserName"/>
<SCRIPT language="JavaScript">
  ...
    rowA = new Object()
    rowA.data1 = '<xsl:value-of select="$user" />'
    rowA.data2 = 'needs'
    rowA.data3 = 'help'
    myTable.append_row(rowA)
  ...
</SCRIPT>
...
</xsl:template>

I hope this helps,

Jeni

Dr Jeni Tennison
Epistemics Ltd * Strelley Hall * Nottingham * NG8 6PE
tel: 0115 906 1301 * fax: 0115 906 1304 * email: jeni.tennison@xxxxxxxxxxxxxxxx


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread