XML query language
|
XQuery is a programming language under development by the W3C that's designed to query collections of XML data. XQuery provides a mechanism to extract and manipulate data from XML documents or any data source that can be viewed as XML such as relational databases or office documents. It is semantically similar to SQL. XQuery uses XPath syntax to address specific parts of an XML document. XSLT 2.0 and XQuery are being jointly developed by the XML Query working group.
XQuery provides a mechanism to pull XML content from multiple sources and dynamically generate new content using a declarative language.
XML is a native data type of XQuery and can be used in queries directly without quoted strings or object calls. You separate XML elements from enclosed expressions using curly braces.
Examples
The sample XQuery code below lists the unique speakers in each act of Shakespeare's play Hamlet.
<html><head/><body> { for $act in doc("hamlet.xml")//ACT let $speakers := distinct-values($act//SPEAKER) return <span> <h1>{ $act/TITLE/text() }</h1> <ul> { for $speaker in $speakers return <li>{ $speaker }</li> } </ul> </span> } </body></html>
XQuery is a "functional language" consisting entirely of expressions. There are no statements, even though some of the keywords imply statement-like behaviors. To execute a function, the expression within the body gets evaluated and its value returned. Thus to write a function to double an input value, you simply write:
declare function local:doubler($x) { $x * 2 }
To write a full query that says Hello World you write the expression:
"Hello World"
External links
- W3C XML Query (XQuery) (http://www.w3.org/XML/Query)
- XQuery 1.0: An XML Query Language (http://www.w3.org/TR/xquery/)
- XQuery.com: Specifications, Articles, Mailing List, and Vendors (http://www.xquery.com)
Portions borrowed with permission from the book "XML Hacks" (O'Reilly).
Previous version based on an article at the French language Wikipedia (http://fr.wikipedia.org/wiki/XML_Query).de:XQuery fr:XML Query nl:XML Query Language pl:XQuery