XPath Quick Reference
XPath Quick Reference
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 68.
• 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 65.
• 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 59 and “Indexable Path Expression Grammar” on page 68.
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 70.
• 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 65.
• 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 62.
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 64.
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 65. 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 65.