0
votes

I need to return the 5 largest elements that the loop returns. I'm trying with the position() function, but I can't...

<IsolatedData>
   <LleidaData>
      {
      for $dl in doc ("dadesLleida.xml")//row
      where $dl/residence = 'No'
      order by xs:integer($dl/confirmed_cases) descending
      return
      <ConfirmedCases>{$dl/confirmed_cases/text(), ' - ', $dl/start_date/text()}</ConfirmedCases>
    } 
    </LleidaData>
  </IsolatedData>```
1
Which XQuery processor or version is that? Later versions have a count clause you can introduce to set up a variable with the "position" and restrict it with a further where for instance. But doing (for .. where .. order .. return ..)[position() le 5] should also do. - Martin Honnen

1 Answers

0
votes

Use either

  for $dl in doc ("dadesLleida.xml")//row
  where $dl/residence = 'No'
  order by xs:integer($dl/confirmed_cases) descending
  count $p
  where $p le 5
  return
  <ConfirmedCases>{$dl/confirmed_cases/text(), ' - ', $dl/start_date/text()}</ConfirmedCases>

or

  (for $dl in doc ("dadesLleida.xml")//row
  where $dl/residence = 'No'
  order by xs:integer($dl/confirmed_cases) descending
  return
  <ConfirmedCases>{$dl/confirmed_cases/text(), ' - ', $dl/start_date/text()}</ConfirmedCases>)[position() le 5]