XML
  Home arrow XML arrow Page 2 - XQuery, concluded
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  
Moblin 
JMSL Numerical Library 
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


    XQuery, concluded - Retrieving the Value of an Attribute


    (Page 2 of 5 )

    Not all of the information is contained in the text of an XML tag. Sometimes information is assigned to an attribute of a tag, such as the UPC code in the CD tag that we show here:

    <cd upc="75679244222">

    You can use an XQuery to extract the value of an attribute by calling the data() function and specifying the @ symbol in front of the attribute name. Let’s say that the UPC code is an attribute of the cd element and the cd element is the child of the catalog element. You then access the UPC code by using the following call to the data() function. You’ve seen something like this used previously in this chapter when you learned about how to use the data() function. The only new feature is the @ symbol, which you use to tell Saxon-B to use the value of the attribute rather than the text of the element.

    {data(doc("catalog.xml")/catalog/cd/@upc)}

    Here’s the XQuery that accesses the value of the upc attribute. This is basically the same XQuery that you learned about previously in this chapter, except that you pass the @upc to the data() function.

    <html>
    <body>
       List of titles in this catalog:<br/> 
       <table border="1">
         
    <tr>
             <td>UPC</td>
             <td>Artist</td>
             <td>Title</td> 
         
    </tr>
          {
             for $cd in doc("catalog.xml")/catalog/cd
               
    order by $cd/artist
             return
             <tr>
               
    <td>{data($cd/@upc)}</td> 
                <td>{data($cd/artist)}</td>  
                <td>{data($cd/title)}</td>
             
    </tr>
          }
       </table>
    </body>
    </html>

    Here’s what the XQuery writes to the output.html file.

    <html>
       <body>
          List of titles in this catalog:<br><table border="1">
          <tr>
            
    <td>UPC</td>
             <td>Artist</td>
             <td>Title</td>
         
    </tr>
         
    <tr>
             <td>74646938720</td>
             <td>Billy Joel</td>
             <td>Songs in the Attic</td>
         
    </tr>
         
    <tr>
             <td>74640890529</td>
             <td>Bob Dylan</td>
             <td>The Times They Are A-Changin'</td>
          </tr>
         
    <tr>
             <td>8811160227</td>
             <td>Jimi Hendrix</td>
             <td>Are You Experienced?</td>

          </tr>
         
    <tr>
             <td>75679244222</td>
             <td>Led Zeppelin</td>
             <td>Physical Graffiti</td>
         
    </tr>
         
    <tr>
             <td>75678263927</td>
             <td>Led Zeppelin</td>
             <td>Houses of the Holy</td>
         
    </tr>
         
    <tr>
             <td>75678367229</td>
             <td>Rush</td>
             <td>Rush in Rio</td>
          </tr>
         
    <tr>
            
    <td>602498678299</td>
            
    <td>U2</td>
            
    <td>How to Dismantle an Atomic Bomb</td>
         
    </tr>
      
    </table>
    </body>
    </html>

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

    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

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







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