Marklogic Server: Xquery and XSLT Reference Guide
Marklogic Server: Xquery and XSLT Reference Guide
MarkLogic 10
May, 2019
Table of Contents
This XQuery and XSLT Reference Guide briefly describes some of the basics of the XQuery
language, but describes more thoroughly the MarkLogic Server implementation of XQuery,
including many of the important extensions to the language implemented in MarkLogic Server.
Additionally, it describes how to invoke XSLT stylesheets and briefly describes the MarkLogic
Server XSLT 2.0 implementation.
The next two chapters (“XQuery Dialects in MarkLogic Server” on page 7 and “MarkLogic
Server Enhanced XQuery Language” on page 15) focus on the MarkLogic Server-specific aspects
of the XQuery language. If you prefer to start with the more generic aspects of the XQuery
language before moving to the MarkLogic Server-specific parts, start with “XQuery Language”
on page 32.
• The different dialects of XQuery supported in MarkLogic Server (see “XQuery Dialects in
MarkLogic Server” on page 7).
• MarkLogic extensions to the XQuery language (see “MarkLogic Server Enhanced
XQuery Language” on page 15).
• An overview of the basic syntax of the XQuery language (see “XQuery Language” on
page 32).
• A brief description of XPath syntax (see “XPath Quick Reference” on page 57).
• An introduction to how namespaces work in XML and XQuery (see “Understanding XML
Namespaces in XQuery” on page 73).
• Using XSLT in MarkLogic Server (see “XSLT in MarkLogic Server” on page 83).
• Some information on how XQuery and XSLT are used as application development
programming languages in MarkLogic Server (see “Application Programming in XQuery
and XSLT” on page 90).
The XQuery specification is a formal recommendation from the W3C XQuery Working Group.
MarkLogic 10 implements the W3C XQuery 1.0 Recommendation (http://www.w3.org/TR/xquery/).
To maximize compatibility with MarkLogic Server and to offer strict XQuery compliance to
those who desire it, as well as to include extensions to the language to make it easier to build
applications, MarkLogic Server supports two dialects of XQuery. This chapter describes these
dialects, and includes the following sections:
You can use library modules from different dialects together, as described in “Rules For
Combining the Dialects” on page 8. Each dialect has a different set of pre-defined namespaces, as
described in “Predefined Namespace Prefixes for Each Dialect” on page 79.
Note the semi-colon at the end of the declaration, which is required in 1.0-ml. The enhanced
dialect has the XQuery 1.0 syntax and also includes various extensions to the language such as
try/catch. This dialect is the default for new App Servers, and is considered the preferred dialect
for new applications. For more details on the enhanced 1.0-ml dialect, see “MarkLogic Server
Enhanced XQuery Language” on page 15.
Note the semi-colon at the end of the declaration, which is required in 1.0. The strict mode is for
compatibility with other XQuery 1.0 processors; if you write a library in 1.0, you can use it with
MarkLogic Server and you can also use it with other conforming processors. Similarly, you can
use modules that are written in standard XQuery with MarkLogic Server.
To use the MarkLogic Server built-in functions in 1.0, you must bind a prefix (for example, xdmp)
to the namespace for the MarkLogic Server functions; there is no need to import a library for
these built-in functions, but you do need to bind the namespace to a prefix. To use the xdmp
functions in 1.0, add prolog entries for the namespace bindings you are using in your query, as in
the following example:
xdmp:version()
When writing modules of different dialects, the best practice is to always use the XQuery version
declaration as the first line, indicating which dialect the module is written in. That way, if the
module is written in a different dialect than the default dialect for the App Server or the program,
it will still work correctly (for details, see “Inheriting the Default XQuery Version From the App
Server” on page 13).
• Migrate an entire application all at once. This method gets everything over with at once,
and therefore focuses the effort. If you have a relatively small amount of code to migrate,
it might make sense to just go ahead and migrate it all at once.
• Migrate one module at a time. This method allows you to spread the migration work over
a number of small tasks instead of one large task, and further allows you to test each
module independently after migration. This technique is very flexible, as you can do a
little bit at a time. A good first step for this one-by-one approach is to start by adding an
XQuery 0.9-ml declaration to the first line of each XQuery file. Then, as you migrate a
module, you can change the declaration to 1.0-ml and make any needed syntax changes to
that module.
Note: MarkLogic has deprecated XQuery 0.9-ml and will be removing it in a future
release.
While MarkLogic Server currently allows 0.9-ml code to run without changes, the dialect is
deprecated and will be removed in a future release. The new XQuery dialect 1.0-ml has
enhancements. Because you can mix modules in the old dialect with modules in the new, you can
perform your migration one module at a time. This section highlights the major syntax and
semantic changes between the XQuery used in 0.9-ml and enhanced XQuery dialect 1.0-ml. The
changes fall into the following categories:
• If you are modifying a main module and it has function declarations that are used in the
same module, they must be declared in a namespace. Library module declarations now
require the namespace keyword and a prefix for the namespace, for example:
Note: Functions in a main module must be put into the local: namespace.
• Function declarations that return the empty sequence now require the empty sequence to
be specified as follows:
empty-sequence()
The 0.9-ml dialect had you specify empty() for the empty sequence.
• Some of the effective boolean value rules have changed. Notably, the following returns
true in 0.9-ml and returns false in 1.0-ml (and throws an exception in 1.0):
This change might affect applications that have if/then/else statements where the if test
returns a sequence of boolean values. In these cases, you might see the if statement
evaluating to false in cases where it previously evaluated to true, causing the else
statement to be evaluated intead of the then statement.
• String functions treat empty arguments as equivalent. For example, the following
statements, while not equivalent, are treated the same:
substring((), 1);
substring("",1)
In the first line, “()” is parsed identically to “""” in the second line.
2.4.3 Inheriting the Default XQuery Version From the App Server
Each App Server has a setting for the default XQuery version. Any requests against that App
Server that do not have explicitly specify an XQuery version declaration are treated as the default
XQuery version value. Because of the way a request inherits it default XQuery version from the
App Server environment, requests without an explicit declaration can be treated differently by
different App Servers (if the App Servers have different default XQuery values). Therefore, it is
best practice to specify the XQuery version in each module.
The task server does not allow you to specify a default XQuery version, and if there is no explicit
version declaration in the XQuery code evaluated on the task server, the default XQuery version is
determined as follows:
To ensure your code is always evaluated in the dialect in which you have written it, regardless of
the context in which it is run, the best practice is to begin each XQuery module with a XQuery
version declaration. For the syntax of the version declaration, see “XQuery Version Declaration”
on page 35.
The following are some basic steps to take when migrating 0.9-ml XQuery code to 1.0-ml:
1. Add XQuery version declarations to all of your existing modules. For code written in
0.9-ml, the declarations will be as follows:
Note: If you use the xquery version "0.9-ml" declaration, you will get a warning
message. MarkLogic recommends that you move to either XQuery dialect 1.0-ml
or 1.0 as soon as possible.
3. For each module you migrate, change the version number string in the XQuery version
declaration to 1.0-ml and add a semi-colon to the line so it appears as follows:
4. Change all of the prolog declarations to the 1.0 syntax (change define to declare, add
semi-colons, and so on, as described in “XQuery Changes from 0.9-ml to 1.0-ml” on
page 9). For the prolog syntax, see “XQuery Prolog” on page 37, the W3C specification
(http://www.w3.org/TR/xquery/#id-grammar), or a third-party XQuery book.
5. If you are modifying a main module and it has function declarations that are used in the
same module, they must be declared in a namespace. The preferred way to put functions
local to a main module is to prefix those functions definitions and function calls with the
local: prefix, which is predefined.
6. If you have any durations that use the xdt namespace prefix, change the prefix to xs (for
example, change xdt:dayTimeDuration to xs:dayTimeDuration).
7. If you are modifying a library module that is defined with the fn namespace URI, you
must change the namespace URI of that module; you cannot use the URI bound to the fn
namespace prefix as the URI for a library module in 1.0 or 1.0-ml. If you do change the
namespace URI of a library module, you must also change the URI in any import module
statements in other modules that call the library.
8. Test the module and correct any syntax errors that occur.
9. After getting the module to run, test your code to make sure it behaves as it did before. Pay
particular attention to parts of your code that might rely on boolean values that take
boolean values of sequences, as those behave differently in 0.9-ml and 1.0-ml (see
“XQuery Changes from 0.9-ml to 1.0-ml” on page 9). Check for any changes due to
function mapping, which is described in “Function Mapping” on page 17.
10. Repeat this process for other modules you want to migrate.
The default XQuery dialect in MarkLogic Server is enhanced. (1.0-ml) The enhanced dialect
includes all of the features in the strict XQuery 1.0 dialect, and adds several other features to
make it easier to use XQuery as a programming language with which to create applications. This
chapter describes the features of the enhanced dialect and includes the following sections:
• try/catch Expression
• Function Mapping
• validate as Expression
• Serialization Options
• Implementation-Defined Semantics
For details on the XQuery language, see “XQuery Language” on page 32 and the W3C XQuery
specification (http://www.w3.org/TR/xquery/).
The following code sample uses a try/catch block to catch exceptions upon loading a document,
and prints out the filename if an exception occurs.
try {
let $filename := "/space/myfile.xml"
let $options := <options xmlns="xdmp:document-load">
<uri>/myfile.xml</uri>
<repair>none</repair>
</options>
Most exceptions can be caught with a try/catch block, but the XDMP-CANCELED, SVC-CANCELED, and
XDMP-DISABLED exceptions cannot be caught in a try/catch block.
When an exception is thrown by code within a try block, all actions taken in that block are rolled
back. If you catch the exception (and do not throw another), then MarkLogic will evaluate
expressions occuring after the try-catch expression.
For example, in the following code, the call to xdmp:document-set-metadata data throws an
XDMP-CONFLICTINGUPDATES exception because it tries to update the document metadata twice in the
same statement. The exception is trapped by the try-catch. The updates in the try block are lost, so
“doc.xml” is not created. The “hello” expression is still evaluated.
By contrast, if you wrap only the call to xdmp:document-set-metadata in the try-catch block, then
the initial document insert still occurs.
Note that Server-Side JavaScript code does not handle JavaScript statements within a try block
the same way as XQuery handles expressions in a try block. In JavaScript, statements in the try
block that complete before the exception occurs are not rolled back if the exception is caught. For
details, see Exception Handling in the JavaScript Reference Guide.
Function mapping also works on multiple singleton parameters, resulting in the cross product of
all the values (equivalent to nested for clauses). In the case of multiple mappings, they occur left
to right. For example, the following is evaluated like a nested for loop:
One consequence of function mapping, which can be surprising the first time you see it, is that if
the value passed for a parameter is the empty sequence, it could result in the function being called
0 times (that is, in the function never runs and results in the empty sequence. For example, if you
entered the empty sequence as the parameter to the above function call, it returns empty, as
follows:
local:print-word( () )
(:
evaluates the print-word function zero times, resulting
in the empty sequence
:)
The local:print-word function is never called in this case, because it is iterating over the empty
sequence, which causes zero invocations of the function. If your function calls are fed by code
that can return the empty sequence (an XPath expression, for example), then you might see this
behavior.
Similarly, you can explicitly disable function mapping in 1.0-ml by adding the following to the
prolog:
If you run code expecting it to map singletons to a sequence if function mapping is diabled, it will
throw an exception because the sequence cannot be cast to a single string.
Note: If you use item operators on a sequence, in the 1.0-ml dialect they will perform
function mapping, and the value will be the effective boolean value of the
sequence of results. In the 1.0 dialect, they will throw an XDMP-MANYITEMSEQ
exception if you try to compare a sequence of more than one item. For more
details, see Item Operators.
Note that you cannot use the semi-colon as a transaction separator in the strict XQuery dialect
(1.0). For more details on transactions, see Understanding Transactions in MarkLogic Server chapter
in the Application Developer’s Guide.
Note that functions and variables in a main module are private by definition, so declaring them
private only makes sense for library modules.
Side effects are extremely useful when building applications. Therefore, MarkLogic Server
includes many functions that have side effects. The following are some examples of functions
with side effects:
• xdmp:set
In XQuery 1.0 strict mode (1.0), you must use the fn:position() function as in the following
example:
The following example shows an XQuery module that imports a stylesheet and runs a function in
the stylesheet:
foo:foo()
Similarly, you can import an XQuery module into an XSLT stylesheet, as described in “Importing
XQuery Function Libraries to a Stylesheet” on page 84.
Note: To use functions and variables from a stylesheet in XQuery, define them in a
namespace in the stylesheet. In XQuery, it is difficult to call functions and
variables in no namespace. Therefore, the best practice is, for functions and
variables in a stylesheet that you plan to import into an XQuery module, define
them in a namespace. Note that in an XQuery library module, all function and
variable declarations must be in a namespace.
• Arrow Operator
• Inline Functions
• Function Annotations
• Switch Statement
In the above example, the map produced by calling map:map or map:with is implicitly the first
param of the applied function (map:with, here).
Without the arrow operator, you would need to make repeated calls to map:put or repeated or
nested calls to map:with, as shown below. The use of the arrow operator can result in more
readable code.
(: using map:put :)
let $map := map:map()
let $_ := map:put($map, $key1, $value1)
let $_ := map:put($map, $key2, $value2)
return $map
(: using map:with :)
map:with(map:with(map:map(), $key1, $value1), $key2, $value2)
For more details, see the discussion of the arrow operator in the XQuery 3.1 specification at
https://www.w3.org/TR/2017/REC-xquery-31-20170321/.
PathExpr1 ! PathExpr2
PathExpr1 is evaluated, and then each item in the resulting sequence acts as the inner focus when
evaluating PathExpr2.
The following example finds all the //child elements of $nodes, and then uses each element as the
context item (“.”) in a call to fn:concat.
For more details, see the discussion of the Simple Map Operator in the XQuery 3.0 specification
at https://www.w3.org/TR/xquery-30/#id-map-operator.
For more details, see the discussion of String Concatenation Expressions in the XQuery 3.0
specification at https://www.w3.org/TR/xquery-30/#id-string-concat-expr.
Q{namespaceURI}local_name
For example, the element <x:p/> in the following node can be referenced as
Q{http://example.com/ns/foo}p.
For more details, see the discussion of Expanded QNames in the XQuery 3.0 specification at
https://www.w3.org/TR/xquery-30/#dt-expanded-qname.
(: returns "ab" :)
For more details, see the discussion of dynamic function calls in the XQuery 3.0 specification at
https://www.w3.org/TR/xquery-30/#id-dynamic-function-invocation.
The following example passes an inline function as the first parameter of fn:map:
(: returns (20,40) :)
For more details, see the discussion of Inline Function Expressions in the XQuery 3.0
specification at https://www.w3.org/TR/xquery-30/#dt-inline-func.
For more details, see the Function Test in the XQuery 3.0 specification at
https://www.w3.org/TR/xquery-30/#id-function-test.
For example, the following code creates a reference to the version of the function
local:doSomething that accept two parameters, and then invokes the function through the
reference:
You can also create references to functions defined by XQuery and MarkLogic. For example:
fn:concat#3 signifies a reference to fn:concat expecting 3 parameters.
For more details, see the following topic in the XQuery 3.0 specification at
https://www.w3.org/TR/xquery-30/#id-named-function-ref.
For example, the following function reference specifies the value 10 for the first parameter of the
referenced function, and uses a placeholder for the second parameter:
You can invoke the function through the reference and supply only one parameter, which will take
the place of the placeholder parameter. For example:
{ $a + $b };
(: returns 13 :)
For more details, see the discussion of partial function application in the XQuery 3.0 specification
at https://www.w3.org/TR/xquery-30/.
MarkLogic supports the annotations defined by the XQuery 3.0 specification. MarkLogic also
defines the following implementation-specific annotations:
The following example defines an external variable with the default value "my default value".
For more details, see VarDefaultValue in the Variable Declaration topic of the XQuery 3.0
specification at https://www.w3.org/TR/xquery-30/#id-variable-declarations.
For example, the case clause in the following typeswitch matches either a name or address
element.
typeswitch($some-node)
case $n as element(name) | element(address) return $n
default return ()
For more details, see the discussion of SequenceTypeUnion in the Typeswitch topic of the
XQuery 3.0 specification at https://www.w3.org/TR/xquery-30/#doc-xquery30-CaseClause.
For example, the following code selects a code path based on the value of a variable.
(: returns "two" :)
You can use a switch statement that tests the value fn:true() as a “shortcut” for a nested set of
if-then-else expressions. For example:
switch (fn:true)
case ($a > 0) return "positive"
case ($a < 0) return "negative"
default return "zero"
For more details, see the Switch Expression topic in the XQuery 3.0 specification at
https://www.w3.org/TR/xquery-30/#id-switch.
The following example specifies a validation level. If you omit the leve, “strict” is implied.
</query>
return validate strict { $query }
For more details, see the discussion of Validate Expressions in the XQuery 3.0 specification at
https://www.w3.org/TR/xquery-30/#id-validate.
The MarkLogic-specific and XQuery standard try/catch expressions differ in the following ways:
• In the standard implementation, the catch clause uses a name test to determine whether or
not to trap a given error. This enables you to trap specific exceptions by name. The
proprietary implementation traps any exception.
• The standard implementation pre-defines several variables in the scope of the expression
evaluated in the catch block. These variables provide details about the error. The
proprietary implementation binds an error element to a variable you specify in your catch
clause, and then you access error details through that variable.
You cannot trap MarkLogic errors such as XDMP-AS by name with the standard implementation
because MarkLogic errors do not have QNames. However, you can trap the XQuery standard
error codes or all errors (“*”) with the standard try/catch expression.
The following is an example of an XQuery standard try/catch expression. It traps all all
exceptions and prints out a message constructed from some of the implicitly defined variables.
For more details, see the Try Catch Expressions discussion in the XQuery 3.0 specification at
https://www.w3.org/TR/xquery-30/#id-try-catch.
http://www.w3.org/TR/xquery/#id-impl-defined-items
This section describes the following implementation-defined items as they are implemented in
MarkLogic Server:
• Namespace path
• External Variables
• Collations
Note: Except where noted, the items in this section apply all of the XQuery dialects
supported in MarkLogic Server.
Note: The fn: prefix is bound to a different namespace in 1.0 and 1.0-ml than in 0.9-ml.
For example, the following code adds http://marklogic.com/xdmp to the namespace path:
An XQuery program that accepts external variables must declare the external variables in its
prolog, as in the following code snippet:
You can create a default value for the variable by adding the := to the specification, as in the
following code snippet:
An XQuery program with this variable declaration would be able to use the string values passed
into it via an external variable with the QName my:variable (where the namespace prefix my was
declared somewhere in both the calling and called environments). You could then reference this
variable in the XQuery program as in the following example:
If you then call this module as follows (assuming the module can be resolved from the path
/extvar.xqy.
Note: MarkLogic Server will not accept more than 1024 external variable value items
over XDBC.
3.12.4 Collations
The XQuery specification allows collation names and default collation values to be determined by
the implementation. MarkLogic Server uses collations to specify the sort order of strings, and it
defines the URIs for the collations. Each query runs with a default collation, and that collation can
come from the environment (each App Server has a default collation setting) or it can be specified
in the XQuery program. Also, you can specify collations for string range indexes and for word
lexicons to specify their sort order. For details about collations in MarkLogic Server, including
the valid URIs for collations, see Encodings and Collations in the Search Developer’s Guide.
The chapter describes selected parts of the XQuery language. It is not a complete language
reference, but it touches on many of the widely used language constructs. For complete details on
the language and complete syntax for the language, see the W3C XQuery specification
(http://www.w3.org/TR/xquery/). Additionally, there are many third-party books available on the
XQuery language which can help with the basics of the language. This chapter has the following
sections:
• XQuery Modules
• XQuery Prolog
• XQuery Comments
• XQuery Expressions
Note: This chapter describes a subset of the XQuery 1.0 recommendation syntax, which
is used in the 1.0 and 1.0-ml dialects. For an overview of the different XQuery
dialects, see “XQuery Dialects in MarkLogic Server” on page 7.
Any valid XQuery expression is a valid XQuery. For example, the following is a valid XQuery:
"Hello World"
It returns the string Hello World. It is a simple string literal, and is a valid XQuery. You can
combine expressions together using the concatenation operator, which is a comma ( , ), as
follows:
"Hello", "World"
This expression also returns a sequnce of two string Hello and World. It is two expressions, each
returning a single item (therefore it returns a sequence of two strings). In some contexts (a
browser, for example), the two strings will be concatenated together into the string Hello World.
Expressions can also return no items (the empty sequence), or they can return sequences of items.
The following adds a third expression:
"Hello", "World", 1 to 10
This expression returns the sequence Hello World 1 2 3 4 5 6 7 8 9 10, where the sequence
1 to 10 is a sequence of numeric values. You can create arbitrarily complex expressions in
XQuery, and they will always return zero or more items.
• Direct Element Constructors: Switching Between XQuery and XML Using Curly Braces
It simply returns the element. The XQuery syntax also allows you to embed XQuery between
XML, effectively “switching” back and forth between an XML syntax and an XQuery syntax to
populate parts of the XML content. The separator characters to “switch” back and forth between
XML and XQuery are the open curly brace ( { ) and close curly brace ( } ) characters. For
example, consider the following XQuery:
<my-element>{fn:current-date()}</my-element>
This expression returns an XML element named my-element with content that is the result of
evaluating the expression between the curly braces. This expression returns the current date, so
you get an element that looks like the following:
<my-element>2008-06-25-07:00</my-element>
You can create complex expressions that go “back and forth” between XML and XQuery as often
as is needed. For example, the following is slightly more complex:
<my-element id="{xdmp:random()}">{fn:current-date()}</my-element>
<my-element id="9175848626240925436">2008-06-25-07:00</my-element>
This technique of constructing XML are called direct element constructors. There are many more
rules for how to use these direct element constructors to create XML nodes. For more details, see
the of the XQuery specification (http://www.w3.org/TR/xquery/#doc-xquery-DirCommentConstructor).
element { xquery-expr }
attribute QName
document
text { xquery-expr }
comment
{ xquery-expr }
processing-instruction
NCName
The following is an example of some XML that is created using computed constructors:
In this example, the comma operator concatenates a constructed attribute (the myatt attribute on
the hello element) and a literal expression (hello world, which becomes the element text node
content) to create the content for the element node. The following example shows how you can
compute the QName with an XQuery expression:
element {xs:QName("hello")} {
attribute myatt { "world" } , "hello world" }
(:
returns the following XML:
<hello myatt="world">hello world</hello>
:)
When you construct XML in XQuery, the XQuery evaluator will always construct well-formed
XML (assuming your XQuery is valid). Compared with other languages where you construct
strings that represent XML, the fact that the XQuery rules ensure that an XML node is well
formed tends to eliminate a whole class of bugs in your code that you might encounter using other
languages.
• Main Modules
• Library Modules
This section provides some basic syntax for XQuery modules. For the complete syntax of XQuery
modules, see the XQuery specification (http://www.w3.org/TR/xquery/#doc-xquery-Module).
Note: XQuery dialect 0.9-ml has been deprecated. MarkLogic recommends that you use
either 1.0-ml or 1.0.
For details on dialects 1.0 and 1.0-ml, including rules for the combining different dialects, see
“XQuery Dialects in MarkLogic Server” on page 7.
"1.0-ml" ;
"0.9-ml"
In 1.0-ml, you can construct programs that have multiple main modules separated by semi-colons,
as described in “Semi-Colon as Transaction Separator” on page 19.
For another example of a main module, see the example at the end of the “Library Modules” on
page 36.
XQuery prolog
If you stored this module under an App Server root as hello.xqy, you could call this function with
the following very simple main module:
my-library:hello()
(: this returns the string "hello" :)
• Declaring Namespaces
• Declaring Options
• Declaring Functions
• Declaring Variables
The library module location and the schema location are not technically required. The location
must be supplied for module imports, however, as they are used to determine the location of the
library module and the module will not be found without it. Also, all modules for a given
namespace must be imported with a single import statement (with comma-separated locations).
For schema imports, if the location is not supplied, MarkLogic Server resolves the schema URI
using the in-scope schemas (schemas in the schemas database and the <marklogic-dir>/Config
directory). If there are multiple schemas with the same URI, MarkLogic Server chooses one of
them. Therefore, to ensure you are importing the correct schema, use the location for the schema
import, too. For details on the rules for resolving the locations, see Importing XQuery Modules, XSLT
Stylesheets, and Resolving Paths in the Application Developer’s Guide.
For more details on imports, see the XQuery specification for schema imports
(http://www.w3.org/TR/xquery/#id-schema-import) and for module imports
(http://www.w3.org/TR/xquery/#id-module-import).
• xdmp:mapping
• xdmp:update
• xdmp:commit
• xdmp:transaction-mode
• xdmp:copy-on-validate
• xdmp:output
• xdmp:coordinate-system
4.5.3.1 xdmp:mapping
declare option xdmp:mapping "false";
The xdmp:mapping option sets whether function mapping is enabled in a module. For details on
function mapping, see “Function Mapping” on page 17.
4.5.3.2 xdmp:update
declare option xdmp:update "true";
The xdmp:update option forces a request to either be an update ("true"), a query ("false"), or to
determine the update mode of the query at compile time ("auto"). Without this option, the request
will behave as if the option is set to "auto" and determine at compile time whether to run as an
update statement (in readers/writers mode) or whether to run at a timestamp. For details on update
statements versus query statements, see Understanding Transactions in MarkLogic Server in the
Application Developer’s Guide.
4.5.3.3 xdmp:commit
declare option xdmp:commit "explicit";
The xdmp:commit option specifies whether MarkLogic treats each XQuery statement as a
single-statement, auto-commit transaction ("auto") or a multi-statement transaction that must be
explicitly commited or rolled back ("explicit"). The default behavior is auto.
For more details, see Understanding Transactions in MarkLogic Server in the Application Developer’s
Guide.
4.5.3.4 xdmp:transaction-mode
Use xdmp:transaction-mode to change the runtime model for newly created transactions. The
transaction mode affects when transactions are created, whether or not they span statement
boundaries, and when and how they are committed. The default mode is auto:
You can specify the following values for xdmp:transaction-mode, as string literals:
• auto (default)
• update-auto-commit
• update
• query
• query-single-statement
• multi-auto
These values correspond to the equivalent settings for the xdmp:set-transaction-mode XQuery
function. Use the option, rather than the API function, if you need to set the transaction mode
before creating any transactions.
For details on transaction modes, see Transaction Mode in the Application Developer’s Guide, and
the discussion of xdmp:set-transaction-mode in MarkLogic XQuery and XSLT Function
Reference.
4.5.3.5 xdmp:copy-on-validate
declare option xdmp:copy-on-validate "true";
The xdmp:copy-on-validate option defines the behavior of the validate expression. You can set
the option to make a copy of the node during schema validation. For details, see “Validate
Expression” on page 52.
4.5.3.6 xdmp:output
The xdmp:output option determines how the output is serialized. The options mirror the
serialization options for xslt using the <xsl:output> XSLT instruction. The following example
causes html serialization:
For details on the <xsl:output> XSLT instruction, from which many of the xdmp:output options
are derived, see http://www.w3.org/TR/xslt#output in the XSLT specification. You can combine
options by having multiple declare option statements.
Valid values for the xdmp:output option are (the values must be string literals):
• method = xml
• method = html
• method = text
• method = sparql-results-json
• method = n-triples
• method = n-quads
• method = sparql-results-csv
• method = rows-json
• method = rows-json-seq
• method = rows-json-multipart
• method = rows-xml
• method = rows-xml-multipart
• method = rows-json-uniform
• method = rows-json-seq-uniform
• method = rows-json-multipart-uniform
• method = rows-xml-uniform
• method = rows-xml-multipart-uniform
• method = rows-json-multipart-node
• method = rows-json-multipart-uniform-node
• method = rows-xml-multipart-node
• method = rows-xml-multipart-uniform-node
• cdata-section-elements = <QName>
Additionally, these are all available in XSLT as attributes on the <xsl:output> instruction. In the
<xsl:output> instruction, use these attributes in the form:
attribute-name="value"
The exceptions to this are the MarkLogic extensions indent-untyped and default-attributes.
When using these attributes, use the namespace prefix xdmp with the attributes (and you must
define the prefix in your stylesheet XML). For example:
4.5.3.7 xdmp:coordinate-system
Use xdmp:coordinate-system to override the App Server default geospatial coordinate system and
precision. For example, the following declaration specifies that a module will use the “wgs84”
coordinate system and double precision in geospatial operations.
The coordinate system name can be any of the canonical names generated by
geo:coordinate-system-canonical, including the following:
• wgs84
• wgs84/double
• raw
• raw/double
Single precision is implied where the name does not explicitly include “double”.
Functions can optionally be typed, both for parmeters to the function and for results of the
function. The following is a very simple function declaration that takes a string as input and
returns a sentence indicating the length of the string:
If this variable were defined in a library module named mylib.xqy stored under your App Server
root, and if you imported that library module bound to the namespace prefix mylib into a main
module, then you can reference this variable in the main module as follows:
$mylib:is-it-january
For more details on collations, see the Encodings and Collations chapter of the Application
Developer’s Guide.
Note: You cannot put a comment inside of a text literal or inside of element content. For
example, the following is not interpreted as having a comment:
• XPath Expressions
• FLWOR Expressions
• The if Expression
• Validate Expression
for clause
return clause
let clause
where clause order by clause
The following sections examine each of the five clauses in more detail:
as type at iterator
The for clause iterates over each item in the expression to which the variable is bound. In the
return clause, an action is typically performed on each item in the variable bound to the
expression. For example, the following binds a sequence of integers to a variable and then
performs an action (multiplies it by 2) on each item in the sequence:
for $x in (1, 2, 3, 4, 5)
return
$x * 2
As is common in XQuery, order is significant, and the items are bound to the variable in the order
they are output from the expression.
You can also bind multiple variables in one or more for clauses. The FLWOR expression then
iterates over each item in the subsequent variables once for each item in the first variable. For
example:
for $x in (1,2,3)
for $y in (4,5,6)
return
$x * 2
In this case, the inner for loop (with $y) is executed one complete iteration for each of the items in
the outer for loop (the one with $x). Even though it does not return anything from $y, the
expression in the return clause is evaluated once for each item in $y, and that happens once for
each item in $x.
You could return something from each iteration of $y, as in the following example:
for $x in (1,2,3)
for $y in (4,5,6)
return
($x * 2, $y * 3)
Alternately, you could write the two for clauses as follows, with the same results:
When you have multiple variables bound in for clauses, it is an effective way of joining content
from one variable with the other. Note that if the content from each variable comes from a
different document, then multiple for clauses in a FLOWR expression ends up performing a join
of the documents.
as type
A let clause produces a single binding for each variable. Consequently, let clauses do not affect
the number of binding tuples evaluated in a FLWOR expression. Variables bound in a let clause
are available to anything that follows in the FLWOR expression (for example, subsequent for or
let clauses, the where clause, the order by clause, or the return clause).
In its simplest form, the let clause allows you to build a FLWOR expression that outputs the
sequence to which the variable is bound. For example, the following expression:
"hello", "goodbye"
A typical use for a let clause is to bind a sequence to a variable, then use the variable in a for
clause to iterate over each item. For example:
let $x := (1 to 5)
for $y in $x
return
$x * 2
Again, this is a trivial example, but it could be that the expression in the let binding is
complicated, and this technique allows you to cleanly structure your code.
where boolean-expression
Only tuples for which the boolean-expression evaluates to true will contribute to the result sequence
of the FLWOR expression The where clause preserves the order of tuples, if any. boolean-expression
may contain and, or and not, among other operators.
Typically, you use comparison operators to test for some condition in a where clause. For
example, if you only want to output from the FLWOR items that start with the letter “a”, you can
do something like the following:
order by $varExpr
stable ascending
descending
empty least
The order by clause can be used to specify an order in which the tuple sequence will be passed to
the return clause. The order by clause can specify any sort key, regardless of whether that sort
key is contained in the result sequence. You can reorder sequences on an ascending or descending
basis.
The following example sorts the sequence bound to $x (in collation order) by each item:
return expression
The return expression is evaluated once for each tuple of bound variables. This evaluation
preserves the order of tuples, if any, or it can impose a new order using the order by clause.
Because the return clause specifies an expression, any legal XQuery expression can be used to
construct the result, including another FLWOR expression.
typeswitch ( expression )
variable as
variable
A typeswitch expression evaluates the first case_expr whose sequenceType matches the type of
the specified expression. If there is no sequenceType match, expr_default is evaluated.
typeswitch ($address)
case $a as element(*, USAddress) return handleUS($a)
case $a as element(*, CanadaAddress) return handleCanada($a)
default return handleUnknown($address)
This code snippet determines the sequenceType of the variable $address, then evaluates one of
three sub-expressions. In this case:
A sequenceType can also be a kind test (such as an element test). It is possible to construct case
clauses in which a particular expression matches multiple sequenceTypes. In this case, the
case_expr of only the first matching sequenceType is evaluated. You can also use the typeswitch
expression in a recursive function to iterate through a document and perform transformation of
the document. For details about using recursive typeswitches, see the Transforming XML Structures
With a Recursive typeswitch Expression chapter of the Application Developer’s Guide.
If expression expr_c1 evaluates to true, then the value of the if expression is the value of
expression expr_r1, otherwise the value of the if expression is the value of expr_r2. The else
clause is not optional; if no action is to be taken, use an empty sequence for expr_r2; there is no
“end if” or similar construct in XQuery:
if (1 eq 2)
then "this is strange"
else ()
The extent of expr_r1 and expr_r2 is limited to a single expression. If a more complex set of
actions are required, an element constructor, sequence, or function call must be used.
These expressions are particularly useful when trying to select a node based on a condition
satisfied by at least one or alternatively all of a particular set of its children.
Imagine an XML document containing log messages. The document has the following structure:
<log>
<event>
<program> .... </program>
<message> .... </message>
<level> .... </level>
<error>
<code> .... </code>
<severity> .... </severity>
<resolved> .... </resolved>
</error>
<error>
....
</error>
....
</event>
....
</log>
Every <event> node has <program>, <message>, and <level> children. Some <event> nodes have
one or more <error> children.
This query returns only those <event> nodes in which there is an <error> node with a <resolved>
element whose value is “false”.
validate { expr }
lax
strict
as XML_type
The expression to validate must be a node referencing an in-scope schema. The node can
reference a schema. The default validation mode is strict. When performing lax validation, the
validate expression first tries to validate the node using an in-scope schema, and then if no schema
is found and none is referenced in the node, the validation occurs without a schema. If a node is
not valid, an exception is thrown. If a node is valid, then the node is returned. For more details,
see the XQuery specification (http://www.w3.org/TR/xquery/#id-validate).
You can also set a prolog option to determine if the node returned is a copy of the original node
(losing its context) or the original node (keeping its context). The XQuery specification calls for
the node to be a copy, but it is often useful for the node to retain its original context (for example,
so you can look at its ancestor elements). The following is the prolog option:
You can specify true or false. This option is true by default in the 1.0 dialect, and false by
default in the 1.0-ml dialect.
(:
validates against the in-scope xhtml schema and returns the element:
<p xmlns="http://www.w3.org/1999/xhtml">hello there</p>
:)
The as XML_type validation mode allows you to specify the type to validate as (rather than use the
in-scope schema definitions for the type). This mode is an extension to the XQuery 1.0 syntax and
is only available in the 1.0-ml dialect.
In the 1.0 dialect (or also in the 1.0-ml dialect), you can specify the an xdmp:validate-type
pragma before an expression to perform the same as XML_type validation, but without the
validate as syntax, as in the following example:
Node comparison tests are useful when creating logic that relies on document order. For example,
if you wanted to verify if a particular node came before another node, you can test as follows:
$x << $y
If this test returns true, you know that the node bound to $x comes before the node bound to $y,
based on document order.
• Sequence Operators
• Item Operators
The section provides a brief overview of the basics of XPath, and includes the following sections:
• Path Expressions
• Restricted XPath
For detailed information about XPath, see the W3C XPath 2.0 language reference
(http://www.w3.org/TR/xpath20/).
<SPEAKER>HAMLET</SPEAKER>
You can make path expressions arbitrarily complex, which makes them a very powerful tool for
navigating through XML structures. For more details about path expressions, see the W3C
XQuery specification (http://www.w3.org/TR/xquery/#id-path-expressions).
A path expression always returns nodes in document order. If you want to return nodes in
relevance order (that is, relevance-ranked nodes), use the MarkLogic Server cts:search built-in
function or put the XPath in a FLWOR expression with an order by clause. Note that both XPath
expressions and cts:search expressions use any available indexes for fast expression evaluation.
For details on cts:search, see the Application Developer’s Guide and the MarkLogic XQuery and
XSLT Function Reference. For details about index options in MarkLogic Server, see the
Administrator’s Guide.
Shorthand (N/A
Axis Description
if no shorthand)
Shorthand (N/A
Axis Description
if no shorthand)
Keep in mind the following notes when using the XPath axes:
The following features only support a restricted XPath subset. Each feature imposes different
limitations.
The following topics provide supporting details for the XPath restrictions applicable to these
features.
For detailed information about XPath, see the W3C XPath 2.0 language reference
(http://www.w3.org/TR/xpath20/).
Note: Avoid creating multiple path indexes that end with the same element/attribute, as
ingestion performance degrades with the number of path indexes that end in
common element/attributes.
The following list defines key aspects of the XPath restrictions. Additional restrictions may apply.
For a complete definition of the valid XPath subset, see “Indexable Path Expression Grammar” on
page 69.
• The only operators you can use in predicate expressions are comparison and logical
operators. (=, !=, <, <=, >=, >, eq, ne, lt, le, ge, gt, and, or).
• The right operand of a comparison in a predicate can only be a string literal, numeric
literal, or a sequence of string or numeric literals.
• You can only use forward axes in path steps. That is, you can use axes such as self::,
child::, descendant::, but you cannot use reverse axes such as parent::, ancestor::, or
preceding::. For details, see http://www.w3.org/TR/xpath/#predicates.
• You can only call functions on the “safe” function list in a predicate expression. For
details, see “Functions Callable in Predicate Expressions” on page 66.
• You cannot span a fragment root. Paths must be scoped within fragment roots.
• You cannot use an unnamed node test as the last path step. For example, when addressing
JSON, you cannot have a final path step such as node() or array-node(). You can use
named nodes, such as node('a').
The following table provides some examples of path expressions that meet the requirements of an
indexable path expression. This set of examples is not exhaustive.
Wildcards /a/*/b
/a/b/*
For more details on using namespace prefixes in indexable path expressions, see Using Namespace
Prefixes in Index Path Expressions in the Administrator’s Guide.
The following table contains some examples of valid XPath expressions that cannot be used to
define path-based indexes. That is, expressions that could be used in other contexts, but for which
cts:valid-index-path or cts.validIndexPath returns false.
To test whether or not an XPath expression is valid as a protected path, use the XQuery function
cts:valid-index-path or the Server-Side JavaScript function cts.validIndexPath.
To learn more about element level security, see Element Level Security in the Security Guide.
• You can use "/" as a context XPath expression if the template has collection or directory
scope. For details, see Collections and Directories in the Application Developer’s Guide.
To test an XPath expression for validity in a TDE template, use the XQuery function
cts:valid-tde-context or the Server-Side JavaScript function cts.validTdeContext.
For more details and examples, see “Path Field and Path-Based Range Index Configuration” on
page 60 and “Indexable Path Expression Grammar” on page 69.
To learn more about TDE, see Template Driven Extraction (TDE) in the Application Developer’s
Guide.
To test an XPath expression for validity in a patch descriptor, use the XQuery function
cts:valid-document-patch-path or the Server-Side JavaScript function
cts.validDocumentPatchPath.
The following list defines key aspects of the XPath restrictions. Additional restrictions may apply.
For a complete definition of the valid XPath subset, see “Patch and Extract Path Expression
Grammar” on page 71.
• The only operators you can use in predicate expressions are comparison and logical
operators. (=, !=, <, <=, >=, >, eq, ne, lt, le, ge, gt, and, or).
• The right operand of a comparison in a predicate can only be a string literal, numeric
literal, or a sequence of string or numeric literals.
• You can only use forward axes in path steps. That is, you can use axes such as self::,
child::, descendant::, but you cannot use reverse axes such as parent::, ancestor::, or
preceding::. For details, see http://www.w3.org/TR/xpath/#predicates.
• You can only call functions on the “safe” function list in a predicate expression. For
details, see “Functions Callable in Predicate Expressions” on page 66.
• You cannot span a fragment root. Paths must be scoped within fragment roots.
The following table provides some examples of path expressions that meet the requirements of an
indexable path expression. This set of examples is not exhaustive.
Wildcards /a/*/b
/a/b/*
The following table contains some examples of valid XPath expressions that cannot be used to
define path expressions in patch operations. That is, expressions that could be used in other
contexts, but for which cts:valid-document-patch-path or cts.validDocumentPatchPath returns
false. This set of examples is not exhaustive.
To learn more about the document patch feature, see the following topics:
• Java Client API: Partially Updating Document Content and Metadata in the Java Application
Developer’s Guide
• Node.js Client API: Patching Document Content or Metadata in the Node.js Application
Developer’s Guide
• REST Client API: Partially Updating Document Content or Metadata in the REST Application
Developer’s Guide
The extract-path is restricted to the same XPath subset that is described in “Patch Feature of the
Client APIs” on page 63.
To test an XPath expression for validity as an extract-path value, use the XQuery function
cts:valid-extract-path or the Server-Side JavaScript function cts.validExtractPath.
To learn more about the extract-document-data query option, see extract-document-data in the
Search Developer’s Guide. To learn more about the equivalent JSearch feature, see Extracting
Portions of Each Matched Document in the Search Developer’s Guide.
The Java and Node.js Client APIs support a similar feature for Optic searches. For details, see
“The Optic API xpath Function” on page 65.
To learn more about the Optic API, see the following topics:
• XQuery and Server-Side JavaScript: Optic API for Multi-Model Data Access in the
Application Developer’s Guide
• Java Client API: Optic Java API for Relational Operations in the Java Application
Developer’s Guide
• Node.js Client API: Using the Optic API for Relational Operations in the Node.js Application
Developer’s Guide
• String Functions
• Mathematical Functions
• Miscellanious Functions
Note: These functions are not supported by XQuery 0.9-ml, which has been deprecated.
fn:insert-before fn:starts-with
• fn:empty
• fn:exists
• fn:false
• fn:not
• fn:true
xs:short xs:gYear
xs:byte xs:gMonthDay
math:cos math:log10
fn:number sem:iri
fn:root sem:unknown
fn:min sem:unknown-datatype
fn:max sem:invalid
For advanced users, this section contains a detailed grammar that defines the subset of XPath you
can use to define path-based indexes. The same grammar applies to XPath expressions for the
following features. Any differences are called out below.
• Template Driven Extraction (TDE): TDE also allows the use of “/” as a TDE context XPath
expression in some cases.
• Element Level Security: No differences.
The grammar is derived from the W3C XML Path Language specification; for details, see
http://www.w3.org/TR/xpath/. If you find it easier to explore the grammar graphically, the BNF is
suitable for use with many tools that generate “railroad diagrams” from BNF, such as
http://bottlecaps.de/rr/ui.
The following grammar expresses the XPath subset you can use to define path-based indexes.
Note that FunctionalCall in the grammar can only be a call to one of the functions listed in
“Functions Callable in Predicate Expressions” on page 66. Also, an unamed KindTest cannot be
used as the leaf step.
For advanced users, this section contains a detailed grammar that defines the subset of XPath you
can use with the following features. More details and examples are available in the referenced
topics.
The grammar is derived from the W3C XML Path Language specification; for details, see
http://www.w3.org/TR/xpath/. If you find it easier to explore the grammar graphically, the BNF is
suitable for use with many tools that generate “railroad diagrams” from BNF, such as
http://bottlecaps.de/rr/ui.
The following grammar expresses the XPath subset. Note that FunctionalCall in the grammar can
only be a call to one of the functions listed in “Functions Callable in Predicate Expressions” on
page 66.
the ":" */
Name ::= NameStartChar (NameChar)*
Predicates ::= Predicate*
Predicate ::= PredicateExpr | "[" Digit+ "]"
Digit ::= [0-9]
PredicateExpr ::= "[" PredicateExpr "and" PredicateExpr "]"
| "[" PredicateExpr "or" PredicateExpr "]"
| "[" ComparisonExpr "]" | "[" FunctionExpr "]"
ComparisonExpr ::= RelativePathExpr GeneralComp SequenceExpr
| RelativePathExpr ValueComp Literal
| PathExpr
FunctionExpr ::= FunctionCall GeneralComp SequenceExpr
| FunctionCall ValueComp Literal
| FunctionCall
GeneralComp ::= "=" | "!=" | "<" | "<=" | ">" | ">="
ValueComp ::= "eq" | "ne" | "lt" | "le" | "gt" | "ge"
SequenceExpr ::= Literal+
Literal ::= NumericLiteral | StringLiteral
KindTest ::= ElementTest
| AttributeTest
| CommentTest
| TextTest
| ArrayNodeTest
| ObjectNodeTest
| BooleanNodeTest
| NumberNodeTest
| NullNodeTest
| AnyKindTest
| DocumentTest
| SchemaElemTest
| SchemaAttrTest
| PITest
TextTest ::= "text" "(" ")"
CommentTest ::= "comment" "(" ")"
AttributeTest ::= "attribute" "(" QNameOrWildcard? ")"
ElementTest ::= "element" "(" QNameOrWildcard? ")"
ArrayNodeTest ::= "array-node" "(" QuotedNCName? ")"
ObjectNodeTest ::= "object-node" "(" QuotedNCName? ")"
BooleanNodeTest ::= "boolean-node" "(" QuotedNCName? ")"
NumberNodeTest ::= "number-node" "(" QuotedNCName? ")"
NullNodeTest ::= "null-node" "(" QuotedNCName? ")"
AnyKindTest ::= "node" "(" QuotedNCName? ")"
SchemaElemTest ::= "schema-element" "(" QName ")"
SchemaAttrTest ::= "schema-attribute" "(" QName ")"
PITest ::= "processing-instruction" "(" (NCName |
StringLiteral)? ")"
QNameOrWildcard ::= QName | "*"
QuotedNCName ::= "'" NCName "'"
| '"' NCName '"'
XQuery is designed to work well with XML content, allowing many convenient ways to search
through XML elements and attributes as well as making it easy to output XML from an XQuery
program. When working with XML, you must understand a little about the XML data model, and
one fundamental aspect of the XML data model is namespaces. This chapter describes XML
namespaces and how they are important in XQuery, and includes the following sections:
• Everything Is In a Namespace
There can also be a default element namespace defined for the module, as described in “Declaring
a Default Element Namespace in XQuery” on page 78. The fact that every element is in a
namespace, along with the fact that XPath expressions of an unknown node return the empty
sequence, make it easy to have simple coding errors (or even typographic errors) that cause your
query to be a valid XPath expression, but to return the empty string. For example, if you have a
simple typographical error in a namespace declaration, then XPath expressions that you might
expect to return nodes might return the empty sequence. Consider the following query against a
database with XHTML content:
You might expect this to return all of the XHTML p elements in the database, but instead it
returns nothing (the empty sequence). If you look closely, though, you will notice that the
namespace URI is misspelled (it is missing the x in xhtml). If you keep in mind that everything is
in a namespace, it can help find many simple XQuery coding errors. The correct version of this
query is as follows, and will return all of the XHTML p elements:
The XML data model is aware of XML schema, and all XML nodes can optionally have XML
types (for example, xs:string, xs:dateTime, xs:integer, and so on). When you are creating
library functions that might be called from a number of contexts, knowing that XQuery accesses
the XML data model can help you to make your code robust. For example, you might have code
that explicitly (or implicitly, using the XQuery rules) casts nodes to a particular XML type,
enforcing strong typing in your code.
When serializing XML, there are five XML reserved characters that are serialized with their
corresponding XML entities. These characters cannot appear as content in a serialized XML text
node. The following table shows these five characters:
There are different ways to serialize the same XML content. The way XML content is serialized
depends on how the content is constructed, the various namespace declarations in the query, and
how the XML content was loaded into MarkLogic Server (for content loaded into a database). In
particular, the ampersand character can be tricky to construct in an XQuery string, as it is an
escape character to the XQuery parser. The ways to construct the ampersand character in XQuery
are:
If you evaluate this query, it returns the following serialization of the specified element:
If you consider a similar query with a namespace prefix binding instead of the default element
namespace declaration:
If you evaluate this query, it returns the following serialization of the specified element:
<hello:some-element xmlns:hello="my.namespace.hello">element
content with & goes here</hello:some-element>
Notice that in both cases, the & character is escaped as an XML entity, and in each case there is an
xmlns attribute added to the serialization. In the first example, there is no prefix bound to the
namespace, but in the second one there is (because it is declared in the query). Both serializations
represent the exact same XML data model.
To construct the double quotation mark and apostrophe characters within a string quoted with one
of these characters (' or "), you can use the character to escape itself, or you can quote the string
with the other quote character, as follows:
For example, the following XML serialization specifies that the XHTML namespace is inherited
from the root element:
<html xmlns="http://www.w3.org/1999/xhtml">
<body><p>This is in the XHTML namespace</p></body>
</html>
Each of the elements (html, body, and p in this example) are in the XHTML namespace.
Similarly, an xmlns namespace declaration with a prefix appended specifies that the prefix is
inherited by the element children.
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:my="my.namespace">
<body>
<p>This is in the XHTML namespace</p>
<my:p>This element is in my.namespace</my:p>
</body>
</html>
One other sublety about default namespaces using the xmlns attribute in constructed elements is
that any XPath statement that is constructed within an element constructor that uses an xmlns
default namespace will default to the namespace of the parent element. This can be unexpected if
you are trying to write an XPath expression using QNames in no namespace. The following code
sample demonstrates how this namespace XPath inheritance works.
(:
Returns:
<blah xmlns="foo"/>
<foo:blah xmlns:foo="foo"><b>hello</b></foo:blah>
Notice how in the first part of the return, the "b" in $x/b
inherits the namespace from the parent element, which is
constructed with a default namespace (xmlns="foo"),
so it returns empty.
In the second $x/b, the "b" is in no namespace.
:)
There are some other subtleties of namespace inheritance in XML. For more details, see the XML
Schema specification (http://www.w3.org/XML/Schema).
An XQuery program that has this prolog declaration will use the XHTML namespace for all
elements where a namespace is not explicitly defined (for example, with a namespace prefix).
Declaring a default element namespace is a convenience and a style which some programmers
find useful. While it is sometimes convenient (so you do not have to prefix element names, for
example), it can also cause confusion in larger programs that use multiple namespaces, so for
more complex programming efforts, explicitly defining namespaces is usually more clear.
There are many functions that use QNames in XQuery, and all of the rules for in-scope
namespaces apply to constructing those QNames. For example, if the namespace prefix my is
bound to the namespace URI my.namespace in the scope of a query, then the following would
construct a QName in that namespace with the local name some-element:
xs:QName("my:some-element")
Similarly, you can construct this QName using the fn:QName function as follows:
fn:QName("my.namespace", "some-element")
Because a prefix is not specified in the second parameter to the above function, the QName is
defined to have a prefix of the empty string ("").
Similarly, you can construct this QName with the prefix my by using the fn:QName function as
follows:
fn:QName("my.namespace", "my:some-element")
XQuery functions and other language constructs that take a QName can use any in-scope
namespace prefixes. For example, the following will construct an html element in the XHTML
namespace:
1.0-ml
Predefined Used For Namespace URI
Prefix
1.0-ml
Predefined Used For Namespace URI
Prefix
1.0
Predefined Used For Namespace URI
Prefix
In MarkLogic Server, you have both the XQuery and XSLT languages available. You can use one
or both of these languages as needed. This chapter briefly describes some of the XSLT language
features and describes how to run XSLT in MarkLogic Server, and includes the following
sections:
• XSLT 2.0
For details about the XSLT 2.0 recommendation, see the W3C website:
• http://www.w3.org/TR/xslt20/
An XSLT stylesheet is an XML document. Each element is an instruction in the XSLT language.
For a summary of the syntax of the various elements in an XSLT stylesheet, see
https://www.w3.org/TR/xslt20/#element-syntax-summary.
• xdmp:xslt-invoke
• xdmp:xslt-eval
The xdmp:xslt-invoke function invokes an XSLT stylesheet from the App Server root, and the
xdmp:xslt-eval function takes a stylesheet as an element and evaluates it as an XSLT stylesheet.
As part of running a stylesheet, you pass the stylesheet a node to operate on. For details on
xdmp:xslt-invoke and xdmp:xslt-eval, see the MarkLogic XQuery and XSLT Function Reference.
• EXSLT Extensions
• xdmp:dialect Attribute
xdmp:xslt-eval(
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xdmp="http://marklogic.com/xdmp"
xmlns:search="http://marklogic.com/appservices/search"
extension-element-prefixes="xdmp"
version="2.0">
<xdmp:import-module
namespace="http://marklogic.com/appservices/search"
href="/MarkLogic/appservices/search/search.xqy"/>
<xsl:template match="/">
<xsl:copy-of select="search:search('hello')"/>
</xsl:template>
</xsl:stylesheet>
,
document{ <doc/> })
Similarly, you can import an XSLT sytlesheet into an XQuery library, as described in “Importing
XQuery Function Libraries to a Stylesheet” on page 84.
The following is an example of a try/catch in XSLT. This example returns the error XML, which
is bound to the variable named e in the name attribute of the <xdmp:catch> instruction.
xdmp:xslt-eval(
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xdmp="http://marklogic.com/xdmp"
extension-element-prefixes="xdmp"
version="2.0">
<xsl:template match="/">
<xdmp:try>
<xsl:value-of select="error(xs:QName('MY-ERROR'),
'This is an error')"/>
<xdmp:catch name="e">
<xsl:copy-of select="$e"/>
</xdmp:catch>
</xdmp:try>
</xsl:template>
</xsl:stylesheet>
,
document{<doc>hello</doc>})
The following is an example of the exsl:document instruction. Note that this is essentially the
same as the xsl:result-document instruction, which is part of XSLT 2.0.
version="2.0">
<xsl:template match="/">
<html>
<head><title>Frame example</title></head>
<frameset cols="20%, 80%">
<frame src="toc.html"/>
<exsl:document href="toc.html">
<html>
<head><title>Table of Contents</title></head>
<body>
<xsl:apply-templates mode="toc" select="*"/>
</body>
</html>
</exsl:document>
<frame src="body.html"/>
<exsl:document href="body.html">
<html>
<head><title>Body</title></head>
<body>
<xsl:apply-templates select="*"/>
</body>
</html>
</exsl:document>
</frameset>
</html>
</xsl:template>
</xsl:stylesheet>,
document{element p { "hello" }})
for $node at $i in $nodes
return
if ( fn:document-uri($node) )
then xdmp:save(
fn:resolve-uri(fn:document-uri($node),
"C://mypath/exsl.xqy"), $node)
else ($node)
The above query will save the two documents created with exsl:document to the App Server root
on the filesystem, making them available to the output document with the frameset. For more
details about the exsl:document instruction, see the EXSLT web site.
If you are using code shared with other stylesheets (especially stylesheets that might be used with
other XSLT processors), use care when setting the dialect to 1.0-ml, as it might have subtle
differences in the way expressions are evaluated.
For details about dialects, see “Overview of the XQuery Dialects” on page 7.
Some of the important points to note about the <xsl:import> instruction are as follows:
• Any absolute URI references in the href attribute are resolved in the context of the current
MarkLogic Server database URIs. Relative paths are resolved relative to current module
in the App Server root. For details, see XQuery Library Modules and Main Modules in the
Application Developer’s Guide.
• Any code imported in an <xsl:import> instruction follows the rules of precedence for
XSLT imports. In general, that means that a stylesheet that imports has precedence over an
imported stylesheet.
• Any XQuery library modules imported into a styleheet follow the rules for XQuery
imports, not the rules for XSLT imports. Notably, only functions and variables in the
imported module are directly available to the stylesheet, not functions and variables that
the XQuery library might import. XQuery library module imports use the
<xdmp:import-module> extension instruction, as described in “Importing XQuery Function
Libraries to a Stylesheet” on page 84.
This section describes the sample URL rewriter for XSLT stylesheets and includes the following
parts:
• xslt-invoker.xqy
• xslt-rewrite-handler.xqy
Once you set up the rewriter as described in the next section, URLs to the App Server of the form:
/filename.xsl?doc=/url-of-context-node.xml
will invoke the filename.xsl stylesheet and pass it the context node at the URI specified in the
doc request field.
/styled/url-of-context-node.xml?stylesheet=/stylesheet.xsl
will invoke the stylesheet at the path specified in the stylesheet request field passing in the context
node in the path after /styled (/url-of-context-node.xml in the above sample).
The following table describes what the request fields you pass translate to when you are using the
sample XSLT rewriter.
doc Specifies the URI of the document to be passed into the stylesheet as the
context node. If there is no doc request field, then it defaults to a context
node of default.xml. If no document with the URI default.xml exists in
the database, then the rewriter will throw an exception.
stylesheet Used with paths that start with /styled. Specifies the path to the
stylesheet to invoke. If it is not present, uses the stylesheet at
default.xslt.
mode The name of the initial mode to pass into the stylesheet. If not present, no
mode is passed.
template The name of the initial template to pass into the stylesheet. If not present,
no template is passed in.
2. In the Admin Interface, navigate to the HTTP App Server configuration for the App
Server in which want to directly invoke XSLT stylesheets.
3. On the HTTP Server Configuration page, find the url rewriter field (it is towards the
bottom of the page).
5. Click OK.
Request against the App Server will now be automatically rewritten to directly invoke stylesheets
as described in the previous section.
On some level, choosing which language to perform a specific task is one of style. Different
programmers have different styles, and so there is no “correct” answer to what to do in XQuery
and what to do in XSLT.
In practice, however, XSLT is very convenient for performing XML transformation. You can do
these transformations in XQuery too, and you can do them well in XQuery, but some
programmers find it more natural to write a transformation in 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
• Using Functions
• Search Functions
• Additional Resources
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” on page 15.
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.
• Recursive Functions
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:
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 Expressionchapter of the Application Developer’s Guide.
You can also use XSLT to perform transformations. For more information about XSLT, see
“XSLT in MarkLogic Server” on page 83.
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.
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.
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.
• Other Publications
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/).
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” on page 13. The Getting Started with MarkLogic Server walks you through this process
of using the Use Cases application some detail.
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:
MarkLogic provides technical support according to the terms detailed in your Software License
Agreement or End User License Agreement.
Complete product documentation, the latest product release downloads, and other useful
information is available for all developers at http://developer.marklogic.com. For technical
questions, we encourage you to ask your question on Stack Overflow.
MarkLogic 10
MarkLogic Server Technical Support
10.0 Copyright
999
The MarkLogic software is protected by United States and international copyright laws, and
incorporates certain third party libraries and components which are subject to the attributions,
terms, conditions and disclaimers set forth below.
For all copyright notices, including third-party copyright notices, see the Combined Product
Notices for your version of MarkLogic.
MarkLogic 10
MarkLogic Server Copyright