gpt4 book ai didi

xslt - XSL 显示 catalog.xml 文件中的最高副本数

转载 作者:行者123 更新时间:2023-12-04 19:49:27 25 4
gpt4 key购买 nike

在此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>

任何人都可以纠正我以实现我在目录中显示具有相同最大库存的所有副本的目标。谢谢。

最佳答案

这个 XSLT 1.0 样式表

<xsl:stylesheet 
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:template match="catalog">
<!-- find the maximum <current_inventory> of all books -->
<xsl:variable name="MaxInventory">
<xsl:for-each select="Book">
<xsl:sort select="current_inventory" data-type="number" order="descending" />
<xsl:if test="position() = 1">
<xsl:value-of select="current_inventory" />
</xsl:if>
</xsl:for-each>
</xsl:variable>

<!-- output all matching <Book>s, sorted by title -->
<table>
<xsl:apply-templates select="Book[current_inventory = $MaxInventory]">
<xsl:sort select="title" data-type="text" order="ascending" />
</xsl:apply-templates>
</table>
</xsl:template>

<xsl:template match="Book">
<tr>
<td><xsl:value-of select="title" /></td>
<td><xsl:value-of select="current_inventory" /></td>
</tr>
</xsl:template>

</xsl:stylesheet>

产生
<table>
<tr>
<td>Beauty Secrets</td>
<td>20</td>
</tr>
<tr>
<td>Picturescapes</td>
<td>20</td>
</tr>
</table>

请注意,存在更有效的解决方案,它涉及 XSL key (Dimitre Novatchev 展示了它)。这个简单易懂,XSL key 解决方案先进,仅对大型输入文档(即数千本书)发挥其“效率卡”。对于小的输入文档,性能差异可以忽略不计。

关于xslt - XSL 显示 catalog.xml 文件中的最高副本数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2481870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com