Re: [xsl] Using javascript to pass a parameter to XSL

Subject: Re: [xsl] Using javascript to pass a parameter to XSL
From: "Joe Fawcett" <joefawcett@xxxxxxxxxxx>
Date: Thu, 17 Mar 2005 18:43:38 -0000
----- Original Message ----- From: "Maria Amuchastegui" <mamuchastegui@xxxxxxxxxxx>
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Thursday, March 17, 2005 5:59 PM
Subject: RE: [xsl] Using javascript to pass a parameter to XSL

<xsl:sort select="$sortKey" order="{$sortOrder}"/>

Now the transformation is happening, but the data is still not sorting, and the value assigned to the variable in the javascript function call is still not getting passed to the stylesheet.


-----Original Message-----
From: Pieter Reint Siegers Kort [mailto:pieter.siegers@xxxxxxxxxxx] Sent: Thursday, March 17, 2005 12:42 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: RE: [xsl] Using javascript to pass a parameter to XSL

Hi Maria,

I've almost got it but it doesn't quite work.

What exactly doesn't work? Let's see...

To start, first error I get is from MSXML 3.0 that complains:

"The value of the "order" attribute may only be "ascending" or "descending""

See Michael Kay's 2nd Edition of XSLT, page 296.

I suggest you want the parameter "order" to contain "ascending" or
"descending", right?


<xsl:sort select="$sortKey" order="{$sortOrder}"/>

That should do the trick.


-----Original Message-----
From: Maria Amuchastegui [mailto:mamuchastegui@xxxxxxxxxxx]
Sent: Jueves, 17 de Marzo de 2005 11:12 a.m.
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] Using javascript to pass a parameter to XSL

I have an HTML file with an embedded XML island which calls an external
stylesheet. I want to use Javascript to pass a parameter to the stylesheet
using the addParameter method. I've almost got it but it doesn't quite work.

Here is the HTML:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html>
<head> <script language="JavaScript"> function sortXML(column,order) { var
xslt = new ActiveXObject("Msxml2.XSLTemplate.3.0");
var xsldoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.3.0");
var xslproc;
xsldoc.async = false;
xsldoc.load('Sort.xsl'); xslt.stylesheet = xsldoc;
var xmldoc = document.getElementById('ChargeableMessages');
xslproc = xslt.createProcessor();
xslproc.input = xmldoc;
xslproc.addParameter("sortKey", column); xslproc.addParameter("sortOrder",
order); xslproc.transform();
container.innerHTML = xmldoc.transformNode(xsldoc); }
</script> </head>
<body onload="sortXML('id','ascending');">

<div id="container"></div>

<xml id="ChargeableMessages" style="display:none;">
<location>Quebec QC</location>
<number>418 683 1234</number>
<location>Montreal QC</location>
<number>514 485 6611</number>
<location>Winnipeg MB</location>
<number>204 475 4565</number>


And here is the stylesheet:

<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet
<xsl:param name="sortKey"/>
<xsl:param name="sortOrder"/>
<xsl:template match="/">
<xsl:apply-templates select="chargeablemessages"/>
<xsl:template match="chargeablemessages">
<h3>Long distance calls - Click on any column heading to
<table border="1">
<th><a href="#"
<th><a href="#"
<th><a href="#"
<th><a href="#"
<th><a href="#"
<th><a href="#"

<xsl:apply-templates select="//message">
<xsl:sort select="$sortKey" order="$sortOrder"/>

<xsl:template match="//message">
<td><xsl:value-of select="id"/></td>
<td><xsl:value-of select="date"/></td>
<td><xsl:value-of select="location"/></td>
<td><xsl:value-of select="number"/></td>
<td><xsl:value-of select="duration"/></td>
<td><xsl:value-of select="charges"/></td>

<xsl:sort select="$sortKey" order="{$sortOrder}"/>
select expects a node-set, you are just giving it a string.
To do this using addParameter you can change it to:
<xsl:sort select="*[name() = $sortKey]" order="{$sortOrder}"/>


Current Thread