XML
  Home arrow XML arrow Page 2 - XQuery
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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 7
    2006-02-09

    Table of Contents:
  • XQuery
  • Testing Saxon-B
  • How XQuery Works
  • A Walkthrough of an XQuery
  • Constructors

  • 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


    XQuery - Testing Saxon-B


    (Page 2 of 5 )

    Once you’ve installed the software, you’ll need to make sure everything is working properly. To do this, create an XML document and an XQuery, and then use Saxon-B to retrieve information from the XML document.

    Here’s the XML document that you’ll use to learn XQuery. Write this document using an editor and save it in the saxon directory as catalog.xml:

    <?xml version="1.0"? >
    <catalog> 
      
    <cd upc="602498678299">
         
    <artist>U2</artist>
         
    <title>How to Dismantle an Atomic Bomb</title>
        
    <price>13.98</price>
        
    <label>Interscope Records</label>
        
    <date>2004-11-23</date>
      </cd>
     
    <cd upc="75679244222">
         <artist>Led Zeppelin</artist> 
         <title>Physical Graffiti</title>
         <price>22.99</price>
         <label>Atlantic</label>
         <date>1994-08-16</date>
     
    </cd>
     
    <cd upc="75678367229">
         <artist>Rush</artist>
         <title>Rush in Rio</title>
         <price>13.98</price>
         <label>Atlantic</label>
         <date>2003-10-21</date>
     
    </cd>
     
    <cd upc="74646938720">
         <artist>Billy Joel</artist>
         <title>Songs in the Attic</title>
         <price>10.99</price>
         <label>Sony</label>
         <date>1998-10-20</date>
     
    </cd>
     
    <cd upc="75678263927">
         <artist>Led Zeppelin</artist>
         <title>Houses of the Holy</title>
         <price>10.98</price>
         <label>Atlantic</label>
         <date>1994-07-19</date>
     
    </cd>
     
    <cd upc="8811160227">
         <artist>Jimi Hendrix</artist>
         <title>Are You Experienced?</title>
         <price>12.99</price>
         <label>Experience Hendrix</label>
         <date>1997-04-22</date>
     
    </cd>
     
    <cd upc="74640890529">
         <artist>Bob Dylan</artist>
         <title>The Times They Are A-Changin'</title>
         <price>9.99</price>
         <label>Sony</label>
         <date>1990-10-25</date>
     
    </cd>
    </catalog>

    Next, you’ll need to create the XQuery. Type the following XQuery into your editor and save it in the saxon directory in a file called catalog.xq. This XQuery retrieves and displays a list of titles contained in the XML document. Although the XQuery probably looks strange to you, you’ll understand each line of the XQuery by the time you finish reading this chapter.

    <html>
    <body>
     
    List of titles in this catalog:
     
    <br/>
     
    <ul>
     
    {
        
    for $x in doc("catalog.xml")/catalog/cd/title
           
    order by $x
           
    return <li>{data($x)}</li>
      }
     
    </ul>
    </body>
    </html>

    The final step to test Saxon-B is to execute the XQuery. Here’s what you need to do:

    1. Open a Command Prompt window if you’re using a Windows computer.
    2. Make saxon the current directory.
    3. Type the following command:

      c:\Saxon> c:\jdk15\bin\java -cp saxon8.jar net.sf.saxon.Query -t catalog
      .xq > output.html
    4. Press ENTER.

     

    This command probably looks like a bunch of gibberish. It isn’t. The first part (c:\jdk15\bin\java) specifies the path to Java and runs Java. The second part (-cp saxon8.jar net.sf.saxon.Query) tells Java to extract the Query portion of the saxon8 .jar file, which is the file that contains Saxon-B. The third part (-t catalog.xq) of the command identifies the XQuery file, which is catalog.xq. The last part (> output .html) redirects the result of running the XQuery to the file called output.html.

    TIP The jdk15 portion of the first part of the command is the directory where  Java is installed on our computer. You probably installed Java in a different directory so you’ll need to replace jdk15 with the name of the directory on your computer where you installed Java.

    You can use this same command to run all the examples in this chapter; however, for each example you’ll need to change the name of the query from catalog.xq to the name we give to the query.

    Nothing much happens when you run Saxon-B—at least nothing you can see on the screen. Open the output.html in an editor and you’ll see the result of your XQuery. It should look like this:

    <html>
       <body>
         
    List of titles in this catalog:<br><ul>
             <li>Are You Experienced?</li> 
             <li>Houses of the Holy</li> 
             <li>How to Dismantle an Atomic Bomb</li>
             <li>Physical Graffiti</li>
             <li>Rush in Rio</li>
             <li>Songs in the Attic</li> 
             <li>The Times They Are A-Changin'</li>
         
    </ul>
       </body>
    </html>

    Figure 9-1 shows the result of your XQuery when it’s displayed by a browser. 


    Figure 9-1  Your browser displays the result of your XQuery request.

     

     

     

    More XML Articles
    More By McGraw-Hill/Osborne


       · This article is an excerpt from the book "XML DeMYSTiFieD," published by...
       · <cd upc="75678263927> entry is missing a quote mark, and messes tp the XML...
       · This is fixed now, thanks for bringing this to our attention
     

    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

    - 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
    - Simple Web Syndication with RSS 2.0
    - Java UI Design with an IDE







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek