我试图弄清楚如何使用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>
答案 0 :(得分:1)
根据规范
<!-- Category: instruction -->
<xsl:for-each
select = node-set-expression>
<!-- Content: (xsl:sort*, template) -->
</xsl:for-each>
<!-- Category: instruction -->
<xsl:for-each
select = expression>
<!-- Content: (xsl:sort*, sequence-constructor) -->
</xsl:for-each>
xsl:sort
定义(如果存在)必须是for-each
内的第一个子元素。