mozilla

Revision 192804 of Setting Parameters

  • Revision slug: The_XSLT//JavaScript_Interface_in_Gecko/Setting_Parameters
  • Revision title: Setting Parameters
  • Revision id: 192804
  • Created:
  • Creator: JdeValk
  • Is current revision? No
  • Comment /* Setting Parameters */

Revision Content

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: setParameter, getParameter and 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");
else
  xsltProcessor.setParameter(null, "myOrder", "descending");

Revision Source

<p>
</p>
<h2 name="Setting_Parameters"> Setting Parameters </h2>
<p>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.
</p><p>XSLT provides the <code>xsl:param</code> element, which is a child of the <code>xsl:stylesheet</code> element. <code>XSLTProcessor()</code> provides three JavaScript methods to interact with these parameters: <code>setParameter</code>, <code>getParameter</code> and <code>removeParameter</code>. They all take as the first argument the namespace URI of the <code>xsl:param</code> (Usually the param will fall in the default namespace, so passing in "null" will suffice.) The local name of the <code>xsl:param</code> is the second argument. setParameter requires a third argument - namely the value to which the parameter will be set.
</p><p><small><b>Figure 7 : Parameters</b></small>
</p>
<pre>XSLT:
&lt;xsl:param name="myOrder" /&gt;

JavaScript:

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

if (sortVal == "" || sortVal == "descending")
  xsltProcessor.setParameter(null, "myOrder", "ascending");
else
  xsltProcessor.setParameter(null, "myOrder", "descending");
</pre>
Revert to this revision