Solutions

MarkLogic Data Hub Service

Fast data integration + improved data governance and security, with no infrastructure to buy or manage.

Learn More

Learn

Stay On Top Of Everything MarkLogic

Be the first to know! News, product information, and events delivered straight to your inbox.

Sign Me Up

Community

MarkLogic World 2019

Learn how to simplify data integration & build innovative applications. Join us in Washington D.C. May 14-15!

Find Out More

Company

Stay On Top Of Everything MarkLogic

Be the first to know! News, product information, and events delivered straight to your inbox.

Sign Me Up

Functions

The XQuery language supports declaring functions and grouping them together in library modules. As your code grows more complex this helps keep it manageable. Here's the above query rewritten to use some local functions (which use the "local" namespace) to build the query piece-by-piece. Notice there's typing information for variables and return values. That's optional but usually a good idea. Each type accepts a modifier the same as in regular expressions: asterisk means 0 to n many, question mark means 0 or 1, plus means 1 to n many, and no modifier means exactly one.

To break functions into library modules you put them in a separate file and give them a special header. Here's file test-lib.xqy (which is already installed on the box for you):

And here's how you import the library and use it:

Search Relevance

Query-Limited Facets

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.