SunQuest
 
       XML
  Home arrow XML arrow XQuery, concluded
IBM developerWorks
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

XQuery, concluded
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2006-02-16

    Table of Contents:
  • XQuery, concluded
  • Retrieving the Value of an Attribute
  • Retrieving the Value of an Attribute and the Attribute Name
  • Functions
  • Looking Ahead

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th -1:00PM EST. Register Today!

    XQuery, concluded


    (Page 1 of 5 )

    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).

    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>

    More XML Articles
    More By McGraw-Hill/Osborne


       · This article is an excerpt from the book "XML DeMYSTiFieD," published by...
     

    Buy this book now. 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.

    XML ARTICLES

    - Path, Predicates, and XQuery
    - Using Predicates with XQuery
    - Navigating Input Documents Using Paths
    - XML Basics
    - Introduction to XPath
    - Simple Web Syndication with RSS 2.0
    - Java UI Design with an IDE
    - UI Design with Java and XML Toolkits
    - Displaying ADO Retrieved Data with XML Islan...
    - Widget Walkthrough
    - Introduction to Widgets
    - The Why and How of XML Data Islands
    - Creating an XUL App Installer
    - Overlays in XUL
    - Skinning Your Custom XUL Applications







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway