XQuery Syntax

XQuery is a concept very similar to SQL. As sql is used to query database tables we can use XQuery to query XML data. Xquery can be used to query XML documents, data which is in XML syntax and databases. Using XQuery we can find XML elements. This can be very useful in situations when we need to search relevant information in large XML documents or XML data. Using XQuery we can exactly extract informatiuon what we are really looking for. XQuery is a W3C standards.

also read:

We will be using the following document to understand the basic of XQuery:

File Name : employees.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<employees>
	<employee vertical="banking">
		<id>17452</id>
		<name>Jason</name>
		<experience>2</experience>
		<salary>35000</salary>
	</employee>

	<employee vertical="insurance">
		<id>14782</id>
		<name>Jim</name>
		<experience>3</experience>
		<salary>45000</salary>
	</employee>

	<employee vertical="telecom">
		<id>12563</id>
		<name>Charles</name>
		<experience>4</experience>
		<salary>55000</salary>
	</employee>
</employees>

Selecting nodes in XQuery

Functions

XQuery uses the doc function to open a XML document.
E.g : doc(“employees.xml”)

Path Expressions

XQuery uses path expressions to traverse a XML document.
E.g: doc(“employees.xml”)/employees/employee/name
The above XQuery produces the following output :

<name>Jason</name>
<name>Jim</name>
<name>Charles</name>

Predicates

Predicates are used in XQuery to filter data from a XML document.
E.g: doc(“employees.xml”)/employees/employee[salary>45000]
The above XQuery produces the following output :

<employee vertical="telecom">
	<id>12563</id>
	<name>Charles</name>
	<experience>4</experience>
	<salary>55000</salary>
</employee>

FLWOR

FLWOR can be expanded to “For, Let, Where, Order by, Return”. Consider this example :

for $x in doc("employees.xml")/employees/employee
where $x/salary>35000
order by $x/name
return $x/name

Output

<name>Jim</name>
<name>Charles</name>

Explanation

In the above example :

  • The for clause selects all employee elements under employees element into the variable $x
  • The where clause selects only those employees whose salary is greater than 35000
  • The order by clause determines the sort order based on a named columm
  • The return clause determines what value should be returned. Here the name of employees is returned

If-Else construct in XQuery

It is possible to write the IF-Else construct in XQuery using the following syntax:

for $x in doc("employees.xml")/employees/employee
return if($x/salary gt 35000)
then <senior>{data($x/name)}</senior>
else <junior>{data($x/name)}</junior>

[Note : The function data is used to extract the value of a node]

Output

	<junior>Jason</junior>
	<senior>Jim</senior>
	<senior>Charles</senior>
	

XQuery Comparisons

We can do comparison in XQuery by using two set of operators :

  • 1) General comparision operators: =, !=, =
  • 2) Value comparision operators: eq, ne, lt, le, gt, ge

The difference between these two is that the General Operators works for multiple set of values while Value comparision works for a single value

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*