XQuery
Priscilla Walmsley (pwalmsley@datypic.com)
ISBN: 1491915103
2nd edition, , O'Reilly Media, Inc.
Chapter 4: Navigating XML by Using Paths
Example | Return 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
|
<prod:product xmlns:prod="http://datypic.com/prod"> <prod:number>563</prod:number> <prod:name language="en">Floppy Sun Hat</prod:name> </prod:product>
declare namespace prod = "http://datypic.com/prod"; <prod:prodList>{ doc("prod_ns.xml")/prod:product/prod:number }</prod:prodList>
<prod:prodList xmlns:prod="http://datypic.com/prod"> <prod:number>563</prod:number> </prod:prodList>
Unabbreviated syntax | Abbreviated equivalent |
---|---|
child::product
|
product
|
child::*
|
*
|
self::node()
|
.
|
attribute::dept
|
@dept
|
attribute::*
|
@*
|
descendant::product
|
.//product
|
child::product/descendant::name
|
product//name
|
parent::node()/number
|
../number
|
Example | Return 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. |
Example | Return 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 |
Example | Return 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
|