• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

XQuery Syntax

March 5, 2009 //  by Krishna Srinivasan//  Leave a Comment

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:

  • XML Tutorials
  • How to Parse XML file using DOM Parser?
  • How to Parse XML file using SAX Parser?

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

Category: XMLTag: XQuery

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.

Previous Post: « JSTL Function in JSP 2.0
Next Post: How to Query XML using XPath »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact