Introduction to XQuery
XQuery Is About Extracting Information from XML Documents.
What you shop already know Know
BEFORE You Study XQuery You Should Have A Basic Understanding of XML and XML Namespaces.
If You Want To Study Thase Subjects First, please read out our xml tutorial.
What is xquuery?
XQuery Is A Language for Querying XML Data XQuery IS Built On XPath Expressions XQuery for XML IS Like SQL for Databases
XQuery Is About Querying XML
Data from Xml Documents. X OF JOURNAL OF ANITenterMICAL EDUCATION.
Here Is An Example of a Question That XQuery Could Solve:
"SELECT All CD Records with a price less Than $ 10 from the cd collection stored in the xml document caled cd_catalog.xml"
XQuery and XPath
XQuery 1.0 and xpath 2.0 Sharees The Same Data Model, The Same Functions, And The Same Syntax.
If You Have Already Studied Xpath You Will Have No Problems with understanding xquuery.
You can read more about xpath in yur xpath tutorial.
XQuery Is Not (YET) A Web STANDARD
XQuery 1.0 is Still A W3C Working Draft.
IT IS Expected to Reach A W3C Recommendation Status Early IN 2004.
To read more about the xquery activity at w3c, please read our w3c tutorial.
XQuery EXAMPLE
Let's try to learn some xquuery by an example.
The XML EXAMPLE Document
We will use this xml docuument called "books.xml" in all our eXamples:
XML Version = "1.0" Encoding = "ISO-8859-1"?>
book>
book>
book>
editor>
book>
bib>
Click Here to View The XML File in your browser
Extracing Nodes with a functions
XQuery Uses Functions to Extract Data from XML Documents.
The Following XQuery:
DOC ("Books.xml")
Will Extract The Full Xml Document from The File "Books.xml". (In Exact Phrases The Function Returns The Document Node of the Document)
Extracing Nodes with a node path
XQuery Uses XPath Node paths to extract data from xml documents.
The Following XQuery:
DOC ("Books.xml") / BIB / BOOK / TITLE
Will Extract The Following Data:
In The Example Above, The Function Doc ("Book.xml") IS Used To Open THE XML Document. (In Exact Phrases the Function Returns the Document Node of the Document)
The XPath node path / bib / book / title is used to extract all the title elements. (/ Bib selects the bib element, / book selects all the book elements under the bib element, and / title selects all the title elements under each book ELEMENT)
If you know how Xath Works, You Will Know That XQuery Will Give the Same Result:
DOC ("Books.xml") // Title
Extracting nodes with an expression
XQuery Uses Expressions to Extract Data from XML Documents.
The Following XQuery:
DOC ("Books.xml") / BIB / BOOK [price <50]
Will Extract The Following Data:
book>
In The Example Above, The Function Doc ("Book.xml") Returns The Document Node of The Document.
The XPath node path / bib / book selects all the book elements under the bib element, and the expression (condition) [price <50] selects a subset of the book elements. (Only the book elements with a price element with a value less Than 50)
XQuery Flwor
FLWOR = for, let, where, order by, return.the xml example document
We will use this xml docuument called "books.xml" in all our eXamples:
XML Version = "1.0" Encoding = "ISO-8859-1"?>
book>
book>
book>
editor>
book>
bib>
Click Here to View The XML File in your browser
Extracing Nodes with flword
The Following Flwor Expression: for $ x in doc ("Books.xml") / BIB / BOOK
Where $ x / price> 50
Order by $ x / title
Return $ X / TITLE
Will Extract The Following Data:
In The Example Above:
The for Clause Selects All Book Nodes (Book Elements Under The Bib Element) INTO A VARIABLE CALLED $ X.
The Where Clause Selects Only The $ x Nodes WITH Price Elements with a value Greater Than 50.
The Order by Clause Orders the $ x nodes (book nodes) by the value of the title elements.
The Return Clause Returns The Title Nodes.