faqts : Computers : Programming : Languages : JavaScript : XML

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

19 of 27 people (70%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

How can I use a JS variable in an XSL querie?

Feb 26th, 2002 10:44
Nathan Morehart, Catalin Amza,


The pseudo-code example below comes from me calling a JS function when 
the page is built to apply a stylesheet to an XML doc. The first thing 
I used this for was data-binding to a table, and sorting by columns. 
So, depending on which column was clicked, sort by that column.
Make sure you have an XML doc loaded.
var myXML = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
myXML.async = false;
myXML.load(xmlDoc.xml);
Now, create an intermediary function.
function passValues() {
  var xsl = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
  xsl.async = false;
  xsl.load("./_xslt/xslMargins_CallSheets.xsl");
  var template = new ActiveXObject("Msxml2.XSLTemplate");
  template.stylesheet = xsl;
  var myProc = template.createProcessor();
  myProc.input = xml; //xml is your xml doc (ie myXML.xml)
  myProc.addParameter('fPara', value1); //these are "xsl variables"
  myProc.addParameter('sPara', value2);
  myProc.addParameter('tPara', value3);
  myProc.transform;
  yourDiv.innerHTML = myProc.output; // provided you are outputting 
HTML from the XSL
}
XSL --
...
<xsl:param name="fPara"/>
<xsl:param name="sPara"/>
<xsl:param name="tPara"/>
You can now conditionally execute XSL depending on the JS values passed 
in.
ie: <xsl:if test="$fPara = 'value1'">
      <xsl:choose>
        <xsl:when test="(@SecondValue = $sPara) and (@ThirdValue = 
$tPara)">
        ...
        ...
I do all of this transformation, then I just spit it back from XSL to 
the page as HTML and set a div's innerHTML to the output.
Hope this helped!