XQuery how to count with "where" condition

185 Views Asked by At

I'm just starting to learn XQuery and I want that it shows me the number of festivals with genre (genero) is the same as "metal". I can't get the total number of them, only separately.

Xquery

for $b in //festival
where $b/@genero="Metal"
return <prueba>{count ($b/@genero="Metal"), $b//nombre}</prueba>

XML

  <Festivales>
    <festival genero="Metal">
        <informacion>
            <nombre>Resurrection Fest</nombre>
            <fecha_inicio>2020-07-01</fecha_inicio>
            <fecha_fin>2020-07-04</fecha_fin>
    </festival>
    <festival genero="Rock-Heavy Metal">
        <informacion>
            <nombre>Rock the Night</nombre>
            <fecha_inicio>2020-06-26</fecha_inicio>
            <fecha_fin>2020-06-27</fecha_fin>
    </festival>
    <festival genero="Hardcore">
        <informacion>
            <nombre>Ieperfest</nombre>
            <fecha_inicio>2020-07-03</fecha_inicio>
            <fecha_fin>2020-07-05</fecha_fin>
        </informacion>
    </festival>
    <festival genero="Metal">
        <informacion>
            <nombre>Download UK</nombre>
            <fecha_inicio>2020-06-12</fecha_inicio>
            <fecha_fin>2020-06-14</fecha_fin>
        </informacion>
    </festival>
</Festivales>

Result

<prueba>1<nombre>Resurrection Fest</nombre>
</prueba>
<prueba>1<nombre>Hellfest</nombre>
</prueba>
<prueba>1<nombre>Download UK</nombre>
</prueba>

Thanks!

1

There are 1 best solutions below

0
On
for $b in //festival[@genero="Metal"]
let $n := $b/informacion/nombre/text()
return 
    <prueba>
        {
            <cnt>{count(//festival[@genero="Metal"]/informacion/nombre[. = $n])}</cnt>
          , $b/informacion/nombre
        }
   </prueba>