XQuery 不同值与 where 子句问题

发布于 2024-09-19 23:14:33 字数 1384 浏览 4 评论 0原文

我对 XQuery 很陌生,所以如果我遗漏了什么,请原谅。

我正在尝试提取元素的某些子节点不同的数据,以及某个同级节点等于某个预定义字符串的数据

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 如下所示:

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

所以基本上我只想要产品的不同出现,并且仅当类别的名称属性等于“Cheap”时。

我的查询返回 DISTINCT 产品,但 where 子句似乎没有效果,它仍然返回类别为“昂贵”的产品。

谁能告诉我我做错了什么。

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

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

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>

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囚你心 2024-09-26 23:14:33

您的 where 子句:

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

扩展为:

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

这相当于

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

,因此只要有至少一种廉价产品,您就会退回所有产品。

您想要类似以下内容的内容

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
)

,如果您喜欢路径表达式,则与以下内容相同

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

Your where clause:

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

is expanded out to:

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

which is equivalent to

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

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

You want something like the following

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"])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文