Document structure
Document order
The order nodes appear in the serialized document, which is the order node-sets are returned in and the order positional predicates count in.
Document order is depth-first: an element comes before its children, which come before its following siblings. Attribute and namespace nodes are ordered too, but their order relative to each other is implementation-defined — which is exactly why canonicalization has to impose one.
It matters most in predicates. `[1]` means first in document order along the axis being used, and on a reverse axis such as ancestor that is the nearest one rather than the topmost.
- Defined in
- XPath 1.0 §5
- Category
- Document structure
- Also written
- reading order
Shown, not asserted
All 2 claims are executed on every test run, against the same engines the tools use. If the behaviour changes, the test fails rather than this page quietly becoming wrong.
Positional selection follows document order.
document.xml <r><a/><b/><c/></r>expression local-name(/r/*[2])What the engine returns
bA union does not preserve the order you wrote it in — the result is in document order.
document.xml <r><a/><b/></r>expression local-name((//b | //a)[1])What the engine returns
a
What catches people out
- Attribute order is not document order in any useful sense. Never depend on it; a canonicalizer will sort it.
- On reverse axes, position 1 is the nearest node, not the first in the document.
- A union operator returns nodes in document order regardless of operand order — there is no way to express "these, then those".
Where it bites
A definition on its own is trivia. These are the pages where this term decides whether a document works.
- XPathposition()How position is counted along an axis.
- XPathUnion |The operator whose result order surprises people.
- XPathPredicates [ ]Where document order does its damage.
Related terms
- ElementThe unit of structure in XML: a start tag, its content, and a matching end tag — or a single self-closing tag when there is no content.
- Canonicalization (C14N)Rewriting a document into a single normal form — sorted attributes, expanded empty elements, resolved references, no declaration — so that equivalent documents become byte-identical.
- Text nodeA run of character data in the tree, as a node in its own right — the model a query language sees rather than the markup you wrote.
Get started
Bring order to the XML your team can't afford to ignore.
Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.