Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1079913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:56:42+00:00 2026-05-16T21:56:42+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:56:42+00:00Added an answer on May 16, 2026 at 9:56 pm

    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"])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.