XSLTProcessor: метод clearParameters()
Базовая поддержка Широко доступна
Эта функция хорошо отработана и работает на многих устройствах и версиях браузеров. Она доступна в браузерах с июля 2015 года.
Метод clearParameters() интерфейса XSLTProcessor удаляет все параметры (<xsl:param>) и их значения из стилей, импортированных в процессор. Тогда XSLTProcessor будет использовать значения по умолчанию, указанные в XSLT-стиле.
Синтаксис
clearParameters()
Параметры
Нет.
Возвращаемое значение
Нет (undefined).
Примеры
Использование clearParameters()
Этот пример показывает, как clearParameters() можно использовать для сброса всех параметров до значений по умолчанию, указанных в XSLT-стиле.
HTML
<div id="result"></div>
JavaScript
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);
Результат
Спецификации
| Спецификация |
|---|
| DOM # dom-xsltprocessor-clearparameters |
Совместимость с браузерами
| Рабочие столы | Мобильные устройства | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox для Android | Opera Android | Safari на iOS | Samsung Internet | WebView Android | |
clearParameters |
1 | 12 | 1 | ≤12.1 | 3.1 | 18 | 4 | ≤12.1 | 2 | 1.0 | 3 |
См. также
© 2005–2024 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/XSLTProcessor/clearParameters