XQuery:两个查询之间的差异

时间:2018-02-03 19:49:59

标签: database xquery

这两个显然相等的查询给出了不同的输出。为什么?究竟发生了什么?

查询1:

for $n in doc("restaurants.xml")/restaurants/restaurant
where $n/name = "PizzaKebab"
return <Dish>{$n/dish/name}</Dish>

输出1:

<Dish>
  <name>Pizza</name>
  <name>Kebab</name>
</Dish>

查询2:

for $n in doc("restaurants.xml")/restaurants/restaurant[name="PizzaKebab"]
return <Dish>{$n/dish/name}</Dish>

输出2:

<Dish>
  <name>Pizza</name>
</Dish>
<Dish>
  <name>Kebab</name>
</Dish>

restaurants.xml

<restaurants>
  <restaurant>
    <name>PizzaKebab</name>
    <dish>
      <name>Pizza</name>
      <price>6</price>
    </dish>
    <dish>
      <name>Kebab</name>
      <price>3</price>
    </dish>
  </restaurant>
</restaurants>

1 个答案:

答案 0 :(得分:2)

您的查询在功能上完全相同。为证明这一点,您的处理器应返回true()以进行以下查询 - 它在我使用eXist 3.6.1,BaseX 8.6.7和Saxon 9.7.0.19的所有测试中都会执行。

xquery version "3.1";

let $restaurants := 
    document {
        <restaurants>
            <restaurant>
                <name>PizzaKebab</name>
                <dish>
                    <name>Pizza</name>
                    <price>6</price>
                </dish>
                <dish>
                    <name>Kebab</name>
                    <price>3</price>
                </dish>
            </restaurant>
        </restaurants>
    }
let $query1 := 
    for $n in $restaurants/restaurants/restaurant
    where $n/name = "PizzaKebab"
    return <Dish>{$n/dish/name}</Dish>
let $query2 := 
    for $n in $restaurants/restaurants/restaurant[name="PizzaKebab"]
    return <Dish>{$n/dish/name}</Dish>
return
    deep-equal($query1, $query2)