Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Introduction to XQuery (Part 1 of 4) : Page 2

XML blurs the distinctions between databases, documents, and messages, but it needs a powerful and elegant query language to reach its full potential. XPath does not go far enough. XQuery intends to be that language.


advertisement
Structure of an XQuery Module
If you're familiar with other query languages such as SQL, OQL, you'll notice that XQuery has some similarities, and you'll probably find some of the syntax to be quite familiar. The term "query module" as used in this article means a "query unit". You can think of an XQuery module as having three parts (see Figure 1).
  • Namespace and Schema Declarations [optional]
  • Function Definitions [optional]
  • Query Expressions
Figure 1: The three parts of a query module.
The first two parts together are called the query prolog. The first part can consist of both namespace declarations and schema import statements. The example in Figure 1 defines a namespace prefix xsd mapped to the URI "http://www.w3.org/2000/01/XMLSchema". The second part contains function definitions. This is the place in a query module to define custom functions. The example in Figure 1 defines a factorial function that accepts an integer parameter and returns its factorial.

The third part of the query contains query expressions. Expressions are the key to XQuery. The example in Figure 1 uses an expression called an Element Constructor, which, as the name suggests, constructs an element named Results. The example invokes the factorial function with a parameter value 10, and the result becomes the content of the Value element.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap