Home arrow XML arrow Page 3 - 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 and the Attribute Name
(Page 3 of 5 )

When you use the @ symbol followed by the attribute name without calling the data() function, an XQuery can return the name of the attribute, along with its value, as we illustrate here:


Figure 9-4.  Here's how the UPC attribute appears when  the output.html file is displayed in the browser.

Letís modify the previous XQuery to display both the UPC attribute name and its value. Hereís the revised XQuery:

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

Hereís the new output.html file. Notice that the attribute appears just as it does in the XML document. It has the attribute name, equal sign, and the value. Saxon-B is smart enough to replace the &quot; that's in the XML document in double quotations.

      List of titles in this catalog:<br><table border="1">
            <td upc="74646938720"></td>
            <td>Billy Joel</td>
            <td>Songs in the Attic</td>
<td upc="74640890529"></td>
            <td>Bob Dylan</td>
            <td>The Times They Are A-Changin'</td>
            <td upc="8811160227"></td>
            <td>Jimi Hendrix</td>
            <td>Are You Experienced?</td>
            <td upc="75679244222"></td>
            <td>Led Zeppelin</td>
            <td>Physical Graffiti</td> 
            <td upc="75678263927"></td>
            <td>Led Zeppelin</td>
            <td>Houses of the Holy</td>
            <td upc="75678367229"></td>
            <td>Rush in Rio</td>
            <td upc="602498678299"></td>
            <td>How to Dismantle an Atomic Bomb</td>

CAUTION Donít place text or any node before the attribute because it will cause  an error. For example, the following statement confuses Saxon-B because the attribute is in the wrong location. Attributes are assigned first and then followed by the text of the element.

<td> some other data {$cd/@upc}</td>

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-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials