XPath Reference

Shorthand

Most XPath you read in the wild is shorthand: slashes, dots, and the @ sign that expand into longer axis-and-test forms. Knowing what each abbreviation stands for makes unfamiliar expressions instantly readable. These are the five you'll see ninety percent of the time.

/

Root or child step separator

A single slash at the start of an expression means 'start from the document root.' Between steps, it means 'select children.' So /cookbook/recipe means: start at the root, select the cookbook child, then select its recipe children. It's the backbone of every location path.

//

Shorthand for /descendant-or-self::node()/

Double slash is XPath's deep search. It selects descendants at any depth. //name finds every <name> element anywhere in the document. It's the most commonly used shorthand, but be aware it can be expensive on large documents since it searches the entire subtree.

.

Current node (self::node())

A single dot refers to the context node — wherever you currently are in the tree. It's often used in predicates to reference the element's own text content, like //ingredient[. = 'Spaghetti'], which tests the string value of the current ingredient element.

..

Parent node (parent::node())

Double dot navigates to the parent of the current node. It's the XPath equivalent of 'go up one level.' You can chain it — ../../ goes up two levels. It's useful when you've matched something specific and need to access a sibling branch of the tree.

@

Attribute axis shorthand

The @ symbol is shorthand for the attribute:: axis. So @id is the same as attribute::id. It's one of the most frequently used XPath features — predicates like [@difficulty='easy'] use it to filter elements by their attribute values.

← Back to full reference