Search code examples
xmlxqueryflwor

Vacations in eXide


<vacaciones>
    <destino identificador="p023" tipo="unico">
        <titulo>París, la ciudad del amor</titulo>
        <fecha_salida>
              <mes>Marzo</mes>
        </fecha_salida>
        <estancias>
          <hotel estrellas="4">Macuya</hotel>
        </estancias>
    </destino>

    <destino identificador="m036" tipo="unico">
        <titulo>Islas Baleares, ahí mismo</titulo>
        <fecha_salida>
            <mes>Abril</mes>
        </fecha_salida>
        <estancias>
          <hotel estrellas="2">Fuentevino</hotel>
          <hotel estrellas="3">Tresarazos</hotel>
        </estancias>
    </destino>

    <destino identificador="i07" tipo="pack">
        <titulo>Italia, en sí misma</titulo>
        <fecha_salida>
            <mes>Agosto</mes>
        </fecha_salida>
        <estancias>
            <hotel estrellas="3">Mortareilo</hotel>
            <hotel estrellas="2">Sisa da Roca</hotel>
            <hotel estrellas="5">A Gatti</hotel>
            <hotel estrellas="3">La Nostra</hotel>
        </estancias>
    </destino>

    <destino identificador="a456" tipo="unico">
        <titulo>Amsterdam, la Venecia del norte</titulo>
        <fecha_salida>
            <mes>Agosto</mes>
        </fecha_salida>
        <estancias>
            <hotel estrellas="3">The sounders</hotel>
        </estancias>
    </destino>

    <destino identificador="n046" tipo="pack">
        <titulo>Los secretos del Nilo</titulo>
        <fecha_salida>
            <mes>Septiembre</mes>
        </fecha_salida>
        <estancias>
        </estancias>
    </destino>
</vacaciones>

I want the nombre of the hotels with more than 2 stars.

I have two different codes:

1.-

xquery version "3.1";
for $i in collection("/db/exercise")//destino
where $i//estancias/hotel/@estrellas>"2"
return $i/estancias/hotel/text()

Which doesn't give the expected result

2.-

xquery version "3.1";
for $i in collection("/db/exercise")//destino
return $i/estancias/hotel[@estrellas>2]/text()

Which gives the expected result.

Can anyone explain to me why does the first one not give the result I wanted? Or point out to a web page that explains why?

And why, when I try to get the titulo of every viaje starting in August, it works?

for $i in collection("/db/exercise")//destino
where $i/fecha_salida/mes="Agosto"
return ($i/titulo)

Solution

  • The first one

    hotel/@estrellas>"2"
    

    checks for the existence of a hotel with an attribute estrellas, which attribute has a value greater than "2". Since at least one of these exists, it will return true.

    The second expression

    /hotel[@estrellas>2]/text()
    

    selects only hotels which have that attribute with that value (i.e, it filters out those hotels which don't have it). The final /text() returns their names.

    For one of many examples, see here.