Kolmapäev, juuli 29, 2026

Ajalugu, Täna ajaloos

TÄNA AJALOOS, 11. juuni ⟩ Hävitati Trooja linn

XQuery (XML Query) is a query language and functional programming language designed to query and transform collections of structured and unstructured data, primarily in the form of XML. It also supports text data and, through implementation-specific extensions, other formats like binary and relational data.

The language was developed by the XML Query working group of the W3C, with version 1.0 becoming a W3C Recommendation in January 2007. XQuery development is closely coordinated with the development of XSLT by the XSL Working Group. Both groups jointly maintain XPath, a shared component of XQuery and XSLT. XQuery extends XPath with features like FLWOR (For, Let, Where, Order by, Return) expressions, making it semantically similar to SQL but optimized for hierarchical rather than relational data.

XQuery 3.1, published in March 2017, added support for JSON and introduced maps, arrays, and additional higher-order functions, significantly expanding the language's capabilities for modern data processing.

XQuery is implemented by many database systems, XML databases, and XML processors, including BaseX, eXist, MarkLogic, Saxon, and Berkeley DB XML, making it a cornerstone technology for processing XML data in enterprise software applications.

Features

XQuery's mission is to:

Examples

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

<html>
<body>
{
  for $act in doc("hamlet.xml")//ACT
  let $speakers := distinct-values($act//SPEAKER)
  return
    <div>
      <h1>{ string($act/TITLE) }</h1>
      <ul>
      {
        for $speaker in $speakers
        return <li>{ $speaker }</li>
      }
      </ul>
    </div>
}
</body>
</html>

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.

Applications

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

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

XQuery and XSLT compared

Scope

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.

Origin

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[according to whom?] in its handling of narrative documents with more flexible structure, while XQuery is stronger in its data handling (for example, when performing relational joins).

Versions

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 still 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.[9][10][11] Many of these features were planned for XQuery 3.0.[12]

Any comparison must take into account the version of XSLT. 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.[13] 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.[14]

XQuery 1.0 lacked any kind of mechanism for dynamic binding or polymorphism; this has been remedied with the introduction of functions as first-class values in XQuery 3.0. 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.[citation needed] 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 1.0 was 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 (see XQuery API for Java) 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.[15][16]

Versions and extensions

Versions

W3C extensions

The World Wide Web Consortium (W3C) developed two major extensions to XQuery:

  • XQuery 1.0 and XPath 2.0 Full-Text,[20] which extends XQuery with full-text search capabilities
  • XQuery Update Facility, which enables data modification in XQuery

Both became W3C Recommendations as extensions to XQuery 1.0. Efforts to adapt them for XQuery 3.0 were abandoned due to resource constraints.

A scripting (procedural) extension for XQuery was proposed but never completed.[21][22]

The EXPath Community Group[23] develops extensions for XQuery and related standards (XPath, XSLT, XProc, and XForms). The following extensions are available:

  • Packaging System,[24] for managing XQuery libraries and modules.
  • File Module,[25] for file system operations.
  • Binary Module,[26] for handling binary data.
  • Web Applications,[27] for building web-based applications

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.

XQuery 3.1 de facto deprecates JSONiq as it has added full support for JSON.

The EXQuery[28] 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.
  • Walmsley, Priscilla (2007). XQuery, 1st Edition. O'Reilly Media. ISBN 978-0-596-00634-1.
  • Walmsley, Priscilla (2015). XQuery, 2nd Edition. O'Reilly Media. ISBN 978-1-4919-1510-3.
  • 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.

Implementations

Overview of popular XQuery implementations
Name License Language XQuery 3.1 XQuery 3.0 XQuery 1.0 XQuery Update 1.0 XQuery Full Text 1.0
BaseX BSD license Java YesYesYesYesYes
eXist LGPL Java PartialPartialYesNoNo
MarkLogic Proprietary C++ NoPartialYesNoNo
Saxon HE Mozilla Public License Java PartialPartialYesYesNo
Saxon EE Proprietary Java YesYesYesYesNo
Xidel GPLv3+ FreePascal YesYesYesNoNo
Zorba Apache License C++ NoYesYesYesYes

See also

References

  1. "XQuery 3.1 Recommendation". 2017-03-21.
  2. "XQuery 3.1: An XML Query Language". 2017-03-21.
  3. "XQuery and Static Typing". 3 April 2023.
  4. W3C (2003-10-25). "cited by J.Robie".{{cite web}}: CS1 maint: numeric names: authors list (link)
  5. Kilpeläinen, Pekka (2012). "Using XQuery for problem solving" (PDF). Software: Practice and Experience. 42 (12): 1433–1465. doi:10.1002/spe.1140. S2CID 15561027. Archived from the original (PDF) on 2016-03-04. Retrieved 2015-08-29.
  6. "Data retrieval with XQuery". Retrieved on 18 January 2016.
  7. 1 2 3 4 "XQuery Tutorial". www.w3schools.com. Retrieved 2025-11-06.
  8. 1 2 "XQuery and XPath Full Text 3.0". www.w3.org. Retrieved 2025-11-06.
  9. Kay, Michael (May 2005). "Comparing XSLT and XQuery". Archived from the original on 2018-06-27. Retrieved 2016-06-15.
  10. Eisenberg, J. David (2005-03-09). "Comparing XSLT and XQuery".
  11. Smith, Michael (2001-02-23). "XQuery, XSLT "overlap" debated". Archived from the original on 2006-06-16. Retrieved 2006-06-19.
  12. "XQuery 3.0 requirements".
  13. Usability of XML Query Languages. Joris Graaumans. SIKS Dissertation Series No 2005-16, ISBN 90-393-4065-X
  14. "XQuery Update Facility".
  15. "XML Syntax for XQuery (XQueryX)".
  16. Michael Kay. "Saxon diaries: How not to fold constants".
  17. "XML and Semantic Web W3C Standards Timeline" (PDF). 2012-02-04. Archived from the original (PDF) on 2019-10-26. Retrieved 2012-02-04.
  18. "XQuery 3.0 Recommendation". 2014-04-08.
  19. "XQuery 3.1 Recommendation". 2017-03-21.
  20. XQuery and XPath Full Text 1.0
  21. XQuery Scripting Extension 1.0 Requirements
  22. XQuery 1.0 Scripting Extension
  23. EXPath Community Group
  24. Packaging System
  25. File Module
  26. Binary Module
  27. Web Applications
  28. "Standard for portable XQuery applications". Retrieved 12 December 2013.
  29. "RESTXQ 1.0: RESTful Annotations for XQuery".

Lisa kommentaar

Sinu e-postiaadressi ei avaldata. Nõutavad väljad on tähistatud *-ga