如何使用xsl:sort检索最大值

时间:2015-06-07 02:26:19

标签: xml xslt-1.0

我有一个xml文件,其中包含多个类型为' advert'的元素。最后一个包含作为子元素的价格。

我想使用浏览所有项目的xsl:sortxsl:for-each提取最高价格值。

1 个答案:

答案 0 :(得分:2)

As stated in the comments for other members, max simply could be used as is without the need of xslt sort and xslt for-each.

To be able to use the xsl in xsl as is, here is a simple piece of code to try:

<xsl:variable name="the_max">
<xsl:for-each select="Node/Item/year">
<xsl:sort data-type="number" order="descending"/>
<xsl:if test="position()=1"><xsl:value-of select="."/></xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="$the_max"/>

using XSL values as follows:

<?xml version="1.0" encoding="UTF-8"?>
<Node>
<Item><year>1985</year></Item>
<Item><year>1986</year></Item>
<Item><year>1987</year></Item>
<Item><year>1988</year></Item>
<Item><year>1989</year></Item>
<Item><year>1909</year></Item>
<Item><year>1991</year></Item>
<Item><year>1992</year></Item>
<Item><year>1993</year></Item>
<Item><year>1994</year></Item>
<Item><year>1995</year></Item>
<Item><year>1996</year></Item>
</Node>