N. America: 800 876 3101 | World: 44 (0) 1753 218 930

Library Modules in XQuery

Functions can be put in library modules, which can be imported by any query. Every module in XQuery is either a main module, which contains a query body to be evaluated, or a library module, which has a module declaration but no query body. A library module begins with a module declaration, which provides a URI that identifies the module for imports, as shown in Listing 1.20.

Listing 1.20 Module Declaration for a Library Module

module "http://example.com/xquery/library/book"

define function toc($book-or-section as element())
as element()*

{
for $section in $book-or-section/section
return

<section>
{ $section/@* , $section/title , toc($section) }
</section>
}

Functions and variable definitions in library modules are namespacequalified. Any module can import another module using a module import, which specifies the URI of the module to be imported. It may also specify the location where the module can be found:

import module "http://example.com/xquery/library/book"
at "file:///c:/xquery/lib/book.xq"

The location is not required in an import, since some implementations can locate modules without it. Implementations are free to ignore the location if they have another way to find modules.

A namespace prefix can be assigned in a module import, which is convenient since the functions in a module can only be called if a prefix has been assigned. The following query imports a module, assigns a prefix, and calls the function:

import module namespace b = "http://example.com/xquery/library/book"
at "file:///c:/xquery/lib/book.xq"

<toc>

{

for $s in doc("xquery-book.xml")/book

return b:toc($s)

}
</toc>

When a module is imported, both its functions and its variables are made available to the importing module.

Stay Informed!

XQuery is one of the hottest XML technologies being developed today. Stay informed with vital news about standards, tools, and trends by signing up for the DataDirect XQuery® newsletter.

Back to School with DataDirect XQuery®

Getting your mind around the possibilities of a data integration technology as promising as XQuery can be difficult, but our XML developers Webinars will help you understand the power and versatility of XQuery, and our favorite XQuery processor, DataDirect XQuery®.

From information about how to leverage relational data to real-world use cases, our Webinars will help you see why DataDirect XQuery® is the best choice for today's tough data integration and aggregation challenges.

Use DataDirect XQuery® to Mine Relational Data!

This informative XQuery webinar shows you how scalable, flexible, and reliable DataDirect XQuery® is for building XML applications that access data from relational databases, XML, Web services and legacy formats; and how it uses document projection and data streaming technologies to efficiently query large (Gigabytes) XML documents.

Check our XQuery Webinars page for more information on this and other Webinar topics.