October 30, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

XQuery Language Expressions

  • March 19, 2004
  • By Kurt Cagle
  • Send Email »
  • More Articles »

Namespaces

The XQuery language has its own specific (default) namespace that defines the functions that are commonly available. Although you can create functions into that namespace (which you are doing implicitly when you create a function without a namespace prefix), you stand the possibility of overwriting a system function with one of your own. In the current context, where all functions are local, this is not necessarily a bad thing, but in situations where more than one person is relying on these functions, that situation could prove disastrous.


Namespaces Can Be Useful, Not Just a Nuisance - People who work with XML only periodically sometimes see namespaces as something of a nuisance. Namespace identifiers are often long and unwieldy, and namespace prefixes can make a seemingly straightforward block of XML seem much more complex. However, namespaces can come in handy.

For instance, I've dealt with XML schemas describing such things as framework components (such as describing a form in Visual Basic via XML). A number of us were working on this application, and it was very important to ensure that the XML code one person wrote with samples wouldn't end up contaminating the base code until it had properly been approved.

To get around this, each developer was assigned their own namespace. The interpreter of the XML was programmed so that only certain namespaces would be enabled in each respective build (that is, you could run your own test-code, but other people running this code without the namespace enabled wouldn't have your functionality). After the XML code was deemed to be working correctly, a particular code's prefixes were swapped over to the formal implementation.

This system worked surprisingly well, especially as we moved more of our operant code into XML form. Without the namespaces, it would have been impossible to keep the coding straight; with them, not only could we tell at a glance whose code we were dealing with, but the software applications could use the same namespaces to assign functionality.


The declare namespace command lets you define other namespaces for use within function declarations. For instance, you might decide to create a package of date functions, and associate them with a given namespace URI (http://www.kurtcagle.net/schemas/xquery/date, for instance). This namespace would then be used to refer to all functions within that package as follows:

declare namespace dates = "http://www.kurtcagle.net/schemas/xquery/date"

define function dates:format-date($dt as xs:dateTime) as xs:string
{
   let $refDateStr := string($dt)
   let $year := substring($refDateStr,1,4)
   let $month:= substring($refDateStr,6,2)
   let $day := substring($refDateStr,9,2)
   return concat($month,'/',$day,'/',$year)
}

<html>
   <body>
   <h1>File Report</h1>
   <table>
      <tr>
         <th>File Name</th>
         <th>Date Created</th>
         <th>Date Last Modified</th>
      </tr>
{for $file in document('directory.xml')//file
return
   <tr>
      <td>{string($file/@name)}</td>
      <td>{dates:format-date($file/@dateCreated)}</td>
      <td>{dates:format-date($file/@dateModified)}</td>
   </tr>
}
      </table>
   </body>
</html>

By doing this, you avoid the problem of namespace collision, and not coincidentally, make it easier to organize your code.

Code Libraries

The second issue is a little more irksome, and has to do with the creation of code libraries. One of the principal reasons for working with functions is the capability to build function libraries that you (and others working in the same space) can use in your own code.

Currently (as of November 15, 2002) there is no provision within the XQuery specification for indicating code libraries, although there is an open-issue item concerning it. The primary difficulty in working with such external libraries revolves around the fact that such function libraries should realistically be in their own namespaces.

One speculative form for adding such functional libraries might look something like the following for the date library:

import "dateFunctions.xquery" in namespace dates = "http://www.kurtcagle.net/
schemas/xquery/dates"

In this case, dateFunctions.xquery is a URL that contains all the custom functions associated with dates. Once declared in this manner, the functions require the namespace prefix to be invoked (for example, dates:format-date($myDate)). Note that by associating the namespace (and its prefix) with the function set, you can use a different prefix than any defined within the imported XQuery call.

Ultimately, functions in XQuery serve much the same purpose as stored procedures (SPROCS) within SQL: They simplify the coding involved within queries, and also form a mechanism for encapsulating business logic within queries (a topic to be covered in Chapter 4 and elsewhere). This becomes critically important in dealing with pipelined architectures, in which the XQuery acts as a filter on a dataset to be passed to another component (such as a Web service, or an XSLT transform). Expect to see more on function libraries in the final specification.

In Brief

  • XQuery is a declarative language. After an item is defined within a given scope (such as a program block or function), the item can't be redefined within that scope.

  • The let statement defines a function that has a constant value.

  • The for keyword assigns to a temporary variable successive values in a sequence.

  • The return keyword takes the sets defined by for and let statements and turns them into a result—XML, a sequence of values, a single string expression, and so on.

  • The where keyword lets you define conditions about which data should be chosen. It creates a predicate cause that narrows a range of potential options.

  • The order by command follows for expressions (and where expressions where they exist) and indicates for a given set of data the order in which the data is output. When it is not explicitly specified with order by, the output order depends on the system architecture.

  • XQuery works with XPath's conditional if/then/else keywords to create logical expressions.

  • XQuery provides many built-in functions and also lets you create user-definable functions. Anything passed into an XQuery function is passed by value, not reference, and a function must always return something of value.

  • About the Author

    Kurt Cagle was a founding writer and frequent contributor for Fawcette's XML and Web Services Magazine, and has spoken on XSLT, SVG, and XSL-FO issues at more than a dozen conferences in the last five years. Kurt wrote his first book on XML in 1998, was one of the first authors to concentrate on the XSLT 1.0 specification for the Microsoft environment in Sybex's XML Developer's Handbook, and was a contributing author to the best selling Beginning XML for Wrox Press, among thirteen other books. His current book is XQuery Kick Start, published by Sams Publishing. He lives in Kirkland, Washington with his wife and two daughters, and writes novels when he isn't writing computer books.






Page 5 of 5



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel