Value Queries

XQuery is equally adept at finding data based on value. The following examples combine structural queries with restrictions on the values returned in the result.

To select all parts that have a parent-part as a child and also have a parent-part value of 1:

dbxml> query '
collection("parts.dbxml")/part[parent-part = 1]'  

100 objects returned for eager expression '
collection("parts.dbxml")/part[parent-part = 1]'

Notice that the query is identical to the query used in the previous example, except that it uses '[parent-part = 1]'. The results follow:

dbxml> print
<part number="1840"><description>Description of 1840</description>
<category>0</category><parent-part>1</parent-part></part>
<part number="1330"><description>Description of 1330</description>
<category>0</category><parent-part>1</parent-part></part>
...
<part number="1300"><description>Description of 1300</description>
<category>0</category><parent-part>1</parent-part></part>
<part number="790"><description>Description of 790</description>
<category>0</category><parent-part>1</parent-part></part>

XQuery also provides a full set of expressions that you can use to select documents from the container. For instance, if we wanted to look up the parts with part numbers 1070 and 1032 we could run the following query:

Note

This query is searching on the value of an attribute rather than the value of an element. This is an equally valid way to search for documents.
dbxml> query '
collection("parts.dbxml")/part[@number = 1070 or @number = 1032]'

2 objects returned for eager expression '
collection("parts.dbxml")/part[@number = 1070 or @number = 1032]'

dbxml> print
<part number="1070"><description>Description of 1070</description>
<category>0</category><parent-part>2</parent-part></part>
<part number="1032"><description>Description of 1032</description>
<category>2</category></part>

Standard inequality operators and other expressions are also available and help to isolate the required subset of data within a container:

dbxml> query '
collection("parts.dbxml")/part[@number > 100 and @number < 105]'

4 objects returned for eager expression '
collection("parts.dbxml")/part[@number > 100 and @number < 105]'

dbxml> print
<part number="101"><description>Description of 101</description>
<category>1</category></part>
<part number="102"><description>Description of 102</description>
<category>2</category></part>
<part number="103"><description>Description of 103</description>
<category>3</category></part>
<part number="104"><description>Description of 104</description>
<category>4</category></part>