Re: Javascript : from xsl to xml

Subject: Re: Javascript : from xsl to xml
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 03 Aug 2000 20:56:28 +0100
Michel,

>2. I have this Javascritp in my xsl :
>
><script language="JavaScript">
><![CDATA[function image(){
>         window.open('  NOT YET DEFINED ');
>}
>]]>
></script>
>
>I would like to know how to make the method "WINDOW.OPEN" work with a 
>variable that would get the information which is stored in the IMAGE 
>element of my xml, so putting the image in the opening window. I will have 
>340 different xml pages, one for each expression of the encyclopedia.

>From what I gather, you are generating this script element as part of the
output HTML from a stylesheet.  Just as with any other output that you
generate within an XSL page, you can insert values of particular XPath
expressions using xsl:value-of.  So if you want the value of the @href
attribute of the IMAGE element, you can use:

  <xsl:value-of select="/ROOT/SEE_PICTURE/IMAGE/@href" />

I'm sure you're aware of this, so I guess that the problem you're having is
inserting this into the javascript.  If you were to simply insert it into
your existing script element like:

<script language="JavaScript">
  <![CDATA[
  function image() {
    window.open('<xsl:value-of select="/ROOT/SEE_PICTURE/IMAGE/@href" />');
  }
  ]]>
</script>

then it would be within a CDATA section, and would thus not be interpreted
as XSLT (or XML for that matter).

Within the XSLT stylesheet, the CDATA section is purely a utility to stop
you from having to escape all the '<' etc. that you would have to
otherwise.  The CDATA section in your XSLT stylesheet does not translate
into a CDATA section in your output.  So your script element translates to:

<script language="JavaScript">
  function image() {
    window.open('  NOT YET DEFINED ');
  }
</script>

as there are no peculiar characters to be escaped within it.  Given that,
you could simply do:

<script language="JavaScript">
  function image() {
    window.open('<xsl:value-of select="/ROOT/SEE_PICTURE/IMAGE/@href" />');
  }
</script>

and the value of the IMAGE's @href will be inserted as an argument.  If
there *are* characters that need to be escaped in your javascript, then
there's no problem just stopping the CDATA section before the piece of XSLT
and then starting it again afterwards:

<script language="JavaScript">
  <![CDATA[
  function image() {
    window.open(']]><xsl:value-of
                      select="/ROOT/SEE_PICTURE/IMAGE/@href" /><![CDATA[');
  }
  ]]>
</script>

I hope that I've understood your problem correctly and that this helps you
solve it.

Cheers,

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