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.
Constructor | Description |
xs:decimal | Constructs a decimal value from a string. The string must be a number. |
xs:date | Constructs a date value from a string. The string must conform to the pattern YYYY-MM-DD; for example, 2006-10-31. |
xs:double | Constructs a double precision floating point number from a string. |
xs:float | Constructs a floating point number from a string. |
xs:hexBinary | Constructs a hex binary value from a string. |
xs:int | Constructs an integer from a string. |
xs:time | 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.
<html>
<body>
List of titles in this catalog:<br/>
<ul>
{
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>
}
</ul>
</body>
</html>
Here’s the result of this XQuery:
<html>
<body>
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>
</ul>
/body>
</html>
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. |
|
This article is excerpted from chapter nine of the book XML DeMYSTiFieD, written by Jim Keogh and Ken Davidson (McGraw-Hill/Osborne, 2005; ISBN: 0072262109). Check it out today at your favorite bookstore. Buy this book now.
|
|