Jeni Tennison
>I have this Javascript 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>
Jeni then carries on answering another question... CDATA sections are designed to take the trauma out of escaping text that
has lots of characters that need escaping in (like < and &). Putting
<![CDATA[...]]> round a section essentially says "do not parse this section
- - everything in it should be interpreted as characters".
So, in the example: <![CDATA[
<img src="global.gif" alt="Go around the world" />
]]> is exactly the same as: <img src="global.gif" alt="Go around the world" /> The XML parser sees a string, not a tag. The XSLT processor therefore sees
a string, not a tag, and processes it as a string, not a tag, which means
that it outputs:
<img src="global.gif" alt="Go around the world" /> which gets displayed in your browser.
The goal you're aiming for is copying something that you have in your XML
source directly into your HTML output. The xsl:copy-of element is designed
precisely for this purpose. xsl:copy-of will give an exact copy of
whatever you select, including attributes and content.
So, you can just have XML like:
<page>
We offer the cheapest air fares to Bombay.
<img src="global.gif" alt="Go around the world" />
</page>
And then a template that says 'when you come across an img element, just
copy it':
<xsl:template match="img">
<xsl:copy-of select="." />
</xsl:template>
If you have lots of HTML that you want to copy straight over from your XML
source to your HTML output, the cleanest approach is to define an 'html'
namespace and mark all the HTML elements as belonging to this namespace:
<page xmlns:html="http://www.w3.org/1999/xhtml">
We offer the cheapest air fares to Bombay.
<html:img src="global.gif" alt="Go around the world" />
</page>
To copy these HTML elements whenever you come across them within your page,
have a template that matches them (html:* - any element in the html
namespace) and copies them:
<xsl:template match="html:*">
<xsl:copy-of select="." />
</xsl:template>
|