XML
  Home arrow XML arrow Page 4 - 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  
Mobile Linux 
App Generation ROI 
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 / 4
    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 - Functions


    (Page 4 of 5 )

    You already learned that a function is a task that Saxon-B already knows how to perform; all you need to do is to call the function in your XQuery whenever you want Saxon-B to perform that task.

    Table 9-2 contains commonly used XQuery functions. You can find a complete list of functions at www.w3.org/2005/02/xpath-functions.

    In addition to calling built-in functions, you can also define your own functions that can be called the same way a built-in function is called. Here’s what you need to do. First create the function by writing a function declaration statement.

    Function

    Description

    Example

    upper-case()

    Converts the argument to uppercase letters.

    upper-case("Led Zeppelin") returns: "LED ZEPPELIN"

    lower-case()

    Converts the argument to lowercase letters.

    lower-case("Led Zeppelin") returns: "led zeppelin"

    substring()

    Returns a substring.

    substring("Led Zeppelin",1, 6) returns: "Led Ze"

    string()

    Returns the string representation of the argument.

    string(645) returns: "645" as a string.

    concat()

    Returns the concatenation of two strings.

    concat("XQu", "ery") returns: "XQuery"

    string-join()

    Returns a concatenation of the arguments separated by the specified separator. The fi rst argument is a list of strings and the second argument is the separator. You may find this particularly useful for displaying names.

    string-join(("Mary", "Ellen", "Smith"), " ") returns: "Mary Ellen Smith"

    string-length()

    Returns the length of the string. If the argument is a node, then it returns the length of the string data for that node.

    string-length("Led Zeppelin") returns: 12

    Table 9-2 Commonly Used Built-In XQuery Functions

    The function declaration statement must have a prefix, a function name, a parameter list, and a return value. In addition, a function declaration statement must also define a code block that contains statements that are executed when the function is called from within an XQuery.

    Here’s the structure of a function declaration statement:

    declare function prefix:function_name($parameter as datatype, ...)
       as returntype
    {
        ... code for the function goes here...
    };

    Let’s declare a function. You’ll call it convertdate and it will convert the date format 2006-10-04 to October 4, 2006. The prefix will be called local. The parameter is the date that will be converted and the return value is the converted date.

    Here’s the function declaration. Notice that the parameter is placed within parentheses. You’ll need to give the parameter a name and specify its data type. The name is always prefaced with a $ symbol. You’ll also need to specify the data type of the value returned by the function. The return type in this example is a string.

    The code block is defined with open and closed French braces ({ }). This is where you place statements that execute each time the function is called. The function begins by assigning all the months to an array called $month. An array is a variable that can have many values. Next, the month-from-date() function is called to extract the month of the date and assign it to the $month variable. The day-from-date() function and year-from-date() function are passed to the concat() function in the return clause to return the reformatted date.

    The function declaration statement must appear at the top of the XQuery, as we show in the following example. Think of this as defining the function before you call the function within the XQuery. The function is called later in the XQuery {local:convertdate(xs:date($cd/date))}.

    declare function local:convertdate($date as xs:date) as xs:string
    {
      
    let $months := ("January","February","March","April","May",
            
    "June","July","August","September",
    "October","November","December")
      
    let $month := $months[month-from-date($date)]
      
    return
      
    concat($month, " ", string(day-from-date($date)), ", ",
            
    string(year-from-date($date)))
    };
    <html>
    <body>
       List of titles in this catalog:<br/> 
       <table border="1">
          
    <tr>
             <td>UPC</td>
             <td>Artist</td>
             <td>Title</td>
             <td>Date</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>
             <td>{local:convertdate(xs:date($cd/date))}</td>
       
    </tr>
      }
      </table>
    </body>
    </html>

    Here’s the output.html file that the XQuery produces:

    <html>
    <body>
          List of titles in this catalog:<br><table border="1">
            
    <tr>
                <td>UPC</td>
                <td>Artist</td>
                <td>Title</td>
                <td>Date</td>
            
    </tr>
            
    <tr>
                <td>74646938720</td>
                <td>Billy Joel</td>
                <td>Songs in the Attic</td>
                <td>October 20, 1998</td>
            
    </tr>
            
    <tr>
                <td>74640890529</td>
                <td>Bob Dylan</td>
                <td>The Times They Are A-Changin'</td>
                <td>October 25, 1990</td>
             </tr>
            
    <tr>
                <td>8811160227</td>
                <td>Jimi Hendrix</td>
                <td>Are You Experienced?</td>
                <td>April 22, 1997</td>
            
    </tr>
            
    <tr>
                <td>75679244222</td>
                <td>Led Zeppelin</td>
                <td>Physical Graffiti</td>
                <td>August 16, 1994</td>
            
    </tr>
            
    <tr>
                <td>75678263927</td>
                <td>Led Zeppelin</td>
                <td>Houses of the Holy</td>
                <td>July 19, 1994</td>
             </tr>
            
    <tr>
                <td>75678367229</td>
                <td>Rush</td>
                <td>Rush in Rio</td>
                <td>October 21, 2003</td>
            
    </tr>
            
    <tr> 
                <td>602498678299</td>
                <td>U2</td>
                <td>How to Dismantle an Atomic Bomb</td>
                <td>November 23, 2004</td> 
             </tr>
          </table>
    </body>
    </html>

    Here’s how the output.html file appears when displayed in a browser (see Figure 9-5). 
                                             


    Figure 9-5.  Here's how the output. html file is 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
    Stay green...Green IT