 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

March 4th, 2004, 04:32 PM
|
|
Registered User
|
|
Join Date: Mar 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Parameter testing
Hi Folks,
I am a newbie to this xsl stuff so please excuse me if i am asking a dumb question.
I am trying to pass a parameter to my xsl which will be tested against the value of an attribute within an element to determine whether that element should be added to a table in my html output. So far I have a lump on my forehead where it keeps hitting the screen.
Here is how I am trying to do the xsl:
<xsl:param name="band" select="none"/>
<xsl:template match="/">
<table>
<xsl:for-each select="root/songs">
<xsl:if test = "contains(@Artist,$band)">
<tr><td><xsl:value-of select="@Title"/></td></tr>
</xsl:if>
</xsl:for-each>
</table>
</xsl:template>
here is some applicable xml:
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml:stylesheet type="text/xsl" href="songs.xsl"?>
<root>
<songs Title="Sail Away" Artist="David Gray"/>
<songs Title="Ladies Nite In Buffalo" Artist="David Lee Roth"/>
<songs Title="Bad Racket" Artist="Greg Howe" />
</root>
iexplore URL appears with e.g. blah_de_blah/songs.html?band=David+Gray
However the contains() function is treating the parameter 'band' as an empty string therefore every <songs> element is added to the table.
Please please tell me what I am doing wrong.
Many thanks
Penry.
Rinky Dinky Doo
|
|

March 5th, 2004, 05:11 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
If you want the param $band to contain the literal string "none" then you must specify it in quotes in the select attribute. For example:
Code:
<xsl:param name="band" select="none"/> <- none is treated as a node name to match
<xsl:param name="band" select="'none'"/> <- none is treated as the string "none"
hth
Phil
|
|

March 5th, 2004, 05:39 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Don't feel too bad about this mistake, if you go to this page, http://www.w3.org/TR/xslt, and search for select="en" you'll see the writers' of the specification fell into the same trap, it should be select="'en'". :)
--
Joe
|
|

March 5th, 2004, 06:38 AM
|
|
Registered User
|
|
Join Date: Mar 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Again,
Thanks for this. This corrected the default value for param band.
However the incoming param is still not being treated as a string literal. Any clues?
Cheers
Penry
Quote:
quote:Originally posted by pgtips
If you want the param $band to contain the literal string "none" then you must specify it in quotes in the select attribute. For example:
Code:
<xsl:param name="band" select="none"/> <- none is treated as a node name to match
<xsl:param name="band" select="'none'"/> <- none is treated as the string "none"
hth
Phil
|
Rinky Dinky Doo
|
|

March 5th, 2004, 07:01 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
how are you passing in the param?
|
|

March 5th, 2004, 01:02 PM
|
|
Registered User
|
|
Join Date: Mar 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi PG,
In my html page have the following line (followed by a transform):
xsl.addParameter("band", String(band));
This produces the following URL bar result:
e.g. iexplore URL appears with e.g. blah_de_blah/songs.html?band=Toploader
Cheers
Penry
Quote:
quote:Originally posted by pgtips
how are you passing in the param?
|
Rinky Dinky Doo
|
|

March 5th, 2004, 04:56 PM
|
|
Registered User
|
|
Join Date: Mar 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Again,
more info:
I added the following line to my xsl for debug purposes
<tr><td><xsl:value-of select="$band"/></td></tr>
Which always produces a cell in my html with the text 'none' in it no matter what value for the band parameter is being passed to the xsl from my html (javascript). So here is some more info to help complete the picture.
There are 3 files; songs.html, songs.xml and songs.xsl
In songs.html I have the following javascript function that gets called everytime a certain form text field edited.
function doSearch(band)
{
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("xml/downloads.xml")
// Load XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("xml/downloads.xsl")
xsl.addParameter("band", band);
// Transform
document.write(xml.transformNode(xsl))
}
I am doing this all on my c drive at the moment on the assumption that it is all client side stuff.
Upon editing the text field and hitting return, the following appears in the url bar of the broswer (IE6)
blahblahblah/songs.html?band=Toploader
I hope this can shed some light on my error.
Cheers
Penry
Rinky Dinky Doo
|
|

March 6th, 2004, 05:36 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Can you show how you are creating the XslTemplate object that you need to use addParameter? You cannot use this method directly on a stylesheet.
--
Joe
|
|

March 6th, 2004, 07:15 AM
|
|
Registered User
|
|
Join Date: Mar 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Joe,
It is looking very much like my javascript is the source of the fault. I am new to that too. Debugging this stuff is hard work. Any tips?
The code in my previous reply is borrowed from an online tutorial, with my erroneous addParameter addition (it causes the function to fail outright). I have since found more code that supposedly does what I want.
my html body contains the following:
<script language="JavaScript">
var xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = false;
xml.load("xml/songs.xml");
var xsl = new ActiveXObject("Microsoft.XMLDOM");
xsl.async = false;
xsl.load("xml/songs.xsl");
document.write(xml.transformNode(xsl));
</script>
Which transforms my xml quite happily with the default parameter value specified in the xsl.
Subsequently I call the folowing function when my 'search' form field is updated. (Completely plagiarised from a tutorial site.)
function doSearch(band){
try
{
document.writeln(band);
var s = new ActiveXObject("MSXML2.FreeThreadedDOMDocument");
var x = document.XMLDocument;
if (x == null)
{
x = navigator.XMLDocument;
s.loadXML(navigator.XSLDocument.xml);
}
else
{
s.loadXML(document.XSLDocument.xml);
}
var tem = new ActiveXObject("MSXML2.XSLTemplate");
tem.stylesheet = s;
var proc = tem.createProcessor();
proc.addParameter("band", band);
proc.input = x;
proc.transform();
var str = proc.output;
var newDoc = document.open("text/html", "replace");
newDoc.write(str);
navigator.XMLDocument = x;
navigator.XSLDocument = s;
newDoc.close();
}
catch(exception){}
}
The first writeln outputs the band name to the browser but nothing else seems to take place.
Cheers Penry
Rinky Dinky Doo
|
|

March 6th, 2004, 01:40 PM
|
|
Registered User
|
|
Join Date: Mar 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Folks,
I solved my parameter passing stuff with the following javascript function:
function doSearch(form)
{
var xslt = new ActiveXObject("Msxml2.XSLTemplate");
var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
var xslProc;
xslDoc.async = false;
xslDoc.resolveExternals = false;
xslDoc.load("xml/songs.xsl");
xslt.stylesheet = xslDoc;
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
xmlDoc.async = false;
xmlDoc.resolveExternals = false;
xmlDoc.load("xml/songs.xml");
xslProc = xslt.createProcessor();
xslProc.input = xmlDoc;
xslProc.addParameter("band", form.band.value);
xslProc.transform();
var str = '<link href="style.css" type="text/css" rel="stylesheet">'
str = str + '<body class="body" bgcolor="#000000" link="#FFFF00" vlink="#FFff00">'
str = str + xslProc.output;
str = str + '</body>';
newWindow=window.open('','','toolbar=no,scrollbars =yes,width=800,height=600');
var newDoc = newWindow.document.open("text/html", "replace");
newDoc.write(str);
}
Cheers
Penry
Rinky Dinky Doo
|
|
 |