Home arrow XML arrow Page 2 - XQuery, concluded

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

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.


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.

   List of titles in this catalog:<br/> 
   <table border="1">
         for $cd in doc("catalog.xml")/catalog/cd
order by $cd/artist

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

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

         <td>Led Zeppelin</td>
         <td>Physical Graffiti</td>
         <td>Led Zeppelin</td>
         <td>Houses of the Holy</td>
         <td>Rush in Rio</td>
<td>How to Dismantle an Atomic Bomb</td>

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.

blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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