XQuery, the Query Language of the Future

XQuery, the Query Language of the Future

Query will likely become the dominant language for querying data from most data sources. Although designed for querying XML data, you can use XQuery to tie together data from multiple data sources. In that respect it is much more powerful than SQL, which will slowly but surely be replaced as the main query language.

You may think I’m making a pretty bold statement here considering the current dominance of SQL with its many dialects, and the fact that XQuery has yet to become a W3 Consortium (W3C) Recommendation. However, there are already several implementations based on working drafts, including one by Microsoft included in the .NET Framework. These implementations show the enormous power of XQuery. But what is XQuery exactly?

XML Query, XQuery for short, is a new query language currently under development by the W3C. It is designed to query XML documents using a SQL-like syntax. XQuery’s capabilities go far beyond SQL however, because XML (and thus XQuery) isn’t bound to the rigid structure of tables and relations. XML can represent a large number of data models. Furthermore an XQuery query can return data from multiple documents in different locations. XSLT has similar capabilities, but many IT people will find XQuery much easier to understand, particularly database administrators familiar with SQL.

The designers of XQuery want you to use it as a unified query language for any data store, including XML files, XML databases, and non-XML data stores. Keep in mind that most implementations are experimental and in the technology preview stage.

You can use XQuery to extract an XML document from a physical or virtual representation of XML data. An example of the latter is SQLXML (provided in Microsoft SQL Server 2000), which enables you to extract data from a SQL Server database formatted as XML using the HTTP protocol. Any system that exposes XML over HTTP is a potential source of data for XQuery. XQuery’s designers hope that XQuery can act as a unified query language for any data store, including XML files, XML databases, and non-XML data stores. With the proliferation of loosely coupled systems and data coming from half way across the globe, performance of multi-document queries is going to be an issue, particularly if you only need a small amount of data from a large document. Future versions of XQuery may alleviate this problem by distributing a query over the queried systems.

Although XQuery is still a working draft it already has broad support. There are several applications providing the ability to query using XQuery. Microsoft has already hinted that the next release of SQL Server (codename Yukon) will provide support for XQuery as well, and both IBM and Oracle will likely offer some kind of XQuery support once XQuery attains W3C Recommendation status.

XQuery Basics
XQuery uses four main keywords to create query expressions: FOR, LET, WHERE, and RETURN. These keywords are commonly used in conjunction to query data and create a result. People familiar with XQuery who build an expression using these keywords refer to this as a FLWR-expression (or FLoWeR-expression). In technical terms, these expressions are element constructors?you use them to construct (sequences of) elements. Let’s start with a simple expression to show you how this works.

   FOR $d IN document("menu.xml")//dish   RETURN            {$d/text()}     

Note that the expression above is itself not well-formed, and thus is not XML. Most W3C standards that deal with XML are usually specified in XML format (XSLT, for example). XQuery represents a departure from this standard. The W3C choose to abandon the need for XQuery to be XML in favor of a simplified model. When you apply the expression above to menu.xml shown in Listing 1, it yields the following result:

   Crab Cakes   Jumbo Prawns   Ceasar Salad   Grilled Salmon   Linguini al Pesto   Rack of Lamb   Dame Blanche   Sorbet   Banana Split

The above result is a sequence of nodes, and as such is not well-formed XML (it has no root element). Hence both the query and the result need not necessarily be XML. A sequence is ordered, so unlike SQL which is set-based, all data returned has a specific order. Returned data can be in the order in which the nodes appear in the source document (document order), but you can also manipulate the order. So how did the data actually get retrieved?

First, the document() function opens the menu.xml file and the XPath expression //dish retrieves all dish nodes in the source document. Second, the FOR keyword iterates through the sequence and assigns a node to the $d variable with each iteration. The expression uses RETURN to build a result for each of the nodes based on the $d variable. In this case, all the RETURN statement does is create a dish element with the text from the original dish element. The curly braces make sure that the XPath expression is evaluated to give a result instead of just showing it as is in the result. The result is very similar to a SQL SELECT query on a simple table. Before we break out of that similarity, let’s look at how you can refine the query to select only certain elements by adding WHERE to the mix.

   FOR $d IN document("menu.xml")//dish   WHERE $d/@id>'6'   RETURN            {$d/text()}     

