For detailed information about XPath, see the W3C XPath 2.0 language reference (http://www.w3.org/TR/xpath20/).
XPath 2.0 is part of XQuery 1.0. XPath is used to navigate XML structures. In MarkLogic Server, the XML structures can be stored in a database or they can be constructed in XQuery. A path expression is an expression that selects nodes from an XML structure. Path expressions are a fundamental way of identifying content in XQuery. Each path has zero or more steps, which typically select XML nodes. Each step can have zero or more predicates, which constrain the nodes that are selected. By combining multiple steps and predicates, you can create arbitrarily complex path expressions. Consider the following path expression (which is in itself a valid XQuery expression):
Against the Shakespeare database (the XML is available at http://www.oasis-open.org/cover/bosakShakespeare200.html), this XPath expression selects all
LINE elements that contain the text
To be or not to be. You can then walk up the document to its parent to see who says this line. as follows:
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.
|Axis||Description||Shorthand (N/A if no shorthand)|
|Selects all ancestor nodes, which includes the parent node, the parent's parent node, and so on.||N/A|
|Selects the current node as well as all ancestor nodes, which includes the parent node, the parent's parent node, and so on.||N/A|
|Selects the attributes of the current node.|
|Selects the immediate child nodes of the current node.|
|Selects all descendant nodes (child nodes, their child nodes, and so on).||N/A|
|Selects the current node as well as all descendant nodes (child nodes, their child nodes, and so on).|
|Selects everything following the current node.|
|Selects all sibling nodes (nodes at the same level in the XML hierarchy) that come after the current node.||N/A|
|Selects the namespace node of the current node.||N/A|
|Selects the immediate parent of the current node.|
|Selects everything before the current node.|
|Selects all sibling nodes (nodes at the same level in the XML hierarchy) that come before the current node.||N/A|
|MarkLogic Server enhancement. Selects the properties fragment corresponding to the current node.||N/A|
|Selects the current node (the context node).|
The XQuery standard functions are the same as the XPath 2.0 functions. These XQuery-standard functions are all built into MarkLogic Server, and use the namespace bound to the
fn prefix, which is predefined in MarkLogic Server. For details on these functions, see the MarkLogic XQuery and XSLT Function Reference reference.