MDN’s new design is in Beta! A sneak peek:

Setting Parameters

While running transformations using precoded .xsl and .xml files is quite useful, configuring the .xsl file from JavaScript may be even more useful. For example, JavaScript and XSLT could be used to sort XML data and then display it. The sorting would have to alternate between ascending and descending sorting.

XSLT provides the xsl:param element, which is a child of the xsl:stylesheet element. XSLTProcessor provides three JavaScript methods to interact with these parameters: XSLTProcessor.setParameter(), XSLTProcessor.getParameter() and XSLTProcessor.removeParameter(). They all take as the first argument the namespace URI of the xsl:param (Usually the param will fall in the default namespace, so passing in null will suffice.) The local name of the xsl:param is the second argument. setParameter requires a third argument - namely the value to which the parameter will be set.

Figure 7 : Parameters

/* XSLT:

<xsl:param name="myOrder" />


// JavaScript:

var sortVal = xsltProcessor.getParameter(null, "myOrder");

if (sortVal == "" || sortVal == "descending")
  xsltProcessor.setParameter(null, "myOrder", "ascending");
  xsltProcessor.setParameter(null, "myOrder", "descending");

Document Tags and Contributors

 Contributors to this page: teoli, cvrebert, Brettz9, trevorh, Fredchat, Sebuls, JdeValk
 Last updated by: teoli,