IntroductionLet's face it, one of the primary tasks we, as Web developers, are faced with is querying data from some data store and allowing users to view and / or manipulate the information via a Web interface. Typically, the data stores that we query from are traditional relational databases, like Microsoft SQL Server or Oracle. with relational databases, the de facto means for querying data is SQL. However, with the ever-continuing rise in the popularity of Web services, and the need for a platform-independent, Internet-transferable data representation format, XML data stores are becoming more and more popular. SQL was never designed for querying semi-structured data stores, and therefore is not suitable for querying XML data stores. (for more information on creating and using Web services IN ASP.NET Be Sure To Read: Creating and Consuming A Web Service. for More Information On Xml Be Sure To Read The Xml Faq Category on Aspfaqs.com.)
script>
So, how does one query an XML data store and retrieve results from such a query Most developers currently use XSLT and XPath to accomplish this task XPath is a syntax for accessing only parts of an XML document that meet a certain criteria;?. XSLT is A Technology That Transforms Another.
While XSLT and XPath have been in use for a while now, there is a new kid on the block: XML Query, or XQuery for short XQuery is a querying language designed specifically to work with XML data stores using a SQL-like syntax.. As of july 2003, xquery 1.0 is still under development by the w
3C
standards body. However, the core features and syntax or XQuery are solid, so now is as good a time as any to learn about XQuery, especially since Yukon, the next version of MS SQL Server, will have built-in XQuery support.In this article we will examine the XQuery syntax and examine some use cases. Following this we'll examine Microsoft's XQuery classes, which are currently available. With these classes, you can start using XQuery in your ASP.NET Web applications today!
XQuery BasicsXQuery is used to query an XML document, so, first things first, we need an XML document to talk about while examining our various queries For this article, let's use the following XML document which describes the structure of a file system.:
XML Version = "1.0" encoding = "UTF-8"?>
folder>
folder>
folder>
folder>
drive>
2003-06-01
.bak file>
2003-06-07
.bak file>
folder>
drive>
filesystem>
The root element of this XML document is
Document ("FileSystem.xml") / FileSystem / Drive [@ letter = "c"] // file
The Document ("FileSystem.xml") Part Indicates The XML Data Store: An XML File named FileSystem.xml. The Output of this Query, Given The FileSystem.xml File Above, Would Be:
The output of an XQuery statement is a collection of XML elements. In the above example, it's a collection of
With this addition, The Output Would Be:
Myfiles>
Note that in our query we used braces ({...}) around the XPath expression within the
The Output Would Have Been:
FLWR ExpressionsWhile simple XPath expressions are fine and good, the real power of XQuery shines through with FLWR expressions FLWR stands for For-Let-Where-Return, and is pronounced "flower" The FLWR expression is akin to SQL's SELECT query;.. It .
Take a moment to think about a SQL SELECT clause. The main ingredients there are the SELECT, FROM, and WHERE clauses. The FROM clause specifies the table (s) to query over. Then, for each row for the table (s) in the FROM clause, the WHERE clause is evaluated. Those rows that pass the evaluation have those fields that are specified in the SELECT clause outputted. FLWR statements are strikingly similar, as we'll see in a moment.
FLWR, AS The Acronym Implies, Has Four Parts, or Clauses, To IT:
· For - the for clause specifies the XML node list to iterate over, and is akin to the SELECT statement's FROM clause The list of XML nodes is specified via an XPath expression For example, if we wanted to iterate over all of the <.. . folder> elements, we'd use the XPath expression document ( "FileSystem.xml") // folder · where - the where clause contains an expression that evaluates to a Boolean, just like the WHERE clause in a SQL SELECT statement Each. Xml Node in The Xml Node List in The for Clause Is Evaluated by The Where Clause Expression; Those That Evaluate To True Move ON, Those That Don't are passed over.
· Return - The Return Clause Specifier The Content That Is Returned from The Flrw Expression.
You May Have Noticed That I Have Omitted The Let Clause. The Examples We'll Be Looking At this Article Will NOT USE The Let CLAUSE.
Now that we have briefly examined the three essential parts of the FLWR expressions, let's see some examples Here's a relatively straightforward example, showing how to get all
For $ mynode in document ("filesystem.xml") // folderwhere $ mynode / @ name = "quake" Return $ mynode
Notice That The for Clause Has The Following Form:
For VariablenaMe in NodeList
In XQuery, variable names are prefixed with $ (ie, $ myNode). The for clause enumerates the node list and for each node in the node list it binds the variable $ myNode to the node. Then, in the where and return clauses, $ myNode can be used to reference the current node being evaluated. So, the for clause iterates through all of the
files>
folder>
We Could AccompLish this USING The FOLLOWING XQUERY EXPRESSION:
For $ mynode in document ("FileSystem.xml") // folder
Where $ mynode / @ name = "quake"
Return
{$ mynode / file}
files>
folder>
Realize That FLWR Expressions Are Just As Powerful As SQL Select Queries. Flwrs Are Capable of Join, Subqueries, And Set-based Operations, Just Like Select Queries.
Now that we've quickly looked at the XQuery syntax, let's turn our attention to using XQuery in .NET! In Part 2 we'll see how to get Microsoft's XQuery classes and how to start using them in an ASP.NET Web application!
XQuery for .NETWhile the XQuery standard is not yet 100% complete, there are many working implementations for a variety of platforms. Microsoft provides a free XQuery engine for .NET, which is available at XQueryServices.com. In order to start using XQuery expressions in your ASP.NET Web applications, you must first download the xquery.msi file at XQueryServices.com and start the installation process.The installation process will create a new directory, C: / Program Files / XQuery Demo, which contains, among other files and subdirectories, a file named Microsoft.Xml.XQuery.dll. to use XQuery expressions in an ASP.NET Web application, simply copy this file to the / bin directory of your ASP.NET application. Next, you will want to import The Microsoft.xml.xQuery Namespace in your code-behind class. Once you have start using xquery expnesss!
XQueryServices.com Down
Since at Least July 27, 2003, XQueryServices.com Has Been Down. (See this blog entry.) In Any Event, The Word from Microsoft Is That Site Is Having Some "Issues" And Should Be Back Up Soon. In The Meantime You can download the contents of the xquys. msi file from the 4guys server here.
Recall that all XQuery expressions are performed over some XML document. The first step, then, in programmatically using XQuery expressions is to specify the XML document that will be involved in the search. To do so, we must create an XQueryNavigatorCollection instance, and add to it the XML documents that we plan on using. (Note that we can perform XQuery expressions over more than one XML document, such as in performing a join over two XML documents.) The following code demonstrates how to create an XQueryNavigatorCollection instance and add To it an XML File: DIM COL As New XQueryNavigatorCollection ()
Col.addnavigator (server.mappath (filename), alias
Alias IS A String Alias That The XML Document Will Be Referred To in The xquery expression. For example, if you have had an xquery expression like:
For $ x in document (foo) Return $ x / bar
The alias foo is being used to reference the xml document. so, in your source code, you'd want to use:
DIM col As new xquerynavigatorcollection ()
Col.addnavigator (server.mappath (filename), "foo")
To actually perform the XQuery expression, we need to create an instance of the XQueryExpression class, passing in the XQuery expression we wish to execute in to the object's constructor. Following this, we call the XQueryExpression's object's Execute () method, passing in the XQueryNavigatorCollection instance we created earlier This will return an XQueryNavigator instance We can call this object's ToXml () method to get the raw XML back The following code demonstrates creating and XQueryExpression object, running the query, and getting back the raw XML...:
DIM Query As String
Query = "for $ x in document (" foo ") // bar" & _
"Where $ x / something = 4" & _ "Return $ X / Somethingelse"
DIM EXPR AS New XQueryExpression (Query)
DIM RAWXML AS STRING = (Exp.execute (COL)). TOXML ()
[View a live demo!]
That's all there is to it! The XQuery engine download from XQueryServices.com, includes sample code showing executing an XQuery expression in both C # and VB.NET. Furthermore, it contains a plethora of XQuery expression examples.
ConclusionIn this article we examined the basics of XQuery and looked at some sample XQuery syntax and example expressions. While the XQuery expressions examined in this article were fairly simple, do not let this fool you into thinking that XQuery can only perform simple queries.
In addition to examining the basic syntax of XQuery, we also looked at how to start using XQuery with .NET. Microsoft provides a free XQuery engine at XQueryServices.com that you can use in your .NET desktop applications or in ASP.NET Web applications . Now is as good a time as any to start learning XQuery, as XQuery is bound to become more prominent as XML data stores continue their meteoric rise. Furthermore, the next version of SQL Server will have inherent XQuery support.
To Assist with Learning XQuery, you might find the folload articles helpful:
· XQuery, The Query Language of The Future
· XQuery: an XML Query Language (PDF)
Five Practical XQuery Applications
· What is xquery?
· An introduction to xquery
Happy programming!