World Library  
Flag as Inappropriate
Email this Article


Article Id: WHEBN0023742879
Reproduction Date:

Title: XQuery  
Author: World Heritage Encyclopedia
Language: English
Subject: XQuery API for Java, Document-oriented database, NoSQL, BaseX, Database
Publisher: World Heritage Encyclopedia


Paradigm(s) declarative, functional, modular
Designed by W3C
Appeared in 2007
Stable release 1.0/ January 23, 2007 (2007-01-23)
Preview release 3.0/ July 23, 2013 (2013-07-23)
Typing discipline dynamic or static,[1][2] strong
Major implementations Many
Influenced by XPath, SQL, XSLT
OS Cross-platform
Filename extension(s) .xq, .xqy, .xquery
Website //Query/
  • XQuery at Wikibooks

XQuery is a query and functional programming language that is designed to query and transform collections of structured and unstructured data, usually in the form of XML, text and with vendor-specific extensions for other data formats (JSON, binary, etc.). The language is developed by the XML Query working group of the W3C. The work is closely coordinated with the development of XSLT by the XSL Working Group; the two groups share responsibility for XPath, which is a subset of XQuery.

XQuery 1.0 became a W3C Recommendation on January 23, 2007.[3]

XQuery 3.0 became a W3C Recommendation on April 8, 2014.[4]

"The mission of the XML Query project is to provide flexible query facilities to extract data from real and virtual documents on the World Wide Web, therefore finally providing the needed interaction between the Web world and the database world. Ultimately, collections of XML files will be accessed like databases".[5]


XQuery provides the means 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.

XQuery contains a superset of XPath expression syntax to address specific parts of an XML document. It supplements this with a SQL-like "FLWOR expression" for performing joins. A FLWOR expression is constructed from the five clauses after which it is named: FOR, LET, WHERE, ORDER BY, RETURN.

The language also provides syntax allowing new XML documents to be constructed. Where the element and attribute names are known in advance, an XML-like syntax can be used; in other cases, expressions referred to as dynamic node constructors are available. All these constructs are defined as expressions within the language, and can be arbitrarily nested.

The language is based on the XQuery and XPath Data Model (XDM) which uses a tree-structured model of the information content of an XML document, containing seven kinds of nodes: document nodes, elements, attributes, text nodes, comments, processing instructions, and namespaces.

XDM also models all values as sequences (a singleton value is considered to be a sequence of length one). The items in a sequence can either be XML nodes or atomic values. Atomic values may be integers, strings, booleans, and so on: the full list of types is based on the primitive types defined in XML Schema.

XQuery 1.0 does not include features for updating XML documents or databases; it also lacks full text search capability. These features are both under active development for a subsequent version of the language. However, the new standards such as XQuery Update Facility 1.0 supports update feature and XQuery and XPath Full Text 1.0 support full text search in XML documents.

XQuery is a programming language that can express arbitrary XML to XML data transformations with the following features:

  1. Logical/physical data independence
  2. Declarative
  3. High level
  4. Side-effect free
  5. Strongly typed


