在这个catalog.xml文件中.我有两本书有相同的库存(即20).我想编写一个XSL文件,它将在目录中显示最大数量的书籍副本.如果有两本或更多相同库存的书籍,则必须显示它们.
<catalog>
<Book>
<sku>12345</sku>
<title>Beauty Secrets</title>
<condition>New</condition>
<current_inventory>20</current_inventory>
<price>99.99</price>
</Book>
<Book>
<sku>54321</sku>
<title>Picturescapes</title>
<current_inventory>20</current_inventory>
<condition>New</condition>
<price>50.00</price>
</Book>
<Book>
<sku>33333</sku>
<title>Tourist Perspectives</title>
<condition>New</condition>
<current_inventory>0</current_inventory>
<price>75.00</price>
</Book>
<Book>
<sku>10001</sku>
<title>Fire in the Sky</title>
<condition>Used</condition>
<current_inventory>0</current_inventory>
<price>10.00</price>
</Book>
</catalog>
Run Code Online (Sandbox Code Playgroud)
以下是我的catalog3.xsl文件,它只能显示两本书中的一本:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:variable name="max"/>
<xsl:template match="/">
<html>
<body>
<h2>Titles of Books for which Most Copies are Available</h2>
<table border="2">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>No of Copies</th>
</tr>
<xsl:apply-templates/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="catalog">
<xsl:for-each select="Book">
<xsl:sort select="current_inventory" data-type="number" order="descending"/>
<tr>
<xsl:if test="position()= 1">
<p><xsl:value-of select="$max = "/></p>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="current_inventory"/></td>
</xsl:if>
</tr>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
任何人都可以纠正我实现我的目标,即在目录中显示具有相同最大库存的所有副本.谢谢.
最大值current_inventory可以通过以下方式计算:
<xsl:variable name="max">
<xsl:for-each select="/catalog/Book/current_inventory">
<xsl:sort data-type="number" order="descending"/>
<xsl:if test="position()=1"><xsl:value-of select="."/></xsl:if>
</xsl:for-each>
</xsl:variable>
Run Code Online (Sandbox Code Playgroud)
调整当前节点与变量xsl:if进行比较的标准即可达到所需的结果。current_inventoryfor-each$max
您正在评估 的position()=1内部for-each,这仅适用于排序集合中的第一项。
我将其设置为寻找current_inventory等于$max:
<xsl:if test="current_inventory = $max">
Run Code Online (Sandbox Code Playgroud)
将这些更改应用到您的样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--Determine the maximum current_inventory -->
<xsl:variable name="max">
<xsl:for-each select="/catalog/Book/current_inventory">
<xsl:sort data-type="number" order="descending"/>
<xsl:if test="position()=1"><xsl:value-of select="."/></xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:template match="/">
<html>
<body>
<h2>Titles of Books for which Most Copies are Available</h2>
<table border="2">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>No of Copies</th>
</tr>
<xsl:apply-templates/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="catalog">
<xsl:for-each select="Book">
<xsl:sort select="current_inventory" data-type="number" order="descending"/>
<xsl:if test="current_inventory = $max">
<tr>
<td>
<xsl:value-of select="title"/>
</td>
<td>
<xsl:value-of select="current_inventory"/>
</td>
</tr>
</xsl:if>
</xsl:for-each>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)