When you use the expression above, you actually limit the result to only the desserts from Listing 1, as shown below:

   Dame Blanche   Sorbet   Banana Split

The WHERE clause more or less acts the same as a SQL WHERE clause. You can add more criteria with AND and OR clauses, limiting the selection even further. Note that the id-attribute is actually compared to a string rather than a number. This is because XQuery uses XPath 2.0 (although it still looks like XPath 1.0 in this sample), which is still a little funky in most implementations. In the .NET implementation used here, the implicit conversion from string to number doesn’t work properly yet. Also only a limited number of the XPath 2.0 functions work at this point. If you start experimenting with XQuery, you’re bound to run into situations where your query is correct according to the specification, but still doesn’t work. Just keep in mind that most implementations are experimental and in the technology preview stage. Once the W3C recommends XQuery I expect to see pretty stable beta implementations very quickly, because none of the major vendors want to lag behind.

One problem with the WHERE expression I presented in my example is that it performs its filtering after the data has been selected. With large documents this is clearly inefficient, although in some cases unavoidable. Limiting the selection by changing the XPath expression in the FOR clause should theoretically result in better performance because it limits the number of iterations. The following query yields the same result, but I’ve removed the WHERE clause and I’ve added a filter to the XPath expression:

   FOR $d IN document("menu.xml")      //dish[$d/@id>'6']   RETURN            {$d/text()}     

You’ll also use XPath filtering when you use the LET keyword. With LET you can assign data to a variable that you can later manipulate. The following (somewhat meaningless) example shows how this works:

   LET $x := document("menu.xml")/menu/desserts/dish   FOR $d IN $x   RETURN            {$d/text()}     

The above query again yields all the desserts from Listing 1, but instead of the FOR expression iterating over data directly pulled from menu.xml, it acts on the $x variable that was filled with the LET expression. Obviously you can use LET in more useful ways than I’ve shown here. It enables you to select data that you can re-use inside an iteration and join/merge that data with another specified data set. The object of the above sample was just to show you how LET operates.

With the FLWR-expressions under your belt you can start to create documents with XQuery instead of just a sequence of nodes. You can do so by embedding one or more queries in XML using curly braces, as shown in Listing 2. Each expression within the curly braces is executed separately. As you can imagine, the result is just a simple HTML page that shows the appetizers, entrees, and desserts from menu.xml separated by a header. You can easily make it more complex by adding formatting outside the FLWR-expressions, and by adding formatting to the HTML built into the RETURN clause of each expression. As long as the RETURN clause is well-formed, you can pretty much add any element and attribute you want.

XQuery in .NET
Microsoft offers XQuery demo classes for the .NET Framework through http://xqueryservices.com. You can either use the online demo with several of the XQuery User Cases (see sidebar), or download the classes and experiment with them yourself. Note that this is just a demo implementation. The namespace and the classes are likely to change for the actual implementation. The overall structure and approach, however, will probably stay the same.

Most W3C standards that deal with XML are usually specified in XML format (XSLT, for example). XQuery represents a departure from this standard.

Executing a query consists of two steps: loading the document(s), and executing the query over the loaded documents, as shown in Figure 1. The documents you load are stored in an XQueryNavigatorCollection object. You can either load a file directly into the collection, or load an XQueryDocument using an XmlReader object, and from there use the CreateNavigator() method to create an XQueryNavigator object that you store in the collection. The result is a collection of documents optimized for XQuery. When you execute a query, you feed the collection to the XQueryExpression object executing the query. An advantage of this approach is the ability to work with both physical and virtual documents because the documents are loaded into the collection using an alias. To refer to a document in the query, you refer to the alias. If this wasn’t possible, you could only work on physical documents, because, for instance, there is no way to address data in a database.

devx-admin

devx-admin

Share the Post:
Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India,

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with

Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in and explore the leaders in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India, and kickstart your journey to

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner for your online project. Your

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the state. A Senate committee meeting

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor supply chain and enhance its

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with minimal coding. These platforms not

Cybersecurity Strategy

Five Powerful Strategies to Bolster Your Cybersecurity

In today’s increasingly digital landscape, businesses of all sizes must prioritize cyber security measures to defend against potential dangers. Cyber security professionals suggest five simple technological strategies to help companies

