xslt – XSL在catalog.xml文件中显示最大份数
发布时间:2020-12-16 05:33:43 所属栏目:百科 来源:网络整理
导读:在这个catalog.xml文件中.我有两本书有相同的库存(即20).我想编写一个XSL文件,它将在目录中显示最大数量的书籍副本.如果有两本或更多相同库存的书籍,则必须显示它们. catalog Book sku12345/sku titleBeauty Secrets/title conditionNew/condition current_i
在这个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> 下面是我的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> 任何人都可以纠正我实现我的目标,即在目录中显示具有相同最大库存的所有副本.谢谢.
最大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的条件:if将for-each中当前节点的current_inventory与$max变量进行比较,可以获得所需的结果. 您正在评估for-each内部的position()= 1,这对于已排序集合中的第一个项目仅为true. 我将其设置为查找等于$max的current_inventory: <xsl:if test="current_inventory = $max"> 将这些更改应用于样式表: <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> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |