XQuery in DBMS

XQuery is the query language used to query the records in the XML document. It is similar to the SQL query in RDBMS. It is the functional language independent of the format of the XML data in the document. This can be used to query XML document, XML database or relational database with XML data.

XQuery has following characteristics

  • It is a functional language used to retrieve the XML data from any document or database.
  • This is similar to SQL in RDBMS, and queries / traverses XML data.
  • It uses XPaths to traverse through XML data to fetch the records.
  • XQuery is supported by almost all database system.

Though the expressions that we have seen above to point to different nodes, simply writing the expression will not give the required results. Those expressions will simply point to the nodes specified by the expressions. It should have proper querying to pull the values at that node. That is done by XQuery.

Consider the below XML document of addresses of different people in US. Let the name of the document be address.xml.  Now we know that if we need to access any node element within this document, we have to use XPath expressions. But these expressions cannot be written within this xml document. We need to have separate file or platform for writing the expression. That means the expressions are written outside the address.xml file. Then how will expression know that it is accessing the address.xml file ?

<Contact>
         <Address Name=”Rose”>
<ApartmentNum>APT 201 </ ApartmentNum>
<Street>Lakeside Village Drive </Street>
<Town> Clinton Township </Town>
<State> MI </State>
<Country> US </Country>
           </Address>
           <Address Name=”William”>
<ApartmentNum>APT 671 </ ApartmentNum>
<Street>Fraser Village Drive </Street>
<Town> Fraser Township </Town>
<State> NY </State>
<Country> US </Country>
           </Address>
           <Address Name=”Robert”>
<ApartmentNum>APT 232 </ ApartmentNum>
<Street>Seianna Drive </Street>
<Town> Danbury </Town>
<State> CT </State>
<Country> US </Country>
           </Address>
</Contact>

 XQuery Functions

When XQuery uses XPath expressions, it will be written outside the xml document, in a separate file or tool. Then the expression will not have any link with the xml file that it is accessing. In order to make the expression to access the address.xml file, XQuery uses a function doc(). This function creates a link between the XQuery and XPath. That means the XPath expressions are written based on the file pointed by doc() function.

doc (“file_name”)

doc (“address.xml”)

Path Expressions

Once doc function is used, the XQuery is now pointing to the xml file that it needs to traverse. Now it can use XPath expressions to traverse the nodes within this xml file. i.e.;

doc (“address.xml”)/Contact/Address/Street
doc (“address.xml”)/Contact/Address[State = “NY”]

Above expression will select all the Addresses whose state is ‘NY’. That means, it will select whole set of details like below :

<Address Name=”William”>
      <ApartmentNum>APT 671 </ ApartmentNum>
      <Street>Fraser Village Drive </Street>
      <Town> Fraser Township </Town>
       <State> NY </State>
      <Country> US </Country>
</Address>

Suppose we have to select only particular field from above list rather than selecting whole details, say street of each addresses in ‘NY’, then we can modify the expression like below :

doc (“address.xml”)/Contact/Address[State = “NY”]/Street

This expression will pull all the streets in the addresses whose state is ‘NY’. This is how we get particular element from the xml file using XPath’s expression and predicate. But same can be done in XQuery using same XPath expression is little different way. XQuery gives the touch of query to above expression using FLWOR – ‘For Let Where Order By Return’. i.e.;

for : this clause selects all the elements under the given XPath expression.
Let : It stores the elements selected in for clause into a variable $x. In XQuery variables starts with ‘$’ symbol.
where : this clause is used to indicate any predicates / conditions.
order by : This clause will sort the result set.
return : This clause will have the elements that need to be returned.

for $x in doc (“address.xml”)/Contact/Address
where $x/State=”NY”
return $x/Street

 This query will return the same result as XPath expression written above, i.e.;

 <Street>Fraser Village Drive </Street>

 Consider another XQuery :

for $x in doc (“address.xml”)/Contact/Address
where $x/ Country =”US”
return $x/Street

 This query pulls all the streets in country US. Now the result will be

<Street>Lakeside Village Drive </Street>
<Street>Fraser Village Drive </Street>
<Street>Seianna Drive </Street>

This result is not ordered. Suppose we want to see the sorted result. Then XQuery will modify as below :

for $x in doc (“address.xml”)/Contact/Address
where $x/ Country =”US”
order by $x/Street
return $x/Street

Now we will have ordered result set from xml document.

<Street>Fraser Village Drive </Street>
<Street>Lakeside Village Drive </Street>
<Street>Seianna Drive </Street>

Suppose we have more than one condition to be specified in xquery above. Then we can use if..then.. else expression above. We can even change the name of node / element using XQuery.

for $x in doc (“address.xml”)/Contact/Address
return if ($x/@Category=”Rose”)
         then <Rose_Address>{data(return $x/Street)} </Rose_Address>
         else <Others_Address>{data(return $x/Street)} </Others_Address>

Above query will check for the value of attribute of address before returning the street value. It checks if it is “Rose”, then it returns her street name with tag ,<Rose_Address>, otherwise it returns the street as <Others_Address> as shown below.

<Rose_Address>Lakeside Village Drive </ Rose_Address >
< Others_Address >Fraser Village Drive </ Others_Address >
< Others_Address >Seianna Drive </ Others_Address >

 In XQuery comparison in the conditions can be done in two methods

  • General Comparison : This method of comparison uses arithmetic operators like =, !=, <, <=,  > and >=. When these operators are used for comparison, comparison is done on the one or more than one value returned by the expression. That means comparison is done on set of values and whichever values satisfies the condition, those elements will be returned.

For example where $x/ Country =”US”, will check for all the addresses which has country as US, and it will pull the street element for them.

  • Value Comparison : This is another method of comparison which uses eq, lt, gt, le, ge, and ne for comparison. This works similar to above comparison but only one value is compared and results are returned based on that.

For example where $x/@Category eq ”Rose”, will check if the category value is equal to “Rose” and if it matches, then it will return the details. If it does not match then it will not check any other address attribute for its value.

XQuery Supports function calls too. Suppose we want to check if the street name has ‘Fraser’ in it and if yes then return its town. This is done by using substring function like below :

for $x in doc (“address.xml”)/Contact/Address
where $x/ substring(Street , 1, 6) =”Fraser”
return $x/Town

There are many other functionalities of XQuery, which are useful in traversing the XML document.

Advantages of XQuery :

  • XQuery can be used to traverse both tabular as well as hierarchical data, provided they are in XML format.
  • These can be used to traverse graphical and tree like structures.
  • It can be used to transform XML documents.
  • It can be used to traverse and build WebPages.
Translate »