Global Layoffs

Tech Layoffs Are Getting Worse Globally

Since the start of 2023, the global technology sector has experienced a significant rise in layoffs, with over 236,000 workers being let go by 1,019 tech firms, as per data

Huawei Electric Dazzle

Huawei Dazzles with Electric Vehicles and Wireless Earbuds

During a prominent unveiling event, Huawei, the Chinese telecommunications powerhouse, kept quiet about its enigmatic new 5G phone and alleged cutting-edge chip development. Instead, Huawei astounded the audience by presenting

Cybersecurity Banking Revolution

Digital Banking Needs Cybersecurity

The banking, financial, and insurance (BFSI) sectors are pioneers in digital transformation, using web applications and application programming interfaces (APIs) to provide seamless services to customers around the world. Rising

FinTech Leadership

Terry Clune’s Fintech Empire

Over the past 30 years, Terry Clune has built a remarkable business empire, with CluneTech at the helm. The CEO and Founder has successfully created eight fintech firms, attracting renowned

The Role Of AI Within A Web Design Agency?

In the digital age, the role of Artificial Intelligence (AI) in web design is rapidly evolving, transitioning from a futuristic concept to practical tools used in design, coding, content writing

Generative AI Revolution

Is Generative AI the Next Internet?

The increasing demand for Generative AI models has led to a surge in its adoption across diverse sectors, with healthcare, automotive, and financial services being among the top beneficiaries. These

Microsoft Laptop

The New Surface Laptop Studio 2 Is Nuts

The Surface Laptop Studio 2 is a dynamic and robust all-in-one laptop designed for creators and professionals alike. It features a 14.4″ touchscreen and a cutting-edge design that is over

5G Innovations

GPU-Accelerated 5G in Japan

NTT DOCOMO, a global telecommunications giant, is set to break new ground in the industry as it prepares to launch a GPU-accelerated 5G network in Japan. This innovative approach will

AI Ethics

AI Journalism: Balancing Integrity and Innovation

An op-ed, produced using Microsoft’s Bing Chat AI software, recently appeared in the St. Louis Post-Dispatch, discussing the potential concerns surrounding the employment of artificial intelligence (AI) in journalism. These

Savings Extravaganza

Big Deal Days Extravaganza

The highly awaited Big Deal Days event for October 2023 is nearly here, scheduled for the 10th and 11th. Similar to the previous year, this autumn sale has already created

Cisco Splunk Deal

Cisco Splunk Deal Sparks Tech Acquisition Frenzy

Cisco’s recent massive purchase of Splunk, an AI-powered cybersecurity firm, for $28 billion signals a potential boost in tech deals after a year of subdued mergers and acquisitions in the

Iran Drone Expansion

Iran’s Jet-Propelled Drone Reshapes Power Balance

Iran has recently unveiled a jet-propelled variant of its Shahed series drone, marking a significant advancement in the nation’s drone technology. The new drone is poised to reshape the regional

Solar Geoengineering

Did the Overshoot Commission Shoot Down Geoengineering?

The Overshoot Commission has recently released a comprehensive report that discusses the controversial topic of Solar Geoengineering, also known as Solar Radiation Modification (SRM). The Commission’s primary objective is to

Remote Learning

Revolutionizing Remote Learning for Success

School districts are preparing to reveal a substantial technological upgrade designed to significantly improve remote learning experiences for both educators and students amid the ongoing pandemic. This major investment, which

Revolutionary SABERS Transforming

SABERS Batteries Transforming Industries

Scientists John Connell and Yi Lin from NASA’s Solid-state Architecture Batteries for Enhanced Rechargeability and Safety (SABERS) project are working on experimental solid-state battery packs that could dramatically change the

Build a Website

How Much Does It Cost to Build a Website?

Are you wondering how much it costs to build a website? The approximated cost is based on several factors, including which add-ons and platforms you choose. For example, a self-hosted

Battery Investments

Battery Startups Attract Billion-Dollar Investments

In recent times, battery startups have experienced a significant boost in investments, with three businesses obtaining over $1 billion in funding within the last month. French company Verkor amassed $2.1

©2023 Copyright DevX - All Rights Reserved. Registration or use of this site constitutes acceptance of our Terms of Service and Privacy Policy.

Sitemap