Setting XSL Parameters from Javascript in AJAXSLT
Posted: October 23rd, 2005 Tags: Howto, Javascript, XSLGoogle’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.
January 14th, 2006 at 8:59 am
please, do you have an example of this online ?
Thanks for your 2 posts on AJAXSLT.
January 14th, 2006 at 10:46 am
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?
January 14th, 2006 at 12:51 pm
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
January 15th, 2006 at 10:09 pm
var theValue = document.getElementByID(”mySelect”).value;
myContext.setVariable(”theValue”, theValue);
January 16th, 2006 at 3:07 am
what do you call “myRootElement” in
“xsltProcessContext(myContext, myXsl, myRootElement);”?
January 16th, 2006 at 10:05 am
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).
January 17th, 2006 at 7:43 am
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”
June 28th, 2006 at 8:31 am
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…
September 26th, 2006 at 8:47 am
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
September 26th, 2006 at 1:51 pm
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?
January 24th, 2007 at 9:48 am
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