【发布时间】:2013-11-12 16:47:11
【问题描述】:
我试图弄清楚如何使用 XSL 按价格对目录的 XML 列表进行排序。现在它只显示来自 XML 的正确信息。如果我使用以下代码。
<xsl:template match="catalog">
<html>
<head>
<title>book catalog</title>
</head>
<body>
<h1>book catalogs</h1>
<table border="1">
<thead>
<tr bgcolor="red">
<td>id</td><td>author</td><td>title</td><td>generation</td><td>price</td><td>publish date</td><td>description</td>
</tr>
</thead>
<xsl:for-each select="book">
<xsl:sort data-type="number" select="price" />
<tbody>
<tr>
<td><xsl:value-of select="@id"/></td>
<td><xsl:value-of select="author"/></td>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="generation"/></td>
<td><xsl:value-of select="price"/></td>
<td><xsl:value-of select="publishDate"/></td>
<td><xsl:value-of select="description"/></td>
</tr>
</tbody>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
但如果我改变 xsl:sort 的位置并将其放在 tbody 标记之后,则会产生错误。 这两个代码有什么区别?是否有必要在 xsl:for-each 之后插入 xsl:sort 或其他任何导致问题的东西?
<body>
<h1>book catalogs</h1>
<table border="1">
<thead>
<tr bgcolor="red">
<td>id</td><td>author</td><td>title</td><td>generation</td><td>price</td><td>publish date</td><td>description</td>
</tr>
</thead>
<xsl:for-each select="book">
<tbody>
<tr>
<xsl:sort data-type="number" select="price" />
<td><xsl:value-of select="@id"/></td>
<td><xsl:value-of select="author"/></td>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="generation"/></td>
<td><xsl:value-of select="price"/></td>
<td><xsl:value-of select="publishDate"/></td>
<td><xsl:value-of select="description"/></td>
</tr>
</tbody>
</xsl:for-each>
</table>
</body>
【问题讨论】:
-
xsl:sort 必须作为 xsl:for-each 的第一个孩子出现。您希望通过将其放在其他地方来实现什么?
-
感谢您的帮助。没什么特别的,只是我想知道有什么区别以及确切的问题是什么。非常感谢,您的评论对我有帮助。