Re: [xsl] Passing javascript variable as parameter to XSL

Subject: Re: [xsl] Passing javascript variable as parameter to XSL
From: "Dean Fiala" <dfiala@xxxxxxxxx>
Date: Wed, 26 Apr 2006 06:25:10 -0400
You can test if the parameter is getting passed by displaying it in
your output...

<xsl:template match="rwsdomesticportfolio/partner">
<xsl:value-of select="$partnername" />

but I believe your problem is that you don't need the single quotes here
<xsl:if test="varpage='$partnername'">

this should work
<xsl:if test="varpage=$partnername">

On 4/26/06, Spencer Easterbrook <s.easterbrook@xxxxxxxxxxxx> wrote:
> Hi all,
>
> Firstly, I confess ... I'm relatively new to XSL, but what I have been
> able to learn and use has been thanks largely to posts on lists and
> forums, and so I'm sure that this is the best place to ask my question.
> Forgive me for what is, I'm sure, a reasonably simple problem!
>
> Problem:  I have an HTML page called 'test.html'.  This page is only a
> test page to sort out this functionality, but what it should do is
> return a table consisting of elements from the selected record in the
> XML file.
>
> The code for the html file is:
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <html><HEAD></HEAD><body>
> <div id="Ranges" class="varRangeDisplay">
> <script language="javascript">
>    var URL1 = unescape(location.href) // get current URL in plain ASCII
>    var xstart1 = URL1.lastIndexOf("/") + 1
>    var xend1 = URL1.length
>    var varpage = URL1.substring(xstart1,xend1);
>
>  //  varpage now contains the page name.
>  //document.write(varpage);
>     var xslt = new ActiveXObject("Msxml2.XSLTemplate");
>     var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
>     var xslProc;
>     xslDoc.async = false;
>     xslDoc.resolveExternals = false;
>     xslDoc.load("../scripts/hereName.xsl");
>     xslt.stylesheet = xslDoc;
>     var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
>     xmlDoc.async = false;
>     xmlDoc.resolveExternals = false;
>     xmlDoc.load("../Content/domestic_portfolio.xml");
>     xslProc = xslt.createProcessor();
>     xslProc.input = xmlDoc;
>     xslProc.addParameter("partnername", varpage);
>     xslProc.transform();
>     document.write(xslProc.output);
> </script>
> </div></body>
>
> Note:  The javascript variable I am trying to pass is called "varpage"
> and evaluates (successfully) to the name of the html document (in this
> case 'test.html').  I am using addParameter to add a parameter called
> "partnername" to the xslProc transformation to use as a XSL:PARAM in the
> XSL file.  But it's not working ... the XSL I have done is:
> <?xml version="1.0" encoding="iso-8859-1"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
> <xsl:output method="html" encoding="iso-8859-1"/>
> <xsl:param name="partnername"></xsl:param>
> <xsl:template match="rwsdomesticportfolio/partner">
> <xsl:if test="varpage='$partnername'">
> <table border="1"><tr>
> <xsl:for-each select="portfolio/range">
>  <td valign="top">
>   <xsl:value-of select="rangename"/>
> <table border="1">
>   <xsl:for-each select="varietal">
>   <tr><td align="center">
>  <xsl:element name="IMG">
>   <xsl:attribute name="SRC">
>   <xsl:value-of select="bottleshot"/></xsl:attribute>
>   <xsl:attribute name="BORDER">0</xsl:attribute>
>  </xsl:element><br />
>  <xsl:value-of select="varietalname"/>
>  </td></tr>
>     </xsl:for-each>
> </table><br />
> </td></xsl:for-each>
> </tr></table>
> </xsl:if>
> </xsl:template>
> </xsl:stylesheet>
>
> The XML file I'm using is available at:
> http://web.aanet.com.au/HairyBeast/rws/Content/domestic_portfolio.xml
> ... it's fairly long so I didn't put it in here.  The important piece is
> an element called <varpage> within each <partner> record which I'm using
> to test against in the XSL.
>
> Basically, I'm getting nothing returned, which means that either the
> xsl:if test is failing or the parameter is not being passed.
>
> What's really bugging me is that if I hardcode the page name in the XSL,
> then it works fine (e.g. <xsl:if test="varpage='test.html">, or any
> other valid varpage value in the XML)!!
>
> I will be eternally grateful if anyone is able to spot the problem and
> let me know how to fix it!!
>
> Regards, Spencer
>
>


--
Dean Fiala
Very Practical Software, Inc
Now with Blogging...
http://www.vpsw.com/blogbaby
Microsoft MVP

Current Thread