分享
 
 
 

XQuery

王朝asp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

Introduction

Let'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.)

[/url]

So, how does one query an XML data store and retrieve results from such a query? Most developers currently use [url=http://aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=203]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 an XML document from one form to 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 W3C 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 Basics

XQuery 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" ?>

<filesystem>

<drive letter="C">

<folder name="My Programs" />

<folder name="Games">

<folder name="Quake">

<folder name="SavedGames">

<file>game1.sav</file>

<file>game2.sav</file>

</folder>

<file>quake.exe</file>

<file>README.txt</file>

<file>EULA.txt</file>

</folder>

</folder>

<folder name="Windows">

<folder name="System32" />

<file>win.exe</file>

</folder>

</drive>

<drive letter="D">

<folder name="Backup">

<file>2003-06-01.bak</file>

<file>2003-06-07.bak</file>

</folder>

</drive>

</filesystem>

The root element of this XML document is <filesystem>, which contains an arbitrary number of <drive> elements. Each <drive> element, in turn, contains an arbitrary number of <folder> elements, and each <folder> element contains an arbitrary number of <folder> elements and <file> elements.

In its simplest form, an XQuery query can simply be an XPath expression. (If you're unfamiliar with XPath, I would strongly encourage you to work through this XPath tutorial before continuing.) For example, if we wanted to get a list of all of the files in the C drive, we could use the following XPath expression as our XQuery query:

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:

<file>game1.sav</file>

<file>game2.sav</file>

<file>quake.exe</file>

<file>README.txt</file>

<file>EULA.txt</file>

<file>win.exe</file>

The output of an XQuery statement is a collection of XML elements. In the above example, it's a collection of <file> elements. You can add static XML elements by just inserting them in the XQuery query. For example, in the above example, perhaps we want all of the <file> elements to appear within an XML root element titled MyFiles. This could be accomplished with the following XQuery expression:

<MyFiles>

{ document("FileSystem.xml")/filesystem/drive[@letter="C"]//file }

</MyFiles>

With this addition, the output would be:

<MyFiles>

<file>game1.sav</file>

<file>game2.sav</file>

<file>quake.exe</file>

<file>README.txt</file>

<file>EULA.txt</file>

<file>win.exe</file>

</MyFiles>

Note that in our query we used braces ({ ... }) around the XPath expression within the <MyFiles> element. The braces denote that the content within the braces is an XQuery expression, and not literal content. For example, had we omitted the braces and used the query:

<MyFiles>

document("FileSystem.xml")/filesystem/drive[@letter="C"]//file

</MyFiles>

The output would have been:

<MyFiles>

document("FileSystem.xml")/filesystem/drive[@letter="C"]//file

</MyFiles>

FLWR Expressions

While 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 allows for XML data to be queried with conditional statements, and then returns a set of XML elements as a result.

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 specifies 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 in 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 <folder> elements whose name attribute equals "Quake":

for $myNode in document("FileSystem.xml")//folder

where $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 $ (i.e., $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 <folder> elements, and for each element, the where clause asks, "Is this element's name attribute equal to Quake?", and if it is, then the return clause outputs the <folder> element.

The return clause can be more involved. In fact, the return clause can return any XQuery expression. For example, we might want our output to look like the following:

<folder>

<name>Quake</name>

<files>

<file>quake.exe</file>

<file>README.txt</file>

<file>EULA.txt</file>

</files>

</folder>

We could accomplish this using the following XQuery expression:

for $myNode in document("FileSystem.xml")//folder

where $myNode/@name="Quake"

return

<folder>

<name>{string($myNode/@name)}</name>

<files>

{$myNode/file}

</files>

</folder>

Realize that FLWR expressions are just as powerful as SQL SELECT queries. FLWRs are capable of joins, 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 .NET

While 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 performed these steps, you can start using XQuery expressions!

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 this site is having some "issues" and should be back up soon. In the meantime, you can download the contents of the xquery.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 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 = (expr.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.

Conclusion

In 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 following 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!

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有