XSLTProcessor: метод importStylesheet()
Базовая линия Широко доступна
Эта функция хорошо зарекомендовала себя и работает на многих устройствах и версиях браузеров. Она доступна в браузерах с июля 2015 года.
Метод importStylesheet() интерфейса XSLTProcessor импортирует XSLT-шаблон для процессора.
Синтаксис
importStylesheet(style)
Параметры
Возвращаемое значение
None (undefined).
Примеры
Использование importStylesheet()
Этот пример показывает, как importStylesheet() загружает XSLT-шаблон в XSLTProcessor для использования при преобразовании XML-данных.
HTML
<div id="result"></div>
JavaScript
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:template match="/">
<ul>
<xsl:for-each select="items/item">
<li><xsl:value-of select="."/></li>
</xsl:for-each>
</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();
// Import the XSLT stylesheet into the XSLTProcessor
xsltProcessor.importStylesheet(xsltDoc);
// 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);
Результат
Спецификации
| Спецификация |
|---|
| DOM # dom-xsltprocessor-importstylesheet |
Совместимость с браузерами
| Рабочие столы | Мобильные устройства | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox для Android | Opera Android | Safari на IOS | Samsung Internet | WebView Android | |
importStylesheet |
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/importStylesheet