Built-in Functions

XQuery has a set of built-in functions and operators, including many that are familiar from other languages, and some that are used for customized XML processing. The complete list of built-in functions is found in [XQ-FO]. This section focuses on the most commonly used functions, some of which must be understood to follow what is said in the rest of the chapter.

SQL programmers will be familiar with the min() , max() , count() , sum() , and avg() functions. The following query returns the titles of books that are more expensive than the average book:

 let $b := doc("books.xml")//book let $avg := average( $b//price ) return $b[price > $avg] 

For our sample data, Listing 1.18 shows the result of this query.

Listing 1.18 Result of Query for Books More Expensive Than Average
 <book year = "1999">    <title>The Economics of Technology and Content for    Digital TV</title>    <editor>      <last>Gerbarg</last>      <first>Darcy</first>      <affiliation>CITI</affiliation>    </editor>    <publisher>Kluwer Academic Publishers</publisher>    <price>129.95</price> </book> 

Note that price is the name of an element, but max() is defined for atomic values, not for elements. In XQuery, if the type of a function argument is an atomic type , then the following conversion rules are applied. If the argument is a node, its typed value is extracted, resulting in a sequence of values. If any value in the argument sequence is untyped, XQuery attempts to convert it to the required type and raises an error if it fails. A value is accepted if it has the expected type.

Other familiar functions in XQuery include numeric functions like round() , floor() , and ceiling() ; string functions like concat() , string-length () , starts-with () , ends-with () , substring() , upper-case () , lower-case () ; and casts for the various simple types. These are all covered in [XQ-FO], which defines the standard function library for XQuery; they need no further coverage here since they are straightforward.

XQuery also has a number of functions that are not found in most other languages. We have already covered distinct-values () , the input functions doc() and collection() . Two other frequently used functions are not() and empty() . The not() function is used in Boolean conditions; for instance, the following returns books where no author's last name is Stevens:

 for $b in doc("books.xml")//book where not(some $a in $b/author satisfies $a/last="Stevens") return $b 

The empty() function reports whether a sequence is empty. For instance, the following query returns books that have authors, but does not return the one book that has only editors:

 for $b in doc("books.xml")//book where not(empty($b/author)) return $b 

The opposite of empty() is exists() , which reports whether a sequence contains at least one item. The preceding query could also be written as follows :

 for $b in doc("books.xml")//book where exists($b/author) return $b 

XQuery also has functions that access various kinds of information associated with a node. The most common accessor functions are string() , which returns the string value of a node, and data() , which returns the typed value of a node. These functions require some explanation. The string value of a node includes the string representation of the text found in the node and its descendants, concatenated in document order. For instance, consider the following query:

 string((doc("books.xml")//author)[1]) 

The result of this query is the string "Stevens W." (The exact result depends on the whitespace found in the original document ”we have made some assumptions about what whitespace is present.)



XQuery from the Experts(c) A Guide to the W3C XML Query Language
Beginning ASP.NET Databases Using VB.NET
ISBN: N/A
EAN: 2147483647
Year: 2005
Pages: 102

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net