The sample XQuery code below lists the unique speakers in each act of Shakespeare's play Hamlet, encoded in hamlet.xml

   for $act in doc("hamlet.xml")//ACT
   let $speakers := distinct-values($act//SPEAKER)

{ string($act/TITLE) }

    { for $speaker in $speakers return
  • { $speaker }
  • }

All XQuery constructs for performing computations are expressions. There are no statements, even though some of the keywords appear to suggest statement-like behaviors. To execute a function, the expression within the body is evaluated and its value is returned. Thus to write a function to double an input value, one simply writes:

declare function local:doubler($x) { $x * 2 }

To write a full query saying 'Hello World', one writes the expression:

"Hello World"

This style is common in functional programming languages. However, unlike most functional programming languages, XQuery 1.0 doesn't support higher-order functions (they first appear in the drafts for XQuery 3.0).


Below are a few examples of how XQuery can be used:

  1. Extracting information from a database for use in a web service.
  2. Generating summary reports on data stored in an XML database.
  3. Searching textual documents on the Web for relevant information and compiling the results.
  4. Selecting and transforming XML data to XHTML to be published on the Web.
  5. Pulling data from databases to be used for the application integration.
  6. Splitting up an XML document that represents multiple transactions into multiple XML documents.

XQuery and XSLT compared


Although XQuery was initially conceived as a query language for large collections of XML documents, it is also capable of transforming individual documents. As such, its capabilities overlap with XSLT, which was designed expressly to allow input XML documents to be transformed into HTML or other formats.

The XSLT 2.0 and XQuery standards were developed by separate working groups within W3C, working together to ensure a common approach where appropriate. They share the same data model (XDM), type system, and function library, and both include XPath 2.0 as a sublanguage.


The two languages, however, are rooted in different traditions and serve the needs of different communities. XSLT was primarily conceived as a stylesheet language whose primary goal was to render XML for the human reader on screen, on the web (as web template language), or on paper. XQuery was primarily conceived as a database query language in the tradition of SQL.

Because the two languages originate in different communities, XSLT is stronger in its handling of narrative documents with more flexible structure, while XQuery is stronger in its data handling (for example, when performing relational joins).


XSLT 1.0 appeared as a Recommendation in 1999, whereas XQuery 1.0 only became a Recommendation in early 2007; as a result, XSLT is at present much more widely used. Both languages have similar expressive power, though XSLT 2.0 has many features that are missing from XQuery 1.0, such as grouping, number and date formatting, and greater control over XML namespaces.[6][7][8] Many of these features are planned for XQuery 3.0.[9]

Any comparison must take into account the fact that XSLT 1.0 and XSLT 2.0 are very different languages. XSLT 2.0, in particular, has been heavily influenced by XQuery in its move to strong typing and schema-awareness.

Strengths and weaknesses

Usability studies have shown that XQuery is easier to learn than XSLT, especially for users with previous experience of database languages such as SQL.[10] This can be attributed to the fact that XQuery is a smaller language with fewer concepts to learn, and to the fact that programs are more concise. It is also true that XQuery is more orthogonal, in that any expression can be used in any syntactic context. By contrast, XSLT is a two-language system in which XPath expressions can be nested in XSLT instructions but not vice versa.

XSLT is currently stronger than XQuery for applications that involve making small changes to a document (for example, deleting all the NOTE elements). Such applications are generally handled in XSLT by use of a coding pattern that involves an identity template that copies all nodes unchanged, modified by specific templates that modify selected nodes. XQuery has no equivalent to this coding pattern, though in future versions it will be possible to tackle such problems using the update facilities in the language that are under development.[11]

Another facility lacking from XQuery is any kind of mechanism for dynamic binding or polymorphism. The absence of this capability starts to become noticeable when writing large applications, or when writing code that is designed to be reusable in different environments. XSLT offers two complementary mechanisms in this area: the dynamic matching of template rules, and the ability to override rules using xsl:import, that make it possible to write applications with multiple customization layers.

The absence of these facilities from XQuery is a deliberate design decision: it has the consequence that XQuery is very amenable to static analysis, which is essential to achieve the level of optimization needed in database query languages. This also makes it easier to detect errors in XQuery code at compile time.

The fact that XSLT 2.0 uses XML syntax makes it rather verbose in comparison to XQuery 1.0. However, many large applications take advantage of this capability by using XSLT to read, write, or modify stylesheets dynamically as part of a processing pipeline. The use of XML syntax also enables the use of XML-based tools for managing XSLT code. By contrast, XQuery syntax is more suitable for embedding in traditional programming languages such as Java[12] or C#. If necessary, XQuery code can also be expressed in an XML syntax called XQueryX. The XQueryX representation of XQuery code is rather verbose and not convenient for humans, but can easily be processed with XML tools, for example transformed with XSLT stylesheets.[13][14]

Extensions and future work

W3C extensions

Currently, two major extensions to the XQuery are under development by the W3C:

Work on XQuery 3.0 was published as a Recommendation on 8 April 2014,[16] and work has started on XQuery 3.1.

A scripting (procedural) extension for XQuery is also being designed.[17]

The EXPath Community Group [18] develops extensions to XQuery and other related standards (XPath, XSLT, XProc, and XForms). There following extensions are currently available:

Third-party extensions

JSONiq is an extension of XQuery that adds support to extract and transform data from JSON documents. JSONiq is a superset of XQuery 3.0. It is published under the Creative Commons Attribution-ShareAlike 3.0 license.

The EXQuery[23] project develops standards around creating portable XQuery applications. The following standards are currently available:

Further reading

  • Querying XML: XQuery, XPath, and SQL/XML in context. Jim Melton and Stephen Buxton. Morgan Kaufmann, 2006. ISBN 1-55860-711-0.
  • XQuery. Priscilla Walmsley. O'Reilly Media, 2007. ISBN 0-596-00634-9.
  • XQuery: The XML Query Language. Michael Brundage. Addison-Wesley Professional, 2004. ISBN 0-321-16581-0.
  • XQuery from the Experts: A Guide to the W3C XML Query Language. Howard Katz (ed). Addison-Wesley, 2004. ISBN 0-321-18060-7
  • An Introduction to the XQuery FLWOR Expression. Dr. Michael Kay (W3C XQuery Committee), 2005.


  • BaseX: BaseX XQuery implementation
  • eXist: eXist XQuery implementation
  • MarkLogic: MarkLogic XQuery implementation
  • RaptorXML Server: Altova's XSLT and XQuery engine
  • SAXON: Michael Kay's XSLT and XQuery processor
  • Sirix: XQuery with versioning extensions
  • XQilla: XQilla xquery implementation
  • Zorba: Zorba XQuery and JSONiq processor
  • EXPath: XPath/XQuery engines, including a feature matrix
  • W3C: XQuery implementations
  • SPARQL2XQuery: SPARQL to XQuery translator
  • Galax – an open source XQuery implementation.


  1. ^ "XQuery 1.0: An XML Query Language (Second Edition)". 2010-12-14. 
  2. ^ "XQuery and Static Typing". 
  3. ^ "XML and Semantic Web W3C Standards Timeline". 2012-02-04. 
  4. ^ "XQuery 3.0 Recommendation". 2014-04-08. 
  5. ^ W3C (2003-10-25). "cited by J.Robie". 
  6. ^ Kay, Michael (May 2005). "Comparing XSLT and XQuery". 
  7. ^ Eisenberg, J. David (2005-03-09). "Comparing XSLT and XQuery". 
  8. ^ Smith, Michael (2001-02-23). "XQuery, XSLT "overlap" debated". 
  9. ^ "XQuery 3.0 requirements". 
  10. ^ Usability of XML Query Languages. Joris Graaumans. SIKS Dissertation Series No 2005-16, ISBN 90-393-4065-X
  11. ^ "XQuery Update Facility". 
  12. ^ XQuery API for Java
  13. ^ "XML Syntax for XQuery (XQueryX)". 
  14. ^ Michael Kay. "Saxon diaries: How not to fold constants". 
  15. ^ XQuery and XPath Full Text 1.0
  16. ^ XML Query (XQuery) 3.0
  17. ^ XQuery Scripting Extension 1.0 Requirements
  18. ^ EXPath Community Group
  19. ^ Packaging System
  20. ^ File Module
  21. ^ Binary Module
  22. ^ Web Applications
  23. ^ "Standard for portable XQuery applications". Retrieved 12 December 2013. 
  24. ^ [1]

External links

  • W3C XML Query (XQuery)
  • XQuery tutorial
  • XQuery API for Java (XQJ) Java Specification Request
  • hamlet.xml Hamlet in XML Format
  • XQuery (presentation - as HTML slides)
  • FunctX XQuery Functions - open source pure XQuery function library, tested with Saxon
  • Discovering XQuery Blog
  • XQuery Development Tools for Eclipse (XQDT)
  • Sirix: Beyond XQuery, providing versioning-functions on top of XQuery through Brackit.
  • BaseX: XQuery Update/Full Text implementation with realtime XQuery editor; Live Demo.
  • eXist supports an early draft of XQuery Update 1.0: XQuery Update Extensions
  • XQuery Live Demo of Zorba by the FLWOR-Foundation
  • SPARQL2XQuery: Translating SPARQL queries to XQuery
  • XQuery 3.0 Rocks Lightning Talk at FOSDEM 2012

Portions borrowed with permission from the books "XML Hacks" (O'Reilly Media) and "XQuery" (O'Reilly Media).

Previous version based on an article at the French language WorldHeritage

This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.

Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.