We’ve joined forces with Smartlogic to reveal smarter decisions—together.

Punctuation in XPath, part 3: “@” and “..”

Posts in this series:
Punctuation in XPath, part 1: dot (“.”)
Punctuation in XPath, part 2: slash (“/”)
Punctuation in XPath, part 3: “@” and “..”
Punctuation in XPath, part 4: predicates (“[…]”)
Punctuation in XPath, part 5: “//”

In this series, we’ve first taken a look at seen what “.” means, and then what “/” means. We’ve also seen that we can use any expression as a step in a path expression, parenthesizing it as necessary. In this third installment on XPath punctuation, we’ll learn about the most common type of step in a path expression, the “axis step” and how it can be abbreviated.

All path expression steps can be categorized into two kinds:

  1. Filter Expression
  2. Axis Steps

filter expression is a primary expression (e.g., variable reference, literal, context item expression, function call, or parenthesized expression) followed by zero or more predicates. An axis step is a step that selects nodes along a specific axis that is followed by zero or more predicates. Let’s get more familiar with these expression steps by walking through examples.

Here’s an example document we’re going to work from, using XQuery to bind it to a variable:

declare variable $doc := document {
      <meta id="firstGroup"/>
      <meta id="secondGroup"/>
declare variable $items := $doc/doc/items/item;

Consider the following four-step expression. This selects the id attribute of each <meta> child of the parent of the first node in $items:


The first step $items[1] is a filter expression. The remaining three steps are all axis steps. In fact, all three make use of some XPath syntax sugar. The following expanded expression is what the above expression is short for:


We see three different axes in use here: “parent”, “child”, and “attribute”. It’s never necessary to use attribute:: or child:: explicitly, since “@” is always short for attribute:: and since child:: is the default when you omit the axis specifier (except when using the lesser-used attribute() or schema-attribute() node tests, in which case attribute:: is the default—either way, you can always use the short form). Most often, you won’t explicitly use parent:: either, however it’s sometimes useful to test the parent’s name or node type, using an expression like parent::foo or parent::document-node().

Axis Step Evaluation

There are in fact 13 XPath axes altogether, and you can use any of them in their expanded form:

  • self::
  • child::
  • attribute::
  • namespace:: (“deprecated” in XPath 2.0 and disallowed in XQuery)
  • descendant::
  • descendant-or-self::
  • following::
  • following-sibling::
  • parent::
  • ancestor::
  • ancestor-or-self::
  • preceding::
  • preceding-sibling::

I won’t go into what sets of nodes each axis selects (you might try experimenting with this handy online XPath axes visualizer to learn some by example), but suffice it to say that the evaluation of an axis step conceptually occurs in three stages, corresponding to the three parts of an axis step:

  1. Axis specifier (one of the above 13 axes or an abbreviation)
  2. Node test (either a name or kind test, such as foo, *, or node())
  3. Predicate (zero or more of them)

The three parts in the axis step of the following expression are “descendant::”, “item”, and “[4]”:


$doc provides the context node, and from there, the axis step is evaluated (at least conceptually) as follows:

  1. Get all nodes along the axis (descendant)
  2. Of those, filter out the ones not matching the node test (item)
  3. Of those, filter out the ones not matching the predicate ([4])

Using XQuery, you could simulate this conceptual process by explicitly breaking the evaluation down into three stages of filtering. The following expression is a longwinded way of selecting the same nodes, just to make my point:

let $matches-axis      := $doc/descendant::node(),  (: everything on axis :)
    $matches-node-test := $matches-axis/self::item, (: filtered :)
    $matches-predicate := $matches-node-test[4]     (: filtered again :)
return $matches-predicate

Syntax Shortcuts

Now that you have a general idea of what an axis step is, here are all the axis-related syntax shortcuts in XPath:

Syntax What it’s short for Notes or exceptions
@ attribute::
.. parent::node()
foo child::foo where “foo” is any node test except attribute(…) or schema-attribute(…)
attribute(foo) attribute::attribute(foo) where “attribute(foo)” is any attribute(…) or schema-attribute(…) node test
// /descendant-or-self::node()/

Continue Reading:

Part 4: predicates (“[…]”)

Part 5: (“//”)

Start a discussion

Connect with the community




Most Recent

View All

Unifying Data, Metadata, and Meaning

We're all drowning in data. Keeping up with our data - and our understanding of it - requires using tools in new ways to unify data, metadata, and meaning.
Read Article

How to Achieve Data Agility

Successfully responding to changes in the business landscape requires data agility. Learn what visionary organizations have done, and how you can start your journey.
Read Article

Scaling Memory in MarkLogic Server

This not-too-technical article covers a number of questions about MarkLogic Server and its use of memory. Learn more about how MarkLogic uses memory, why you might need more memory, when you need more memory, and how you can add more memory.
Read Article
This website uses cookies.

By continuing to use this website you are giving consent to cookies being used in accordance with the MarkLogic Privacy Statement.