XQuery different values ​​with the job where clause

I am very new to XQuery, so excuse me if I missed something.

I am trying to extract data where certain sub-elements of a DISTINCT element, and also where some kindred node 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 queries that I request are as follows:

<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 events for the product, and only where the category attribute name is "Cheap".

My query returns DISTINCT products, but the where clause does not seem to have an effect, it still returns products whose category is “expensive”.

Can anyone tell me what I'm doing wrong.

+2
source share
1

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"])
+4

All Articles