我试图弄清楚如何使用 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>