XSTLProcessor 绕过 <tr> 和 <td> 标签
XSTLProcessor bypass <tr> and <td> tags
我在客户端中有一个 XML 和 XSL 文件,想将其转换为 HTMLElement 以便我可以附加到我的当前元素
但结果与我的预期相去甚远。除了 <td>
和 <tr>
标记被排除在 HtmlElement 结果
之外,每个看起来都很好
我的XML文件
<?xml version="1.0" encoding="UTF-8"?>
<ns1:Courses xmlns:ns1="www.Course.com">
<ns1:Course xmlns:ns1="www.Course.com">
<ns1:Id>2153</ns1:Id>
<ns1:Name>7 bước làm sao tận hưởng một chuyến du lịch nước ngoài với chi phí thấp</ns1:Name>
<ns1:Author>Hoàng Lê Giang</ns1:Author>
<ns1:AuthorImageURL>
d1nzpkv5wwh1xf.cloudfront.net/320/k-57b67d6f60af25054a055b25/20170928-gianghl_2809/gianghl01.jpg
</ns1:AuthorImageURL>
<ns1:Rating>0.0</ns1:Rating>
<ns1:RatingNumber>0</ns1:RatingNumber>
<ns1:Cost>599000.0</ns1:Cost>
</ns1:Course>
我的 XSL 文件
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" encoding="UTF-8" indent="yes" media-type="text/html"/>
<xsl:template match="/">
<xsl:for-each select="//*[local-name()='Course']">
<tr>
<td class="col1">
<div class="course_name">
<a href="{*[local-name()='SourceURL']}">
<xsl:value-of select="*[local-name()='Name']"/>
</a>
</div>
<div class="course_small_detail">
<img class="img_author_small"
src="{*[local-name()='AuthorImageURL']}"/>
<span class="author_name">
<xsl:value-of select="*[local-name()='Author']"/>
</span>
</div>
</td>
<td class="col2">
<xsl:value-of select="*[local-name()='Cost']"/>
</td>
<td class="col3">
<xsl:value-of select="*[local-name()='Rating']"/>
</td>
</tr>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
我的 javascript 用于转换和附加结果的代码
var xmlDoc = xmlHttp.responseXML;
var xslDoc = xslHttp.responseXML;
xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xslDoc);
//used to add to html document
var resultDocument = xsltProcessor.transformToFragment(xmlDoc, document);
console.log(resultDocument);
document.getElementById("xmlResult").appendChild(resultDocument);
我的 html 结果渲染
<table class="table_result">
<thead>
<tr>
<th>header1 </th>
<th>header2 </th>
<th>header3 </th>
</tr>
</thead>
<tbody id="xmlResult">
</tbody>
</table>
我相信这是因为我用来转换的 XSLTProcessor,因为我试图用我的 IDE 的 XSLT 工具转换它,结果是完全正确的
对于当前版本的 Firefox,Chrome 和 Windows 10 1803 上的 Edge,我可以使用 XSLT return 带有 XHTML [=12 的片段=] 元素然后被插入到托管 HTML 文档和 tbody
:
var domParser = new DOMParser();
var xmlString = `<root>
<item>
<value>foo 1<\/value>
<value>foo 2<\/value>
<value>foo 3<\/value>
<\/item>
<item>
<value>bar 1<\/value>
<value>bar 2<\/value>
<value>bar 3<\/value>
<\/item>
<\/root>`;
var xslString = `<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="item">
<tr>
<xsl:apply-templates/>
<\/tr>
<\/xsl:template>
<xsl:template match="value">
<td>
<xsl:apply-templates/>
<\/td>
<\/xsl:template>
<\/xsl:stylesheet>`;
var xmlInputDoc = domParser.parseFromString(xmlString, 'application/xml');
var xsltDoc = domParser.parseFromString(xslString, 'application/xml');
var xsltProc = new XSLTProcessor();
xsltProc.importStylesheet(xsltDoc);
var fragment = xsltProc.transformToFragment(xmlInputDoc, document);
console.log(fragment);
var table = document.getElementById('table1');
var tBody = document.getElementById('xmlResult');
tBody.appendChild(fragment);
<table id="table1" class="table_result">
<thead>
<tr>
<th>header1 </th>
<th>header2 </th>
<th>header3 </th>
</tr>
</thead>
<tbody id="xmlResult">
</tbody>
</table>
我在客户端中有一个 XML 和 XSL 文件,想将其转换为 HTMLElement 以便我可以附加到我的当前元素
但结果与我的预期相去甚远。除了 <td>
和 <tr>
标记被排除在 HtmlElement 结果
我的XML文件
<?xml version="1.0" encoding="UTF-8"?>
<ns1:Courses xmlns:ns1="www.Course.com">
<ns1:Course xmlns:ns1="www.Course.com">
<ns1:Id>2153</ns1:Id>
<ns1:Name>7 bước làm sao tận hưởng một chuyến du lịch nước ngoài với chi phí thấp</ns1:Name>
<ns1:Author>Hoàng Lê Giang</ns1:Author>
<ns1:AuthorImageURL>
d1nzpkv5wwh1xf.cloudfront.net/320/k-57b67d6f60af25054a055b25/20170928-gianghl_2809/gianghl01.jpg
</ns1:AuthorImageURL>
<ns1:Rating>0.0</ns1:Rating>
<ns1:RatingNumber>0</ns1:RatingNumber>
<ns1:Cost>599000.0</ns1:Cost>
</ns1:Course>
我的 XSL 文件
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" encoding="UTF-8" indent="yes" media-type="text/html"/>
<xsl:template match="/">
<xsl:for-each select="//*[local-name()='Course']">
<tr>
<td class="col1">
<div class="course_name">
<a href="{*[local-name()='SourceURL']}">
<xsl:value-of select="*[local-name()='Name']"/>
</a>
</div>
<div class="course_small_detail">
<img class="img_author_small"
src="{*[local-name()='AuthorImageURL']}"/>
<span class="author_name">
<xsl:value-of select="*[local-name()='Author']"/>
</span>
</div>
</td>
<td class="col2">
<xsl:value-of select="*[local-name()='Cost']"/>
</td>
<td class="col3">
<xsl:value-of select="*[local-name()='Rating']"/>
</td>
</tr>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
我的 javascript 用于转换和附加结果的代码
var xmlDoc = xmlHttp.responseXML;
var xslDoc = xslHttp.responseXML;
xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xslDoc);
//used to add to html document
var resultDocument = xsltProcessor.transformToFragment(xmlDoc, document);
console.log(resultDocument);
document.getElementById("xmlResult").appendChild(resultDocument);
我的 html 结果渲染
<table class="table_result">
<thead>
<tr>
<th>header1 </th>
<th>header2 </th>
<th>header3 </th>
</tr>
</thead>
<tbody id="xmlResult">
</tbody>
</table>
我相信这是因为我用来转换的 XSLTProcessor,因为我试图用我的 IDE 的 XSLT 工具转换它,结果是完全正确的
对于当前版本的 Firefox,Chrome 和 Windows 10 1803 上的 Edge,我可以使用 XSLT return 带有 XHTML [=12 的片段=] 元素然后被插入到托管 HTML 文档和 tbody
:
var domParser = new DOMParser();
var xmlString = `<root>
<item>
<value>foo 1<\/value>
<value>foo 2<\/value>
<value>foo 3<\/value>
<\/item>
<item>
<value>bar 1<\/value>
<value>bar 2<\/value>
<value>bar 3<\/value>
<\/item>
<\/root>`;
var xslString = `<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="item">
<tr>
<xsl:apply-templates/>
<\/tr>
<\/xsl:template>
<xsl:template match="value">
<td>
<xsl:apply-templates/>
<\/td>
<\/xsl:template>
<\/xsl:stylesheet>`;
var xmlInputDoc = domParser.parseFromString(xmlString, 'application/xml');
var xsltDoc = domParser.parseFromString(xslString, 'application/xml');
var xsltProc = new XSLTProcessor();
xsltProc.importStylesheet(xsltDoc);
var fragment = xsltProc.transformToFragment(xmlInputDoc, document);
console.log(fragment);
var table = document.getElementById('table1');
var tBody = document.getElementById('xmlResult');
tBody.appendChild(fragment);
<table id="table1" class="table_result">
<thead>
<tr>
<th>header1 </th>
<th>header2 </th>
<th>header3 </th>
</tr>
</thead>
<tbody id="xmlResult">
</tbody>
</table>