Skip to main content
XMLDir

Search 309 pages — tools, formats, elements, namespaces, directory, comparisons, XPath, datatypes, glossary, parse errors, schema errors, use cases, blog and product.

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

    b
  • A 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".

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.