Introduce DataBase,Asp.net,JavaScript,Xml,Html,Css,Sql,Php,ASP.NET Controls,AJAX,Tools,HTML,CSS,JavaScript,Open Source Project,WPF,.Net Framework,Linq
Top Recommended Hosting

A Simple XQuery Example

by the3factory 4/13/2008 11:52:00 PM
XQuery is used to query an XML document, and for that you need an XML document to talk about while examining the various queries. For the purposes of this chapter, consider the XML document in Listing 1, which describes the structure of a set of products. Listing 1: Sample XML file
     


707
Sport-100 Helmet, Red
HL-U509-R


709
Mountain Bike Socks, M
SO-B909-M


710
Mountain Bike Socks, L
SO-B909-L


712
AWC Logo Cap
CA-1098


The root element of this XML document is
, which contains an arbitrary number of
 elements. Each
 element, in turn, contains
, , and
 elements. In addition, the
 element also contains a Category attribute.
Just as SQL needs to be able to access any row or column in a relational table, XQuery needs to be able to access any node in an XML document. XML structures have both hierarchy and sequence, and can contain complex structure. Path expressions directly support hierarchy and sequence, and allow you to navigate any XML structure. In its simplest form, an XQuery can simply be an XPath expression. For example, to get a list of all of the product names that are of type “Socks”, you could use the following XQuery:
     doc("Products.xml")/Products/Product[@Category="Socks"]/Name
The doc(“Products.xml”) part indicates the XML data store, which is an XML file named Products.xml in this case. Given the preceding contents of the Products.xml file, the output of this query would be as follows:
     Mountain Bike Socks, M
Mountain Bike Socks, L
The output of an XQuery statement is a collection of XML elements. In the previous example, it is a collection of elements.

Related posts

Sign up for PayPal and start accepting credit card payments instantly.


Powered by BlogEngine.NET 1.2.0.0