XSLTProcessor: removeParameter() 方法
XSLTProcessor 介面的 removeParameter() 方法用於從處理器中匯入的樣式表中移除引數(<xsl:param>)及其值。
語法
js
removeParameter(namespaceURI, localName)
引數
namespaceURI-
與引數名稱關聯的名稱空間。 "null" 值被視為與空字串 (
"") 相同。 localName-
關聯名稱空間中的引數名稱。
返回值
無(undefined)。
示例
使用 removeParameter()
首先,將 showItems 引數設定為 "yes",這允許列表項在輸出中顯示。
之後,使用 removeParameter() 移除 showItems 引數,然後再次執行轉換,結果將不會顯示任何專案。
HTML
html
<div id="result"></div>
JavaScript
js
const xmlString = `
<items>
<item>Item 1</item>
<item>Item 2</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 'showItems' to 'no' and perform the first transformation
xsltProcessor.setParameter(null, "showItems", "no");
const resultContainer = document.getElementById("result");
let resultFragment = xsltProcessor.transformToFragment(xmlDoc, document);
resultContainer.appendChild(resultFragment);
// Add a horizontal rule to separate the results
resultContainer.appendChild(document.createElement("hr"));
// Remove the 'showItems' parameter, reverting it to the default value ('yes')
xsltProcessor.removeParameter(null, "showItems");
resultFragment = xsltProcessor.transformToFragment(xmlDoc, document);
resultContainer.appendChild(resultFragment);
結果
規範
| 規範 |
|---|
| DOM # dom-xsltprocessor-removeparameter |
瀏覽器相容性
載入中…