Cathy Claires
Cathy Claires

Reputation: 79

xquery multiple maximum values

How do i return all the maximum values in xquery? I used max() but it only gives me one! Thanks.

<id>a</id>
<average>5</average>
<id>b</id>
<average>5</average>
<id>c</id>
<average>5</average>
<id>d</id>
<average>4</average>
<id>e</id>
<average>1</average>

and i want my results to be:

<id>a</id>
<id>b</id>
<id>c</id>

Thanks!

Upvotes: 2

Views: 360

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243449

Use:

/*/id[following-sibling::*[1]/number() eq max(/*/average)]

The above is both a correct XQuery and a correct XPath 2.0 solution.

Verification with Saxon EE 9.3.04 XQuery:

When the above XQUery is applied on the following XML document (the provided fragment, wrapped into a single top element to make it a well-formed XML document):

<t>
    <id>a</id>
    <average>5</average>
    <id>b</id>
    <average>5</average>
    <id>c</id>
    <average>5</average>
    <id>d</id>
    <average>4</average>
    <id>e</id>
    <average>1</average>
</t>

the wanted, correct result is produced:

<?xml version="1.0" encoding="UTF-8"?><id>a</id><id>b</id><id>c</id>

When this XSLT 2.0 transformation is applied (using Saxon 9.1.05) on the same XML document (above):

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <xsl:sequence select=
   "/*/id[following-sibling::*[1]/number() eq max(/*/average)]"/>
 </xsl:template>
</xsl:stylesheet>

again the same correct result is produced:

<id>a</id>
<id>b</id>
<id>c</id>

Upvotes: 1

Related Questions