XQuery

XQuery

(pwalmsley@datypic.com)

ISBN: 0596006349

1st edition, , O'Reilly Media, Inc.

Chapter 4: Navigating input documents using paths

Table 4-1. Simple path expressions
ExampleExplanation
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 the catalog element
doc("catalog.xml")/catalog/*/number All number elements that are grandchildren of the catalog element
Example 4-1. Prefixed name tests
declare namespace prod = "http://datypic.com/prod";
<prod:prodList>{
  doc("prod_ns.xml")/prod:product/prod:number
}</prod:prodList>
Table 4-5. More complex steps (examples start with doc("catalog.xml")/catalog/)
ExampleMeaning
product/(number | name)All number AND name children of product.
product/(* except number) All children of product except number. See "Combining Results" in Chapter 9 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/)
ExampleMeaning
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/)
ExampleDescription
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