XQuery

XQuery

(pwalmsley@datypic.com)

ISBN: 1491915103

2nd edition, , O'Reilly Media, Inc.

Chapter 4: Navigating XML by Using Paths

Table 4-1. Simple path expressions
ExampleReturn value
doc("catalog.xml")/catalog The catalog element that is the outermost element of the document
doc("catalog.xml")//product All product elements anywhere in the document
doc("catalog.xml")//product/@dept All dept attributes of product elements in the document
doc("catalog.xml")/catalog/* All child elements of catalog
doc("catalog.xml")/catalog/*/number All number elements that are grandchildren of catalog
Example 4-1. Prefixed name tests
Input document (prod_ns.xml)
<prod:product xmlns:prod="http://datypic.com/prod">
  <prod:number>563</prod:number>
  <prod:name language="en">Floppy Sun Hat</prod:name>
</prod:product>
Query
declare namespace prod = "http://datypic.com/prod";
<prod:prodList>{
  doc("prod_ns.xml")/prod:product/prod:number
}</prod:prodList>
Results
<prod:prodList xmlns:prod="http://datypic.com/prod">
  <prod:number>563</prod:number>
</prod:prodList>
Table 4-4. Unabbreviated and abbreviated syntax examples
Unabbreviated syntaxAbbreviated equivalent
child::product product
child::* *
self::node() .
attribute::dept @dept
attribute::* @*
descendant::product .//product
child::product/descendant::name product//name
parent::node()/number ../number
Table 4-5. More complex steps (examples start with doc("catalog.xml")/catalog/)
ExampleReturn value
product/(number | name) All number AND name children of product.
product/(* except number) All children of product except number. See for more information on the | and except operators.
product/(if (desc) then desc else name) For each product element, the desc child if it exists; otherwise, the name child.
product/substring(name, 1, 30) A sequence of xs:string values that are substrings of product names.
Table 4-6. Predicates (examples start with doc("catalog.xml")/catalog/)
ExampleReturn value
product[name = "Floppy Sun Hat"] All product elements that have a name child whose value is equal to Floppy Sun Hat
product[number < 500] All product elements that have a number child whose value is less than 500
product[@dept = "ACC"] All product elements that have a dept attribute whose value is ACC
product[desc] All product elements that have at least one desc child
product[@dept] All product elements that have a dept attribute
product[@dept]/number All number children of product elements that have a dept attribute
Table 4-7. Position in predicates (examples start with doc("catalog.xml")/catalog/)
ExampleReturn value
product[2] The second product child of catalog
product[position() = 2] The second product child of catalog
product[position() > 1] All product children of catalog after the first one
product[last()-1] The second to last product child of catalog
product[last()] The last product child of catalog
*[2] The second child of catalog, regardless of name
product[3]/*[2] The second child of the third product child of catalog
Datypic XQuery Services