XQuery与where子句问题的不同值 [英] XQuery distinct values with where clause problem

查看:145
本文介绍了XQuery与where子句问题的不同值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对XQuery还是很陌生,所以如果我因某种原因缺少某些东西,请原谅。

I'm very new to XQuery so excuse me if I am somehow missing something.

我正在尝试提取元素中某些子节点所在的数据DISTINCT,以及某个兄弟节点等于某个预定义字符串的地方

I am trying to extract data where certain sub-nodes of an element are DISTINCT, as well as where a certain sibling node's is equal to some predefined string

for $product in fn:distinct-values(document('cpwdoc')/root/package/properties/value[@key="product"])
where document('cpwdoc')/root/package/categories/category[@name="Cheap"]
return $product

我要查询的XML如下:

The XML I am querying looks like this:

<root>
 <package>
      <title>Some package 1</title>
      <categories><category group="Other" name="Cheap"/></categories>
      <properties>
        <value key="product">BLUE-TOOTHBRUSH</value>
      </properties>
    </package>
 <package>
      <title>Some package 2</title>
      <categories><category group="Other" name="Expensive"/></categories>
      <properties>
        <value key="product">BLUE-TOOTHBRUSH</value>
      </properties>
    </package>
 <package>
      <title>Some package 3</title>
      <categories><category group="Other" name="Expensive"/></categories>
      <properties>
        <value key="product">TOOTHPASTE</value>
      </properties>
    </package>
</root>

所以基本上我只想要产品的DISTINCT出现,并且只在类别的名称属性为

So basically I want only DISTINCT occurances of the product, and only where the name attribute of the category is equal to "Cheap".

我的查询返回DISTINCT产品,但是where子句似乎无效,它仍返回类别为昂贵的产品。

My query returns DISTINCT products, but the where clause seems to have no effect, it still returns products whose category is "Expensive".

有人可以告诉我我在做什么错。

Can anyone advise as to what I am doing wrong.

推荐答案

您的where子句:

where document('cpwdoc')/root/package/categories/category[@name="Cheap"]

扩展为:

where boolean(document('cpwdoc')...)

等效于

where exists(document('cpwdoc')...)

,因此,只要有至少一种便宜的产品,您就退货所有产品。

and so you are returning all products as long as there is at least one cheap product.

您想要类似以下内容

distinct-values(
  for $package in document('cpwdoc')/root/package
  let $value := $package/properties/value
  where $value/@key = "product" and $package/categories/category/@name="Cheap"
  return $value
)

如果您喜欢路径表达式,则与

which if you like path expressions is the same as

distinct-values(document('cpwdoc')/root/package[categories/category/@name="Cheap"]/properties/value[@key="product"])

这篇关于XQuery与where子句问题的不同值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