XSLTProcessor: setParameter() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

The setParameter() method of the XSLTProcessor interface sets the value of a parameter (<xsl:param>) in the stylesheet imported in the processor.

Syntax

js
setParameter(namespaceURI, localName, value)

Parameters

namespaceURI

The namespace associated with the parameter name. A "null" value is treated the same as the empty string ("").

localName

The name of the parameter in the associated namespace.

value

The value of the parameter.

Note: Firefox supports any kind of parameter. Chrome, Edge and Safari only support string parameters.

Return value

None (undefined).

Examples

Using setParameter()

This example demonstrates how to pass parameters from JavaScript to an XSLT stylesheet using setParameter(), allowing for dynamic modification of the transformation output based on these parameters.

HTML

html
<div id="result"></div>

JavaScript

js
const xmlString = `
<items>
  <item>Item 1</item>
  <item>Item 2</item>
  <item>Item 3</item>
</items>
`;

const xsltString = `
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="showItems" select="'yes'"/>
  <xsl:param name="highlightColor" select="'yellow'"/>
  <xsl:template match="/">
    <ul>
      <xsl:if test="$showItems = 'yes'">
        <xsl:for-each select="items/item">
          <li style="background-color: {$highlightColor};">
            <xsl:value-of select="."/>
          </li>
        </xsl:for-each>
      </xsl:if>
    </ul>
  </xsl:template>
</xsl:stylesheet>
`;

const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, "application/xml");
const xsltDoc = parser.parseFromString(xsltString, "application/xml");

const xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsltDoc);

xsltProcessor.setParameter(null, "showItems", "yes");
xsltProcessor.setParameter(null, "highlightColor", "lightblue");

// Perform the transformation from XML to HTML
const resultFragment = xsltProcessor.transformToFragment(xmlDoc, document);

// Display the transformed result in the page
document.getElementById("result").appendChild(resultFragment);

Result

Specifications

Specification
DOM
# dom-xsltprocessor-setparameter

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
setParameter
Chrome – Full support
Chrome 1 (Release date: 2008-12-11)
footnote
footnote Chrome only supports string values.
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote
footnote Edge only supports string values.
Firefox – Full support
Firefox 1 (Release date: 2004-11-09)
footnote Full support
Opera – Full support
Opera 12.1 (Release date: 2012-11-20)
footnote
footnote Opera only supports string values.
Safari – Full support
Safari 3.1 (Release date: 2008-03-18)
footnote
footnote Safari only supports string values.
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote
footnote Chrome Android only supports string values.
Firefox for Android – Full support
Firefox for Android 4 (Release date: 2011-03-29)
footnote Full support
Opera Android – Full support
Opera Android 12.1 (Release date: 2012-10-09)
footnote
footnote Opera only supports string values.
Safari on iOS – Full support
Safari on iOS 2 (Release date: 2008-07-11)
footnote
footnote Safari on iOS only supports string values.
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote
footnote Samsung Internet only supports string values.
WebView Android – Full support
WebView Android 3 (Release date: 2011-02-22)
footnote
footnote WebView only supports string values.
WebView on iOS – Full support
WebView on iOS 2 (Release date: 2008-07-11)
footnote
footnote WebView on iOS only supports string values.

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
See implementation notes.

See also