A specification for a query language which enables a user or programmer to extract information from an Extensible Markup Language (XML) file or any collection of data that can be XML-like is XQuery. Its syntax is intended to be easy to use as well as understand. However, there is much more to know about XQuery.
So, let’s move further towards the detailed study of XQuery. Though, before learning XQuery, we must have the basic knowledge of XML. Therefore, let’s begin with the introduction to XML in brief.
Overview of XML
XML is short for Extensible Markup Language. It is nothing but a text-based markup language derived from Standard Generalized Markup Language (SGML).
Moreover, instead of only specifying how to display it like HTML tags, XML tags identify the data and are also used to store and organize the data, which are used to display the data. XML introduces new possibilities by adopting many successful features of HTML still it's not going to replace HTML in the future.
Now, we can learn about XQuery. So let’s begin with the introductory guide to XQuery.
What is XQuery?
A functional language which we use to retrieve information stored in XML format is what we call XQuery. We can use it on XML documents, relational databases containing data in XML formats, or XML Databases. Since April 8, 2014, XQuery 3.0 is a W3C recommendation.
In other words, a functional query language which we use to retrieve information stored in XML format is XQuery. We can say, what SQL is for databases, the same is XQuery for XML. XQuery was mainly designed to query XML data.
XQuery is built on XPath expressions. Additionally, this is a W3C recommendation which is supported by all major databases.
Well, the definition of XQuery on its official documentation is as follows:
"XQuery is a standardized language for combining documents, databases, Web pages and almost anything else. It is very widely implemented. It is powerful and easy to learn. XQuery is replacing proprietary middleware languages and Web Application development languages. XQuery is replacing complex Java or C++ programs with a few lines of code. XQuery is simpler to work with and easier to maintain than many other alternatives."
There are various types of nodes in XQuery, such as attribute, element, text, namespace, processing-instruction, document (root) nodes, and comment. Additionally, XML documents are treated as trees of nodes. And, the root of the tree is known as a document node.
Nodes with no children or parent and an instance of one of the built-in atomic data types defined by XML Schema are Atomic Values.
And, atomic values or nodes that are XQuery specific data types are called Items.
What is XQuery Used for?
Originally, XQuery was devised as a query language for data stored in XML form. That shows its key role is to get information out of XML databases which include relational databases that store XML data or present an XML view of the data they hold.
Also, we can use XQuery for manipulating free-standing XML documents. For instance, toward transforming messages passing between applications. Especially, here, XQuery competes directly with XSLT, and choosing a language is largely a matter of personal preference.
Although we can say, some people are also using it for rendering XML into HTML for presentation. And this shows its huge popularity as well. However, this is not the job XQuery was designed for, but people tend to find new ways of using it.
What Does XQuery Do?
As we know, XQuery is a functional language which we use for finding and extracting elements and attributes from XML documents. And, apart from that, it has several examples of use such as:
- To extract information to use in a web service
- To generate summary reports
- To transform XML data to XHTML
Characteristics of XQuery
- Functional Language: To retrieve/querying XML based data.
- Analogous to SQL: What SQL is to databases, XQuery is to XML.
- XPath based: To navigate through XML documents, XQuery uses XPath expressions.
- Universally accepted: All major databases support XQuery.
- Expression-oriented programming language: XQuery is an Expression-oriented programming language with a simple type system.
- W3C Standard: It is a W3C standard.
XQuery is About Querying XML
For finding and extracting elements and attributes from XML documents, XQuery language is derived.
XQuery Basic Syntax Rules
Here, we are listing some basic syntax rules:
- The elements, variables, and attributes of XML must be valid XML names.
- The string value, in an XQuery, can be in single or double quotes.
- Also, it is necessary that a name followed with a $ while an XQuery variable is defined, such as the $bookstore.
- Moreover, its comments are delimited with (: and :). For example- (: XQuery Comment :)
XQuery and XPath
There are the same functions and operators and the same data model in both XQuery 1.0 and XPath 2.0. Knowing XPath already when learning XQuery is very beneficial.
Transforming XML Documents Using XQuery
XSL stylesheets and XQuery are quite similar. Both can transform an XML input into another format. And, when we define the transformation scenario, we need to specify the input URL. It is possible to save and open results in the associated application. We can even run an FO processor on the output of an XQuery.
Between many XQuery files, the transformation scenarios may be shared, and are also exported together with the XSLT scenarios.
Further, they can be managed in the Configure Transformation Scenario dialog box or the Scenarios view. The transformation can be executed on the XML document defined in the XML URL field, or the documents referenced from the query expression if this field is empty.
XQuery parameter transforms must be set in the Parameters dialog box. Additionally, the parameters which are in a namespace must be defined using the qualified name.
XQuery FLWOR Expressions
The pronunciation for FLWOR is "flower." Here F stands for "For,” L for “Let,” W for “Where,” O for “Order by,” R for “Return." It is defined as:
For - It selects a sequence of nodes.
Let - This binds a sequence to a variable.
Where - It filters the nodes.
Order by - This sorts the nodes.
Return - Finally, what to return gets evaluated once for every node.
1. XQuery vs. XPath
A functional programming & query language which we use to query a group of XML data is what we call XQuery.
An XML path language which we use to select nodes from an XML document using queries is what we call XPath.
To extract and manipulate data from either XML documents or relational databases and MS Office documents which support an XML data source, we use it.
To compute values like numbers, strings, and boolean types from other XML documents, we use it.
In the form of a tree model with seven nodes, namely processing instructions, elements, document nodes, attributes, namespaces, text nodes, and comments, XQuery is represented.
And, as the tree structure navigates it by selecting different nodes, XPath is represented.
It supports XPath as well as extended relational models.
It is a component of the query language.
To create syntax for new XML documents, XQuery helps.
To define a common syntax and behavior model for XPointer and XSLT, XPath was created.
2. XQuery vs. XSLT
It is Programme-driven.
XSLT is document-driven.
It is declarative.
It is functional.
It is not written in XML but developed by the XML Query working group of the W3C.
It is written in XML.
We use it for simple Transformations.
It is the language which was specially designed to process tree structures.
It is considered easier to learn.
Comparatively, it is difficult to learn.
Some Advantages of XQuery
No wonder that using XQuery is beneficial for all. So, here we are listing some top advantages of XQuery, such as:
- It is possible to retrieve both hierarchical and tabular data with XQuery.
- We can use XQuery to query graphical & tree structures.
- To transform XML documents into XHTML documents, we can use XQuery.
- Moreover, it enables us to work in one common model no matter what type of data we are working with: XML, relational, or object data.
- For queries which represent results as XML, to query XML stored inside or outside the database, as well as to span relational and XML sources, XQuery is ideal.
- Also, it enables us to create many different types of XML representations from the same data.
- By using XQuery, we can query both relational sources and XML sources. Also, one can create one XML result.
- We can directly use XQuery to query web pages.
- One can build web pages using XQuery.
- It is possible to transform XML documents via XQuery.
For XML-based databases and object-based databases, XQuery is ideal. Well, in comparison to purely tabular databases, object databases are much more flexible and powerful.
Hence, we have seen that XQuery acts like an expression language because it specifies the exact flow of data and operations to achieve the desired result.
And we have seen that it is a specification for a query language which enables a user or programmer to extract information from an Extensible Markup Language (XML) file and also any data collection that can be XML-like. Even its syntax is intended to be easy to use as well as understand.
So, this is all about XQuery introduction. Hope it helps!