Home arrow XML arrow XQuery, concluded
XML

XQuery, concluded


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 second 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 / 9
February 16, 2006
TABLE OF CONTENTS:
  1. · XQuery, concluded
  2. · Retrieving the Value of an Attribute
  3. · Retrieving the Value of an Attribute and the Attribute Name
  4. · Functions
  5. · Looking Ahead

print this article
SEARCH DEVARTICLES

XQuery, concluded
(Page 1 of 5 )

Conditional Statements

A conditional statement specifies the search criteria for an XQuery. It simply tells Saxon-B to retrieve specific information from an XML document only if the information adheres to the conditional expression contained in the conditional statement.

A conditional expression contains operators and operands that resolve to either true or false. Operators include less than (<), greater than (>), equals (=), less than or equal to (<=), greater than or equal to (>=), and not equal to (!=). An operand is a value evaluated by an operator in a conditional expression. In this case, it’s the name of the XML tag that contains the information that you want Saxon-B to compare to the search criteria—and the value of the search criteria is an operand as well.

A conditional statement is used in a for clause, which we explained earlier in the section “For, Let, and Order By Clauses”. It’s also used in an if…then…else statement. An if…then…else statement basically tells Saxon-B, “If the conditional expression is true, then execute these statements, else execute these other statements if the conditional expression is false.”

The if…then…else statement contains a conditional expression and two blocks of statements, as we show here. The conditional expression is contained within parentheses. In this example, the conditional expression is testing where the text of the price XML tag is greater than 15. The first code block is between the if…then and the else. It contains statements that are executed if the text of the price tag is greater than 15. The second block of code follows the else. The statement contained beneath the else is executed if the text of the price tag is not greater than 15.

Notice that we use the round-half-to-even() function. This function rounds the calculated value. So 1.5 becomes 2 and 2.5 becomes 2. It rounds to the nearest even number. The round-half-to-even() function has two parameters. The first parameter is the value that’s being rounded. In this case, we used a calculation. The result of the calculation is rounded. The second parameter is the number of decimal places that we want to show. In this example, we show two decimal places.

if($price > 15) then
  
round-half-to-even($price * 0.8, 2)
else
  
round-half-to-even($price * 0.9, 2)

Another form for an if…then…else statement is the if…then…else if…else statement. The if…then…else if…else statement is very similar to the if…then… else statement except that it contains two conditional statements.

The first conditional statement is the same as the one we show in the previous example. The second conditional statement is in the else if portion of the statement. This tells Saxon-B that if the first conditional statement is false, then test the second conditional statement. If the second conditional statement is true, then execute the statements between the else if and else. If the second conditional statement is false, then execute the statements that follow the else.

There are three code blocks in an if…then…else if…else statement, as we show here. The first code block is between the if…then and else if. The second code block is between the else if and else. And the third code block is after the else.

if($price > 15) then
  
round-half-to-even($price * 0.8, 2)
else if($price > 11) then
  
round-half-to-even($price * 0.9, 2)
else
  
"No discount"

Suppose you want to extract the CD artist and title and then determine if a discount should be applied to the price. If the price is more than $15.00, then calculate a 20-percent discount. If the price is greater than $11.00 and less then or equal to $15.00, then calculate a 10-percent discount. There’s no discount if the price is less than or equal to $11.00.

Here is the XQuery that you’ll need to write to extract this information and perform these calculations. You’ll notice that this is basically the same XQuery that you used throughout this chapter. However, we inserted an if…then…else if…else statement in the return clause of the XQuery. This conditional statement returns either the discounted price or the price itself if the price is less than or equal to $11.00. It also controls the format for decimal values.

<html>
<body>
   List of titles in this catalog:<br/> 
   <table border="1">
     
<tr>
         <td>Artist</td>
         <td>Title</td>
         <td>List Price</td>
         <td>Sale Price</td>
     
</tr>
     {
       
for $cd in doc("catalog.xml")/catalog/cd
           let $price := xs:decimal($cd/price)
           order by $cd/artist
        return
          
<tr>
              <td>{data($cd/artist)}</td>
              <td>{data($cd/title)}</td>
              <td>{$price}</td>
              <td>
               
{
                
if($price > 15) then 
                  round-half-to-even($price * 0.8, 2)
               else if($price > 11) then
                  round-half-to-even($price * 0.9, 2)
               else
                 "No discount" 
             
}
              </td>
           </tr>
   }
 
</table>
</body>
</html> 

                                                


Figure 9-3.  Here's the result of the XQuery when it's displayed in a browser.

Here’s what the XQuery writes to the output.html file, and Figure 9-3 shows the result when it’s displayed in a browser.

<html>
   <body>
      List of titles in this catalog:<br><table border="1">
       
<tr>
           <td>Artist</td>
           <td>Title</td>
           <td>List Price</td>
           <td>Sale Price</td>
        
</tr>
        
<tr>
           <td>Billy Joel</td>
           <td>Songs in the Attic</td>
           <td>10.99</td>
           <td>No discount</td>
        
</tr>
       
<tr>
           <td>Bob Dylan</td>
           <td>The Times They Are A-Changin'</td>
           <td>9.99</td>
           <td>No discount</td>
        </tr>
       
<tr>
           <td>Jimi Hendrix</td>
           <td>Are You Experienced?</td>
           <td>12.99</td>
           <td>11.69</td>
        
</tr>
       
<tr>
           <td>Led Zeppelin</td> 
           <td>Physical Graffiti</td> 
           <td>22.99</td>
           <td>18.39</td>
        
</tr>
        
<tr>
           <td>Led Zeppelin</td>
           <td>Houses of the Holy</td>
           <td>10.98</td>
           <td>No discount</td>
        </tr>
        
<tr>
           <td>Rush</td>
           <td>Rush in Rio</td>
           <td>13.98</td>
           <td>12.58</td>
        
</tr>
        
<tr>
           <td>U2</td>
           <td>How to Dismantle an Atomic Bomb</td>
          <td>13.98</td>
          <td>12.58</td>
        </tr>
      /table>
   </body>
</html>


blog comments powered by Disqus
XML ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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