Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 4th, 2004, 04:32 PM
Registered User
 
Join Date: Mar 2004
Location: Redditch, , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
Reply With Quote
  #2 (permalink)  
Old March 5th, 2004, 05:11 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
Reply With Quote
  #3 (permalink)  
Old March 5th, 2004, 05:39 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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
Reply With Quote
  #4 (permalink)  
Old March 5th, 2004, 06:38 AM
Registered User
 
Join Date: Mar 2004
Location: Redditch, , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #5 (permalink)  
Old March 5th, 2004, 07:01 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

how are you passing in the param?
Reply With Quote
  #6 (permalink)  
Old March 5th, 2004, 01:02 PM
Registered User
 
Join Date: Mar 2004
Location: Redditch, , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #7 (permalink)  
Old March 5th, 2004, 04:56 PM
Registered User
 
Join Date: Mar 2004
Location: Redditch, , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #8 (permalink)  
Old March 6th, 2004, 05:36 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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
Reply With Quote
  #9 (permalink)  
Old March 6th, 2004, 07:15 AM
Registered User
 
Join Date: Mar 2004
Location: Redditch, , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #10 (permalink)  
Old March 6th, 2004, 01:40 PM
Registered User
 
Join Date: Mar 2004
Location: Redditch, , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parameter object malfunction - out parameter dash dev C# 2005 6 December 4th, 2007 12:58 PM
Testing a String gabster XSLT 2 August 15th, 2007 12:13 PM
Encryption Testing dkb Javascript 1 January 5th, 2007 06:37 AM
Testing and debugging Brettvan1 VB.NET 8 February 16th, 2006 07:22 AM
testing mark C# 0 June 4th, 2003 08:37 PM



All times are GMT -4. The time now is 10:12 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.