このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

XSLTProcessor: importStylesheet() メソッド

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

importStylesheet()XSLTProcessor インターフェイスのメソッドで、プロセッサーに XSLT スタイルシートをインポートします。

構文

js
importStylesheet(style)

引数

style

インポートするノード (Node)。これは、 XML 文書(すなわち Document のうち、 doctypename"xml" であるもの)で、 XSLT スタイルシートまたはリテラル結果要素変換を含むもの、または、 <xsl:stylesheet> or <xsl:transform> を表す要素 (Element) です。

返値

なし (undefined)。

importStylesheet() の使用

この例では、 XML データの変換に使用するために、 importStylesheet() が XSLT スタイルシートを XSLTProcessor に読み込むことを示しています。

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

結果

仕様書

Specification
DOM
# dom-xsltprocessor-importstylesheet

ブラウザーの互換性