Loading TOC...
Matches for cat:guide (cat:guide/xquery (cat:guide/xquery (cat:guide))) have been highlighted. remove
XQuery and XSLT Reference Guide (PDF)

MarkLogic Server 11.0 Product Documentation
XQuery and XSLT Reference Guide
— Chapter 8

Application Programming in XQuery and XSLT

In MarkLogic Server, XQuery and XSLT are not only used to query XML, but are also used as programming languages to create applications. They are especially powerful as a programming languages to create web applications, as you can easily write XQuery and/or XSLT code that outputs XHTML, which is the XML variant of HTML. This chapter describes some of the language features that make XQuery and XSLT particularly useful as application programming languages, and includes the following sections:

Design Patterns

For any programming language, there are design patterns that develop over time to perform various tasks. In XQuery with MarkLogic Server, one design pattern developers have gravitated toward is using MarkLogic Server to create single-tier applications, where an XQuery program accesses the content in a database, prepares it for display to an application, and sends the results to a client over an HTTP App Server.

Many of the extensions in the 1.0-ml enhanced XQuery dialect make building these types of applications easier and more efficient. Extensions to the language such as try/catch are very useful in building robust applications. For details on these extensions, see MarkLogic Server Enhanced XQuery Language.

The Application Developer's Guide lists many common design patterns in MarkLogic Server, and the Search Developer's Guide lists common design patterns for MarkLogic Server specific search application functionality. These guides provide details about searches, lexicons, and many other techniques developers use to build applications in MarkLogic Server.

Using Functions

Functions are a powerful way to encapsulate XQuery code. For an example of an XQuery function, see Declaring Functions. This section covers the following aspects of functions:

Creating Reusable and Modular Code

Functions provide a convenient way to modularize or componentize your XQuery code. When you move some functionality into a function in a library module, it allows you to call that library module and use any of its functions from any other XQuery module, allowing maximum code reuse. You can separate the library modules any way that makes sense for your development environment. For example, you can use a model-view-controller (MVC) approach where you have a set of functions that are used to access the content, a set of functions used to display the content in a user-interface, and a set of functions used to control the business logic of the application (for example, workflow logic based on various events).

Recursive Functions

Using functions recursively (creating functions that call themselves) is a useful design pattern in XQuery. Recursive functions are very convenient for iterating through an XML tree structure to perform XML transformations from one structure to another.

Note that MarkLogic will apply tail call optimization to a recursive XQuery function if and only if the function return type is untyped. For example:

(: Can be tail call optimized - no explicit return type :)
declare function my:func(
  $param as xs:string
) {
  ...
};

(: Cannot be tail call optimized - explicitly returns node() :)
declare function my:func(
  $param as xs:string
) as node() {
  ...
};

Recursive functions that are not tail call optimized create a new stack frame for each call and can eventually cause a stack overflow if the call stack gets too deep. By contrast, tail call optimized recursive functions use constant stack space.

For details on performing recursive transformations, see the Transforming XML Structures With a Recursive typeswitch Expression chapter of the Application Developer's Guide.

You can also use XSLT to perform transformations. For more information about XSLT, see XSLT in MarkLogic Server.

Search Functions

MarkLogic Server includes functions to perform high-performance full-text search queries. The cts:query constructors allow you to compose complex queries. The cts:search API returns relevance-ranked, search-engine style queries. The cts:contains API can be used in XPath predicates or other XQuery expressions. Both cts:search and cts:contains take the composable cts:query APIs as a parameter, allowing you to perform full-text searches in any XQuery or XSLT context, whether it is on content stored in a database or on content constructed in memory.

There are many index settings on the database configuration. The indexes speed up searches (both XPath and cts:search) on documents in the database. The default index settings provide a good mix of performance and economy of disk space, and the default settings work well in many applications. If you want more index options, you can configure them at the database level.

For details on composing cts:query constructors, see Composing cts:query Expressions in the Search Developer's Guide. For the syntax of the various search built-in functions, see the MarkLogic XQuery and XSLT Function Reference. For details on index options, see the Databases and Text Indexing chapters of the Administrator's Guide.

Updates and Transactions

MarkLogic Server is a transactional system that ensures data integrity. When you perform updates on documents in a database, the system automatically locks any needed documents to ensure those documents are not updated by any other concurrent transactions. If a query reads a document, the system ensures that it the query reads a consistent view of the document throughout the transaction.

There are XQuery/XSLT functions built into MarkLogic Server to create documents, update documents, and delete documents in a database. These update built-in functions are used in XQuery programs, so you can build complex logic (or whatever is required by your application) into your programs that update content.

For details on transactions, see the Understanding Transactions in MarkLogic Server chapter in the Application Developer's Guide. For details on the update built-in functions, see the MarkLogic XQuery and XSLT Function Reference.

HTTP App Server Functions

When you issue XQuery requests against a MarkLogic Server HTTP App Server, the requests are processed over the HTTP protocol. MarkLogic Server provides XQuery built-in functions to perform various HTTP server functions. Use these functions to HTTP-server related actions such as adding an HTTP header, accessing the request object, and so on.

The App Server functions are extremely useful when you are creating complete applications that return XHTML. For details about the signatures of the App Server functions, see the MarkLogic XQuery and XSLT Function Reference.

Additional Resources

This section lists some sources for additional XQuery resources. They include:

MarkLogic Server Documentation

In addition to this document, which describes the XQuery language implemented in MarkLogic Server, the MarkLogic Server documentation also includes XQuery API documentation for all of the XQuery-standard functions as well as the MarkLogic-defined XQuery functions. Included in the API documentation are many useful XQuery code samples.

The other documents in the MarkLogic Server library describe various other aspects of the product. In particular, the Application Developer's Guide includes many useful XQuery design patterns that work well with MarkLogic Server. For a description of MarkLogic Server documentation, see the product documentation section of the MarkLogic Developer site (http://developer.marklogic.com/).

XQuery Use Cases

MarkLogic Server includes an application that shows the XQuery Use Cases. The Use Cases have been developed by the W3C XQuery Working Group and demonstrates how a significant number of core tasks can be implemented using the XQuery language. The W3C describes the use cases in the following document:

http://www.w3.org/TR/xquery-use-cases/

The Use Cases have a default XQuery dialect of 1.0, so if you want to run code in 1.0-ml, use an XQuery version declaration in the prolog, as described in Specifying the XQuery Dialect in the Prolog. The Getting Started with MarkLogic Server walks you through this process of using the Use Cases application some detail.

Other Publications

In addition to the MarkLogic Server documentation, there are many excellent third-party books on XQuery. See the MarkLogic developer site for some recommendations (http://developer.marklogic.com).

You can also look directly at the XQuery specification, although much of the specification is geared more toward people who are implementing an XQuery processor rather than for people who are writing applications in XQuery. Nevertheless, it is very useful to at least get some familiarity with the following specifications:

« Previous chapter