Erik A. Hanson's Weblog

Setting XSL Parameters from Javascript in AJAXSLT

Posted: October 23rd, 2005    Tags: Howto, Javascript, XSL

Google’s AJAXSLT library is very nice, but it is lacking in documentation and sometimes hard to figure out.

Recently, I was trying to pass some info from my Javascript into my XSL stylesheet. Here’s how I finally did it:

(Quick note: in real life, I don’t name variables "myThis" and "myThat"; I’m just doing it here to make it clear what is my code and what is part of XSL, AJAXSLT, Javascript, etc.)

The XSL

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="myParam" select="'myDefault'"/>
  <xsl:template match="/">
    myParam: <xsl:value-of select="$myParam"/>
  </xsl:template>
</xsl:stylesheet>

The Javascript

var myContext = new ExprContext(myXml);
myContext.setVariable("myParam",
    new StringValue("the value for myParam"));
xsltProcessContext(myContext, myXsl, myRootElement);

The Trick

So the big trick, which I figured out after reading lots of the AJAXSLT code, was to set the parameter to a StringValue, rather than a regular Javascript String. StringValue is part of AJAXSLT. Update: Will, in the comments below, mentions that there are four total types: StringValue, NumberValue, BooleanValue and NodeSetValue.

Troubleshooting

Don’t forget to declare your parameter with <xsl:param.../> before using it with <xsl:value-of select="$..."/>, otherwise Ajaxslt will blow up in the middle of rendering.



11 Responses to “Setting XSL Parameters from Javascript in AJAXSLT”

  1. Will Says:

    please, do you have an example of this online ?
    Thanks for your 2 posts on AJAXSLT.

  2. Erik Says:

    The site that I used this code in isn’t live yet. But is the example code in this post not enough? Do you have a specific question I might be able to answer?

  3. Will Says:

    oupps oupps oupps
    I have a form whith a select and a submit in a xhtml page.
    each options of the select have a value (1, 2, 3, 4, 5…)
    the submit do two XHR (one for the xml and a second for xsl).
    how could you pass the value of the selected option in xhtml to the xsl.
    like this :
    xsl:\template match=”/”
    xsl:apply-templates select=”page/message/things/thing[$theValue]“/
    /xsl:template

    xsl:template match=”page/message/things/thing[$theValue]”

    /xsl:template

  4. Erik Says:

    var theValue = document.getElementByID(”mySelect”).value;
    myContext.setVariable(”theValue”, theValue);

  5. Will Says:

    what do you call “myRootElement” in
    “xsltProcessContext(myContext, myXsl, myRootElement);”?

  6. Erik Says:

    It’s the node that you want to put the result of the XML+XSL rendering into. See http://www.eahanson.com/weblog/?p=4 for more info (I called it “myNode” in that one).

  7. Will Says:

    It works!!!!
    But in my case it was :
    myContext.setVariable(”theParam”,new NumberValue(theValue));
    You know what, Erik? you are fantastic.

    in xpath.js :
    “The four types are:
    StringValue
    NumberValue
    BooleanValue
    NodeSetValue”

  8. Andrew Bruce Says:

    After some fiddling, and a read of some api-like documentation I found at http://mocha.ch/OpenMocha/Handbook/Combined%20Browser%20Reference/ , I found that getting the result of a transformation from a xsltProcessContext call (rather than a straight xsltProcess call, which requires XSLT in a DOM node), can be done like so:

    var node = document.createElement(’output’);
    xsltProcessContext(context, xsl, node);
    var output = xmlText(node);

    this is seriously tricky without documentation, guys…

  9. Jeremy Patterson Says:

    Eric,

    Thanks for the tutorial, I need a bit of assistance though. I am trying to pass values into an xml page through the url. I defaulted a parameter strParam = ‘None’. When I click a link in the html, I want it to load a parameter, which will make the page dynamic based on the conditions I set in the xslt.

    http://www.webpage.com/myXML.xml?strParam=SomeValue

    would you have some code on how to perform this. I tried Sarissa, but couldn’t figure it out. I think your implementation might work, just need a little more detail. I have very limited xslt background and none in javascript. I am using this format because I have limited resources and I need the site to be dynamic. I know the xslt works because I can compile it against saxon and the logic will work.

    You are basically doing the same thing here:
    http://www.eahanson.com/weblog/?p=4

    I’m probably missing something that was already explained. Would you have an example javascript function, that sets the parameter? How would I call the javascript to trigger the parameter change, when the page reloads?

    Thank you for your assistance - Jeremy

  10. Erik Says:

    Hi Jeremy,

    I’m sorry for being so dense but I can’t quite figure out what you’re asking. Can you perhaps try re-stating your question or re-explaining what you’re trying to accomplish?

  11. Matt Says:

    Hi Erik -

    I see you have been working with ajaxslt for quite a while. That’s good, because I’m just starting (although I’m not a newbie coder).

    I am trying to pass a value into my xsl via javascript, as you have shown above, but I can’t seem to get it to work. Please take a look at what I have and let me know if you can spot the error.

    Any assistance you (or anyone else reading this) can offer, would be very much appreciated.

    Thanks.
    - Matt

    javascript:
    function transform(strXML, strXSL) {
    // strXML & strXSL are populated via ajax calls to the files,
    // so they are just text.

    var myXSL = xmlParse(strXSL);

    // putHere is defined in the body as
    //
    var myNode = document.getElementById(’putHere’);

    var myContext = new ExprContext(strXML);
    myContext.setVariable(”CURRENT_PAGE”, new StringValue(”TWO”));

    xsltProcessContext(myContext, strXSL, myNode);
    }

    xsl content:

    1

    CURRENT_PAGE:

    As a side note, the following javascript works, but I can’t figure out how to pass a param into the xsl.
    function transform2(strXML, strXSL) {
    var xml = xmlParse(strXML);
    var xslt = xmlParse(strXSL);
    var html = xsltProcess(xml, xslt);
    document.getElementById(’putHere’).innerHTML = html;
    }

    Within my firefox error console I see the following error:
    “template.nodeName has no properties”

    This blog seems to be about a year old, is there an updated way to do this now?

    Thanks again.
    - Matt

Leave a Reply

About Me

I'm a software developer and consultant in San Francisco. I mostly develop web apps in Javascript, Java and Rails. There's a little more about me on my LinkedIn profile.

Recent Projects

wshlst.com: a group-oriented wish list website.

BoardsConsult​/​RotationTracker: a site to help medical residents prepare for the U.S. Medical Licensing Exam, and a related site to help medical schools evaluate and track the progress of the residents in their programs.

Jobs

I moderate a few completely free jobs mailing lists with RSS feeds and zero spam: XP jobs, Rails jobs and Mac development jobs. Also, my pals at Pivotal Labs have agile Rails and Java job openings.

Plug: Web Hosting

Looking for cheap shared web hosting and/or domain registration? I've been using DreamHost for years and I've been very happy with them. They offer tons of disk space and bandwidth, one-click installation for apps like WordPress and Gallery, unlimited domains, thousands of email accounts, private domain registration, and more. They are also a nice bunch of folks, not some big dumb company.