XSLTProcessor: importStylesheet() 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.

Phương thức importStylesheet() của giao diện XSLTProcessor nhập một biểu định kiểu XSLT vào bộ xử lý.

Cú pháp

js
importStylesheet(style)

Tham số

style

Node cần nhập. Có thể là một tài liệu XML (tức là Document với doctypename"xml") chứa biểu định kiểu XSLT hoặc phép biến đổi phần tử kết quả literal, hoặc một Element đại diện cho <xsl:stylesheet> hoặc <xsl:transform>.

Giá trị trả về

Không có (undefined).

Ví dụ

Sử dụng importStylesheet()

Ví dụ này cho thấy cách importStylesheet() tải một biểu định kiểu XSLT vào XSLTProcessor để sử dụng trong việc biến đổi dữ liệu XML.

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: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);

Kết quả

Thông số kỹ thuật

Specification
DOM
# dom-xsltprocessor-importstylesheet

Tương thích trình duyệt