下面是 XML 和 XSLT 的示例:
XML:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<count>5</count>
<idx>10</idx>
<cds>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
</cds>
</catalog>
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" omit-xml-declaration="yes" standalone="yes" />
<xsl:template match="/catalog/cds">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Title</th>
<th style="text-align:left">Artist</th>
</tr>
<xsl:for-each select="cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
问题:为什么在转换给定的 XML 文档时元素 count
和 idx 的内容
一直打印在 table 前吗?
最佳答案
XSLT 内置模板(XSLT2 和 XSLT3 的文档)正在执行此操作。具体来说:
<xsl:template match="text()|@*">
<xsl:value-of select="."/>
</xsl:template>
您可以覆盖或简单地更改您的模板以匹配 cds
<xsl:template match="/catalog/cds">
匹配/
:
<xsl:template match="/">
并且将避免虚假文本节点输出。
关于xml - 为什么我的 XSLT 输出中有额外的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36558224/