XML
  Home arrow XML arrow Page 3 - 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  
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
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 - How XQuery Works


    (Page 3 of 5 )

    An XQuery must contain a conditional expression that specifies the search criteria. A conditional expression is either true or false. For example, /catalog/cd/title = ‘XML Demystified’ is a conditional expression. Saxon-B looks at the next title tag in the XML document and determines if the text of the title tag is ‘XML Demystified’. If so, then the conditional expression is true; otherwise, the conditional expression is false.

    A conditional expression is used within a FLWOR expression. FLWOR sounds like more gibberish, but it’s really an acronym for for, let, where, order by, and return clauses. A clause is a component of an XQuery.

    For, Let, and Order By Clauses

    You use the for and let clauses to assign values to variables within the XQuery. A variable is a placeholder for a value such as $x. Look at the catalog.xq file and you’ll notice the following for clause. For each title tag in the catalog XML file, Saxon-B assigns the text of the current title XML tag to the $x variable and then sorts the titles.

    for $x in doc("catalog.xml")/catalog/cd/title

    Look at the code that follows the for clause and you’ll see that variable $x is used by the order by clause. The text of the title replaces the $x variable when Saxon-B runs the XQuery before processing the order by clause.

    The order by clause places the value of $x in sorted order. In this example, the order by clause sorts all the titles in alphabetical order before storing the titles into the output.html file. Titles within the XML document remain unchanged.

    You can specify the direction of the sort by using ascending or descending, as we show here. The default direction is ascending.

    order by $x descending

    The let clause, not used in the catalog.xq example, assigns a value to a variable. Suppose you want to assign the title ‘XML Demystified’ to variable $x. Here’s how you write the let clause to do this.You can then use $x in place of ‘XML Demystified’ throughout the XQuery.

    let $x := 'XML Demystified'

    The Where and Return Clauses

    You use the where clause to specify a filter criterion using a conditional expression. Let’s say that you want to see titles by Jimi Hendrix. You can use the where clause to tell Saxon-B to compare the text of the artist tag to Jimi Hendrix. If there is a match, then the text of the title tag is assigned the variable $x, which is then used by the order by clause to sort all titles by Jimi Hendrix. This is illustrated in the following segment of the XQuery.

    for $x in doc("catalog.xml")/catalog/cd/title
      
    where doc("catalog.xml")/catalog/cd/artist = 'Jimi Hendrix'
    order by $x

    The return clause identifies information that Saxon-B returns to the output.html file. It can return literal characters, the content of tags in an XML document, and the value of variables used within the XQuery.

    The catalog.xq XQuery uses the return clause to return HTML characters and the value of the $x. The HTML characters format the data for the browser. The data($x) function is called to return the text associated with $x. Remember $x is the title element that includes the markup tag and any child nodes. The data() function extracts the text from between the tags, for example:

    $x equals: <title>How to Dismantle an Atomic Bomb</title>
    data($x) equals: How to Dismantle an Atomic Bomb

    A function is a task that Saxon-B performs. You call the function by name anytime you want the task to be performed in your XQuery. This is referred to as a function call and must be contained with French braces ({ }). This example calls the data() function, which extracts the text from the argument:

    return <li>{data($x)}</li>

    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

    - 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 4 hosted by Hostway
    Stay green...Green IT