This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Finding element with min value in multiple files
- To: Peter Dulak <peter dot dulak at reuters dot com>
- Subject: Re: [xsl] Finding element with min value in multiple files
- From: Jeni Tennison <mail at jenitennison dot com>
- Date: Fri, 12 Jan 2001 09:39:30 +0000
- CC: xsl-list at lists dot mulberrytech dot com
- Organization: Jeni Tennison Consulting Ltd
- References: <200101120235.VAA13770@biglist.com>
- Reply-To: xsl-list at lists dot mulberrytech dot com
Hi Peter,
> How do I find max or min value of a given element in multiple files?
Lots of ways. A quick and simple way to find the minimum would be to
have a variable hold the values:
<xsl:variable name="costs"
select="document(Replies/File)/Document/Cost" />
And then to find those nodes in the $costs node set where there's no
other node in the node set with a smaller value:
$costs[not($costs < .)]
Another way is to use a sort in ascending order and then pick the
first in the list:
<xsl:for-each select="$costs">
<xsl:sort select="." data-type="number" />
<xsl:if test="position() = 1">
<xsl:value-of select="." />
</xsl:if>
</xsl:for-each>
A final way involves constructing a new result tree fragment holding
the costs:
<xsl:variable name="costs">
<xsl:copy-of select="document(Replies/File)/Document/Cost" />
</xsl:variable>
and then to use this as the basis of a recursive solution. Apply
templates to the first Cost in this list:
<xsl:apply-templates select="$costs/Cost[1]" />
and have a template:
<xsl:template match="Cost" mode="minimum">
<xsl:variable name="next"
select="following-sibling::Cost[. < current()]" />
<xsl:choose>
<xsl:when test="$next">
<xsl:apply-templates select="$next" mode="minimum" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="." />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
This solution works best under XSLT 1.1, but you can use it now if you
use the node-set() extension function provided under most XSLT
processors. For now you need to apply templates with something like:
<xsl:apply-templates select="saxon:node-set($costs)/Cost[1]" />
Which one of the above works best will depend on how many Costs you
have, what your processor optimises and so on - try them to see.
I hope that helps,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list