XSLTProcessor: transformToFragment() ã¡ã½ãã
Baseline
åºãå©ç¨å¯è½
ãã®æ©è½ã¯åºãå®è£ ããã¦ãããå¤ãã®ãã¼ã¸ã§ã³ã®ç«¯æ«ããã©ã¦ã¶ã¼ã§åä½ãã¾ãã2015å¹´7æä»¥éããã¹ã¦ã®ãã©ã¦ã¶ã¼ã§å©ç¨å¯è½ã§ãã
transformToFragment() 㯠XSLTProcessor ã¤ã³ã¿ã¼ãã§ã¤ã¹ã®ã¡ã½ããã§ã XSLTProcessor ã«é¢é£ä»ãããã XSLT ã¹ã¿ã¤ã«ã·ã¼ãã使ç¨ãã¦ãæå®ããã Node ã®ã½ã¼ã¹ã DocumentFragment ã«å¤æãã¾ãã
æ§æ
js
transformToFragment(source, document)
弿°
è¿å¤
DocumentFragment ã§ãã
ä¾
>transformToFragment() ã®ä½¿ç¨
ãã®ä¾ã§ã¯ãXML ãã¼ã¿ã HTML ã«å¤æãã transformToFragment() ã®ä½¿ç¨æ¹æ³ã示ãã¦ãã¾ãã夿å¾ã® HTML ã¯ãææ¸ãã©ã°ã¡ã³ãã¨ã㦠DOM ã«ç´æ¥æ¿å
¥ãããã¨ãã§ãã¾ãã
HTML
html
<div id="result"></div>
JavaScript
js
const xmlString = `
<books>
<book>
<title>Book 1</title>
<author>Author 1</author>
</book>
<book>
<title>Book 2</title>
<author>Author 2</author>
</book>
</books>
`;
const xsltString = `
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<ul>
<xsl:for-each select="books/book">
<li>
<strong><xsl:value-of select="title"/></strong>
by <em><xsl:value-of select="author"/></em>
</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();
xsltProcessor.importStylesheet(xsltDoc);
// Perform the transformation, returning the result as a document fragment
const resultFragment = xsltProcessor.transformToFragment(xmlDoc, document);
// Insert the result into the page
document.getElementById("result").appendChild(resultFragment);
çµæ
仿§æ¸
| 仿§æ¸ |
|---|
| DOM > # dom-xsltprocessor-transformtofragment > |