我对xml和xsl完全陌生,所以我遇到一些困难,让我的xml文件看起来像我想要的那样。基本上问题是表格里面的所有内容都正确显示,但xml的内容也显示在表格之后。所以我总是有一个表,后面跟着xml的所有数据。我正在Firefox 16.0.2上测试我的xml文件。
这是我的xml文件的一部分。
<root>
<name id = "content_here">
<first> Cathy </first>
<last> Claires </last>
</name>
... more names down here
</root>
我试图在Firefox上以表格格式显示它,这就是我为xsl文件所做的。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="root">
<html>
<body>
<table>
<tr>
<th> id </th>
<th> first name </th>
<th> last name </th>
</tr>
<xsl:for-each select="name">
<tr>
<td> <xsl:value-of select="@id"/> </td>
<td> <xsl:value-of select="first"/> </td>
<td> <xsl:value-of select="last"/> </td>
</tr>
</xsl:for-each>
</table>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
任何人都可以给我一个关于如何摆脱桌子后额外内容的提示?谢谢!
答案 0 :(得分:1)
xsl:apply-templates
指令会导致模板上下文的所有节点子节点(此处为root
元素)悬停在built-in templates中。从样式表中删除它应删除内容。
请注意,实际使用xsl:apply-templates
规则时,有更好的方法。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="root">
<html>
<body>
<table>
<tr>
<th> id </th>
<th> first name </th>
<th> last name </th>
</tr>
<xsl:apply-templates/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="name">
<tr>
<td> <xsl:value-of select="@id"/> </td>
<td> <xsl:value-of select="first"/> </td>
<td> <xsl:value-of select="last"/> </td>
</tr>
</xsl:template>
</xsl:stylesheet>
此处xsl:apply-templates
用于将模板匹配应用于root
内table
的子项。匹配name
元素时,会创建tr
。这通常比使用xsl:for-each
更好。