This article describes the main features of the XQuery query language and discusses the application of xQuery language in data query, conversion.
Introduction XML has become a standard in fact data representation and data exchange. Start, core features such as self-description and specific scalability provide the flexibility required for messaging between different applications. Especially with the vigorous development of Web Service in recent years, XML is increasingly active in data exchange and storage, and the semi-structural data expressed by XML is increasingly common, especially in the B2B e-commerce industry. In the face of an exponential growth of XML data, it is inevitable to require more efficient data management capabilities and faster and more accurate queries. Therefore, how to accurately query the information required from the XML data source, it will become more and more important.
I. XQuery Overview The XQuery Working Group was formally established in September 1999. It was a flexible query language to extract data from the XML document. The latest XQuery draft announced by W3C is the version of November 12, 2003, which is still in constant revision and perfect [1]. As a new type of query language, XQuery draws the advantages of several other query languages, suitable for queries of various types of XML data sources, not only querying power, but also simple and flexible and easy to implement. Moreover, XQuery also has the characteristics of retrieving information from a variety of databases, which can query various data and documents.
XQuery is built on the XPath specification, which is capable of selecting a special node sequence from the document via the XPath expression. XQuery is a functional language that represents a query as an expression. Through a variety of expressions it support, it can have a variety of different forms. Various XQuery expressions can be completely nested, also support sub-queries. At present, the three mainstream manufacturers of the database industry Oracle, IBM, and Microsoft have provided support for the XQuery specification in their respective products.
Second, XQuery and its application examples
1. Query Application XQuery has a powerful query and retrieval function, which completes queries by various expressions, symbols, operands, and its expressive operation objects can be another expression. As a function language, it also allows various expressions to nested each other. At the same time, it is also a strict language that requires strict data. The operand, operators, and functions in the expression must be specified [2].
First, look at an example of XML document BIB.xml that states book information.
China-Pub
Java programming ideas
Mechanical Industry Press
2002
Bruce
Eckel
99
XML manual
Electronic Industry Press
2003
Charles F.
Goldfarb
69
......
1. 1 XPath
XPath is a language that can find location information in an XML document that extracts a single project or a set of items from an XML file. XPath is similar to the file path we used in your computer system, just like the C: / Winnt we are well known. By XPath path expression, you can easily locate data in an XML document to determine nodes. For example, this basic type of XPath expression (corresponding to the XML document above): / bib / vendor / @ ID. This expression starts from the document root, selects all BIB sub-elements, then select all the Vendor child elements of the BIB, and finally select all ID attributes of the Vendor child element. Of course, the ability to XPath expressions is far exceeded by this simple statement.
Using XPath quickly locates and extracts information in the XML hierarchy, and its built-in function provides a comprehensive function that can be easily handled value and text data.
The following is a typical XPath path expression: (with "/" to start representing an absolute path of the element)
Path Expression Select XML Document section / Select the root node of the XML document / * Select all child nodes of the root node, * Match all BIB elements of the node / bib to select the root node // Book Select root node Book Elements in All Backhills Nods @ ID Select sub-node / BIB / BOOK [2] containing ID attributes Select BIB's second child // vendor [@ id = 'id1_2'] / book selection compliance with "Properties ID All BOOK Elements of 'Id1_2' "/ BIB / VENDOR / BOOK [Year> 2002] Select all BOOK elements that meet" Element Year> '2002' ", of course, XPath can also achieve many other features, please see W3C XPath specification [3].
1. 2 FLWR expression
The most powerful feature in xQuery is FLWR expression (pronounce flower), which is a typical expression that enables a query with some actual meaning. FLWR expressions include pattern matching, filter selection, and results constructing these three operations. The FLWR statement is the closer SQL statement with XQuery. Using the FLWR statement, you can create a specific query with more natural methods than XPath 1.0 statements. [5]
The FLWR expression is made up of the clause defined by the for-let-where-return, updated to FLWOR, O represents the newly added Order By clause in the latest standard.
FLWOR expression represents the first letters of For-let-where-order by- return. The resulting FLWOR expression can complete a lot of tasks that are difficult to complete in XSLT. It supports iteration and can bind variables to intermediate results. This expression is very useful when connecting and reconstructing data for two or more documents. Each FLWOR expression has one or more For clauses, one or more LET clauses, an optional WHERE clause, an ORDER BY clause, and a return clause. The FOR clause binds the node to the variable to continue to go to each node in the cycle sequence; the Let clause is a variable to assign a value or a sequence; the return clause defines the content to return each tuple; The WHERE clause is true if its valid Boolean value is true, then the group is reserved, and its variable binding is used in the return clause, and if its valid Boolean value is false, the group is discarded.
Here is a simple example:
For $ i in document ("bib.xml") / BIB / vendor / book
Where $ I / Price> 80
Return
{$ I / TITLE, $ I / Price}
This example is not used to let statements, it is only optional. It should be noted that the variables are started with symbol $, which are bound to different node sequences and then passed through the statement. The flower bracket {} represents the output information and the information to be evaluated. It can be seen that the FLWOR expression is a type type with a variety of changes that generate a large number of different query instances. The "return" keyword behind the object itself can be replaced by another FLWOR expression, which can constantly connect the FLWOR expression, so that XQuery has a very rich expression.
Its query results are:
Java
99
1.3 Other features
In addition to path expressions and FLWR expressions, XQuery has five basic expression modes: element constructor, operator, and function expressions, conditional expressions, defined expressions, list constructs, data type expressions [ 4]. By a variety of combinations, a query statement with rich and powerful query search function can be generated.
Below is an example of using an arithmetic operator ' ':
Let $ a: = document ("bib.xml") / bib / vendor / book [1] / price ("bib.xml") / BIB / VENDOR / BOOK [2] / Price
Return
{$ A $ B}
The results of the query are as follows:
168
In the query statement, you can also use if ... then ... else ... such a selection structure, such as the following example:
For $ i in document ("bib.xml") / BIB / vendor / book
Return
IF ($ I / Price <70)
THEN
{$ I / TITLE, $ I / Price}
Else
{$ I / TITLE, $ I / Price}
The results of the query are as follows:
Java
99
XML Handbook
69
2. Conversion Applications When using integration, a variety of vocabulary can be used, and if these applications are interactive, including data exchange, it is necessary to convert XML documents for different phrases. At this time, XQuery can adapt the demand because it has a lot of functions such as XSLT to convert data from potential heterogeneous data sources.
Let's take a look at an example of using XQuery to implement document conversion, which converts the properties of the XML document to an element display. This is an original XML document as an input, which describes the order item.
Below is the XQuery statement used to implement the conversion function. It is just simply changing the structure of the original document, plus some elements.
Let $ ORDER: = Document ("Order.xml") / ORDER
Return
{$ ORDER / @ id}
{
For $ item in $ Order / Item
Return
{String ($ item / @ id)}
{String ($ item / @ qty)}
}
The result of the conversion is as follows:
111
10
123
Twist
135
46
In this example, when multiple applications using XML data uses different vocabulary, simplifying the problem, when they use different DTD (Schema), this is likely to happen, due to different applications based on different The actual considerations are designed to use Element or attributes when using XML to express certain features. This involves the conversion of the document.
Of course, in the actual demand, the conversion between the vocabulary is probably more complicated than this example, but from this example we can see that the powerful conversion function of XQuery is enough to express most of this requirement. Because XQuery has a very flexible construction of the results tree mechanism, it has considered the structural conversion when designing, so in some cases it makes a conversion work than the XSLT is more concise.
3. Update Applications Although the latest XQuery 1.0 standard also does not support update operations, this research is developing very quickly, and some Native XML database products such as Tamino, Ipedo has been updated in their respective products. Moreover, another organization XML: DB also proposes a draft xupdate language. Obviously, XQuery is not perfect in this regard than the SQL / XML can be easily updated. This is also the most obvious gap between XQuery and SQL / XML. The syntax for updating the elements in the XML document has not been defined in XQuery. The update mechanism for SQL / XML has been defined and has been applied to Oracle and DB2.
Third, the comparison of query language From the previous example, especially the FLWR structure, we can see that XQuery has the appearance and ability of SQL, which is welcome to the user who is familiar with the relational database. So what is new for SQL standards in the database industry for XML query requirements. We may wish to compare SQL / XML and XQuery with XML query language standards. As part of the latest SQL: 2003 standards raised in December 2003, SQL / XML is based on the extension of relational query language, and has especially added support for XML. Although SQL / XML and XQuery are XML query criteria, they are based on completely different models, SQL / XML is more suitable for relational models, which can provide full access in existing SQL environments, including xQuery1.0 standards are not prepared Provided features such as updates and full text queries. In addition, the advantage of SQL / XML is that database vendors have accumulated many years of experience in SQL's query optimization. It can be said that SQL / XML provides functions that can support relational data in existing SQL environments while creating XMLs from relational data.
For queries, SQL / XML, and XQuery reach similar effects only based on relational data. However, their implementation mechanisms for the same function may be completely different, because SQL / XML operation is between SQL and XML, and XQuery is based on pure XML, it is from XML-centered angle. Expected questions.
XQuery is best at handling the "mix" document, which contains documents that describe the flow and quantization data. Therefore, XQuery shows more important advantages for queries across relational databases and other XML data sources.
It seems that they are complementary. SQL / XML and XQuery standards are hoped to join the other part of the other party in the standard. However, from a long time, developers may wish to keep XML abstractions on all of their data sources. In this case, as a rich and comprehensive programming language developed for processing complex data, XQuery may become an important standard.
IV. Summary now, the XQuery standard is still growing and perfect, in general, XQuery is the best choice for XML programmers who need to process XML data or need to process XML and relational data, but It is equally insufficient, and relatively mature SQL, its technical support and experience should be less. We can affirm that whether SQL / XML or XQuery will play an important role in XML queries, and XQuery will become an important standard in general XML processing.
Reference
http://www.w3.org/xml/query http://www.w3.org/tr/xquery http://www.w3.org/tr/xpath xquery: a guided tour http: // www. DataDirect.com/news/whatsnew/xquerybook/index.ssp 2003.9
About the author Li Xia, studying graduate student, Wuhan University of Technology Computer Science and Technology, Wuhan 430063 E-mail: Joylee4u@yahoo.com.cn Tel: 027-86568023; 13667290779