XSLTProcessor: clearParameters() Methode
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since Juli 2015.
Die clearParameters()
-Methode der XSLTProcessor
Schnittstelle entfernt alle Parameter (<xsl:param>
) und deren Werte aus dem Stylesheet, das im Prozessor importiert wurde. Der XSLTProcessor
verwendet dann die Standardwerte, die im XSLT-Stylesheet angegeben sind.
Syntax
js
clearParameters()
Parameter
Keine.
Rückgabewert
Keiner (undefined
).
Beispiele
>Verwendung von clearParameters()
Dieses Beispiel zeigt, wie clearParameters()
verwendet werden kann, um alle Parameter auf ihre Standardwerte zurückzusetzen, wie im XSLT-Stylesheet angegeben.
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:template match="/">
<!-- If showItems is 'yes', display the list of items -->
<xsl:if test="$showItems = 'yes'">
<ul>
<xsl:for-each select="items/item">
<li><xsl:value-of select="."/></li>
</xsl:for-each>
</ul>
</xsl:if>
<!-- If showItems is 'no', display a message -->
<xsl:if test="$showItems = 'no'">
<div>No content to show</div>
</xsl:if>
</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);
// Set the 'showItems' parameter to 'no' and perform the first transformation
xsltProcessor.setParameter(null, "showItems", "no");
let resultFragment = xsltProcessor.transformToFragment(xmlDoc, document);
document.getElementById("result").appendChild(resultFragment);
// Add a horizontal rule to separate the results
document.getElementById("result").appendChild(document.createElement("hr"));
// Clear all parameters, resetting 'showItems' to its default value ('yes')
xsltProcessor.clearParameters();
resultFragment = xsltProcessor.transformToFragment(xmlDoc, document);
document.getElementById("result").appendChild(resultFragment);
Ergebnis
Spezifikationen
Specification |
---|
DOM> # dom-xsltprocessor-clearparameters> |
Browser-Kompatibilität
Loading…