XPath Reference

Node-Set Functions

These functions answer questions about nodes themselves: how many there are, what they're named, what namespace they belong to. count() is the workhorse inside predicates and sanity checks. name() and local-name() matter the moment you deal with namespaced documents, where element names carry a prefix you may or may not want to match.

count(node-set)

Number of nodes

Returns the number of nodes in a node-set. This is one of the most commonly used functions — perfect for counting how many elements match a pattern, like how many ingredients a recipe has or how many recipes are in the cookbook.

name(node?)

Qualified name of node

Returns the qualified name (including any namespace prefix) of a node. If no argument is given, it uses the context node. This is useful for debugging or for generic templates that need to know what element they're processing.

local-name(node?)

Local part of name

Returns the local part of a node's name, without any namespace prefix. In a document with xmlns:ns='...', an element <ns:item> has local-name 'item'. This is essential when working with namespaced XML where you want to match elements regardless of their prefix.

namespace-uri(node?)

Namespace URI

Returns the namespace URI of a node. For elements without a namespace, it returns an empty string. This function is important when processing documents with multiple namespaces, letting you check which namespace an element belongs to rather than relying on prefixes.

id(value)

Select element by ID

Selects the element with the given ID attribute value. For this to work, the document needs a DTD or schema that declares which attribute is the ID. In practice, many XML documents don't have this, so //element[@id='value'] is often used as a more reliable alternative.

generate-id(node?)

Stable unique id for a node

Returns a string that uniquely identifies the given node within the document — same node, same string, every call. Useful for de-duping, building lookup keys, or correlating nodes across multiple expressions when the document itself has no @id.

deep-equal(a, b)

Structural value equality

Compares two sequences structurally: same length, same item types, same atomized values, and (for nodes) same children. The right tool when '=' is too lax (it succeeds on any matching pair) and 'is' is too strict (it only checks node identity).

← Back to full reference