Home arrow XML arrow Page 5 - XQuery


XQuery speeds up the process of finding information contained in an XML document, which is very handy when dealing with long XML documents. This article, the first of two parts, will teach you how to write XQuery expressions. It is excerpted from chapter nine of the book XML DeMYSTiFieD, written by Jim Keogh and Ken Davidson (McGraw-Hill/Osborne, 2005; ISBN: 0072262109).

Author Info:
By: McGraw-Hill/Osborne
Rating: 5 stars5 stars5 stars5 stars5 stars / 10
February 09, 2006
  1. · XQuery
  2. · Testing Saxon-B
  3. · How XQuery Works
  4. · A Walkthrough of an XQuery
  5. · Constructors

print this article

XQuery - Constructors
(Page 5 of 5 )


Information contained in an XML document is stored as a series of characters called a string. This isnít a problem unless you want to use the information in a calculation; then you need to convert the character to a type of information that can be calculated. There are several types of data, which are referred to as data types. Youíre familiar with most of them. These are numbers, decimals, dates, and Boolean. Boolean is true or false. And, of course, there is a string.

Letís say that you want to calculate the sales tax for a CD. The price of the CD is $19.95 and is in the XML document. However, $19.95 is a string and not a number. Therefore, you must convert $19.95 from a string to a decimal. A decimal contains both a whole number and a decimal number.

You can convert information contained in an XML document to another data type by using a constructor. A constructor tries to convert the content of an XML tag to a data type. If it fails, then it returns an error. Table 9-1 contains a list of constructors. You use them within the XQuery to convert information from the XML document into a different data type so it can be used in a calculation.

A constructor requires you to pass it the name of the tag. The constructor then returns the converted value. Typically youíll assign the converted value to a variable and then use the variable in a calculation.

Hereís how to convert the CD price to a decimal value. The xs:decimal() is the constructor. The doc() function retrieves the catalog.xml document that contains the price. /catalog/cd/price are parent/child tags that identify the tag whose text is being converted to a decimal. The converted value is assigned to the $price variable:

let $price := xs:decimal(doc(ďcatalog.xml")/catalog/cd /price)

Constructors are frequently used in the conditional statements in a where clause to compare the search criteria to the value of the XML tag. Letís say that you want to extract the title and price of all the CDs that are less than $11.00 with a release date greater than 1993-10-31.

In order to do this, you need to convert the text of the price tag to a decimal and the text of the date tag to a date data type. Once theyíre converted, you can write a conditional statement that compares these values against the search criteria.




Constructs a decimal value from a string. The string must be a number.


Constructs a date value from a string. The string must conform to the pattern YYYY-MM-DD; for example, 2006-10-31.


Constructs a double precision floating point number from a string.


Constructs a floating point number from a string.


Constructs a hex binary value from a string.


Constructs an integer from a string.


Constructs a time.


Table 9-1: Constructors Used to Convert XML Strings

Letís walk through the next example and see how this works. The for clause opens the catalog XML document and assigns the /catalog/cd tags to the $cd variable. You then combine the $cd variable with the /price tag and the /date tag in the constructors. You do this to make the code easier to read. You could have passed the complete parent/child tags to the constructor such as we did in the example we show earlier in this section.

You use the xs:decimal() constructor and the xs:date() constructor to convert the price and the date to its respective data type and assign the converted value to variables. You then use variables in the conditional expression of the where clause. The value of the $price variable is compared to 11.00 and the value of the $date variable is compared to 1993-10-31. Notice that 1993-10-31 must also be converted to a date because ď1993-10-31Ē is a string just like the date in the XML document. You use the less than operator (<) to determine if the value of the price variable is less than 11.00; and use the greater than operator (>) to determine if the value of the date variable is greater than 1993-10-31. Both conditions must be true for the conditional statement to be true, and for information about the CD to be returned by the XQuery.

List of titles in this catalog:<br/>
for $cd in doc("catalog.xml")/catalog/cd
let $price := xs:decimal($cd/price)
        let $date := xs:date($cd/date)
where $price < 11.00 and $date > xs:date("1993-10-31")
order by $date
return <li>{data($cd/title)} - ${$price} - {$date}</li>

Hereís the result of this XQuery:

List of titles in this catalog:<br><ul>
<li>Houses of the Holy - $10.98 - 1994-07-19</li>
<li>Songs in the Attic - $10.99 - 1998-10-20</li>

Figure 9-2 shows the result of the XQuery when itís displayed by a browser. 

Figure 9-2.  The XQuery result your browser displays.

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus

- Open XML Finally Supported by MS Office
- XML Features Added to Two Systems
- Using Regions with XSL Formatting Objects
- Using XSL Formatting Objects
- More Schematron Features
- Schematron Patterns and Validation
- Using Schematron
- Datatypes and More in RELAX NG
- Providing Options in RELAX NG
- An Introduction to RELAX NG
- Path, Predicates, and XQuery
- Using Predicates with XQuery
- Navigating Input Documents Using Paths
- XML Basics
- Introduction to XPath

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials