1
<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>

我想要超过 2 星的酒店的名称。

我有两个不同的代码:

1.-

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

哪个没有给出预期的结果

2.-

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

这给出了预期的结果。

谁能向我解释为什么第一个没有给出我想要的结果?或者指出一个解释原因的网页?

为什么,当我从 8 月开始尝试获得每个 viaje 的名称时,它会起作用?

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

1 回答 1

1

第一个

hotel/@estrellas>"2"

检查是否存在hotel带有属性的 a estrellas,该属性的值大于“2”。由于其中至少存在一个,它将返回true.

第二个表达

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

仅选择具有该属性且具有该值的酒店(即,它会过滤掉那些没有该属性的酒店)。最后/text()返回他们的名字。

有关众多示例之一,请参见此处。

于 2020-05-13T17:24:16.577 回答