AAmit’s LibraryLEARN · PRACTISE · REFLECT

Computers · Programming Atlas

XPath 1.0 Introduction

Standalone XML query expressions and explicit result contracts: values, predicates, axes, comparisons, namespaces and missing results. Earlier XSLT support expressions are prerequisites, not new duplicate coverage. XPath 2.0/3.1, variables, extensions and arbitrary documents remain outside this introduction.

Audience: Learners who can read a small XML document and invoke an installed command-line tool.

Practice: 60 to 90 minutes.

Website reference edition

This introductory material is available here for reading and practice planning. New offline workbench exports, PDF and ZIP editions for these additions are not published. The released Atlas 0.11 downloads cover the earlier 19 packs only. These pages do not run teaching code or contact an AI service.

Runtime and boundaries

Use the installed /usr/bin/xmllint with XML_CATALOG_FILES and SGML_CATALOG_FILES empty and --nonet. The test harness supplies explicit arguments without a shell and a five-second deadline for each owned process. This old installed parser is recorded for reproducible learning, not recommended for hostile documents. --nonet is not a local-file sandbox. These authored fixtures contain no external-resource constructs. Local examples were tested on macOS 27.0.1 arm64; no complete standards conformance or other-platform result is claimed. No browser runs teaching code.

Introductory module · 2 exercises

Lessons

Ask a query, then name its result

A query selects nodes or computes a value. The examples deliberately print a number, string or boolean so expected output is clear. A missing selection and an empty string are not interchangeable in application logic. XML parsing must succeed before query evaluation.

Context changes the meaning of a position

Read a path step by step. A predicate acts on the context created at that point. Compare //item[1] with (//item)[1] using two shelves, then change one shelf and predict both counts. The exercise on preceding siblings asks for the nearest match, so axis direction matters.

Comparisons can hide a missing-value bug

Do not read node-set equality as comparing two lists. The comparison example contains both read and code; test equality and inequality separately. Also predict boolean("false"): nonempty text is truthy, despite its spelling. Define whether missing data should be rejected before using it.

Match the namespace as well as the local name

The namespace fixture contains two item names in different namespaces. Pair local-name() with namespace-uri() to avoid accepting the other item. This is a deliberate strategy for this CLI; a host API with namespace binding can use prefixes instead. A source default namespace does not bind an unprefixed query name.

Keep data out of expression syntax

These expressions are fixed authored text, passed as one argument without a shell. Do not concatenate user text into a query. The absent-node case records the installed CLI behavior separately from count(...)=0. Inspect errors and exit status; an empty node selection is not invalid XPath syntax. In this installed 2.9.13 build, both the empty node-set fixture and malformed query return status 10, with different diagnostics. Do not classify the cause from the number alone or substitute newer-manual status 11.

Reference cards

Absolute path: /study/item

Select child item elements under the document element study.

Value: string(...) / count(...)

Request a predictable string or numeric result.

Predicate: [condition] / [1]

Filter in the context at that step; parentheses can change grouping.

Axis: preceding-sibling::item[1]

Select the nearest preceding item sibling in this reverse-axis step.

Comparison: node-set = value

May succeed for one matching node; test unequal values separately.

Namespace: local-name() and namespace-uri()

Compare both parts of a name in this fixed CLI example.

Missing data: count(/study/missing)

Return zero for no selected nodes; no raw-node serialization is needed.

Numeric sum: sum(/study/item/@minutes)

Add authored numeric attributes; a nonnumeric member can yield NaN.

Support file: XPath 1.0 Introduction practice instructions.txt
Staged content only; no browser export acceptance or release.
Use the fixed commands shown with the authored input files. The local validator also exercises separate expected rejections. No external inputs, runtime downloads or installations.
Use the installed /usr/bin/xmllint with XML_CATALOG_FILES and SGML_CATALOG_FILES empty and --nonet. The test harness supplies explicit arguments without a shell and a five-second deadline for each owned process. This old installed parser is recorded for reproducible learning, not recommended for hostile documents. --nonet is not a local-file sandbox. These authored fixtures contain no external-resource constructs. Local examples were tested on macOS 27.0.1 arm64; no complete standards conformance or other-platform result is claimed. No browser runs teaching code.

Examples and practice

All code and values below are inert reading material. Preserve each exact filename and companion when making your own practice copy.

XPath 01 - Select a value

The result is a string from one selected attribute. If the selector matches nothing, this conversion prints an empty line; the caller must decide whether that is acceptable.

Filename: XPath 01 Select a value.xpath

string(/study/item[@id="b"]/@name)

Expected output:

code

Recorded invocation:

XML_CATALOG_FILES= SGML_CATALOG_FILES= /usr/bin/xmllint --nonet --xpath 'string(/study/item[@id="b"]/@name)' "XPath 01 Input.xml"

Companion: XPath 01 Input.xml

<study><item id="a" name="read"/><item id="b" name="code"/></study>

Typed result contract

variant
xpath
summary
code
stdout

Type: string

"code\n"
exit status

Type: integer

0
stderr
match
exact
value

Authored case table

variant
xpath
rows
  1. name
    example
    input

    Type: string

    "<study><item id=\"a\" name=\"read\"/><item id=\"b\" name=\"code\"/></study>"
    stdout

    Type: string

    "code\n"
    exit status

    Type: integer

    0
    stderr contains

    Type: string

    ""
  2. name
    missing selected attribute
    input

    Type: string

    "<study><item id=\"b\"/></study>"
    stdout

    Type: string

    "\n"
    exit status

    Type: integer

    0
    stderr contains

    Type: string

    ""
  3. name
    escaped text
    input

    Type: string

    "<study><item id=\"b\" name=\"read &amp; write\"/></study>"
    stdout

    Type: string

    "read & write\n"
    exit status

    Type: integer

    0
    stderr contains

    Type: string

    ""
XPath 02 - Compare values

Equality and inequality each find a matching comparison here; they are not complements for this node set. A nonempty string converts to true.

Filename: XPath 02 Compare values.xpath

concat(/study/item = "read", "|", /study/item != "read", "|", boolean("false"))

Expected output:

true|true|true

Recorded invocation:

XML_CATALOG_FILES= SGML_CATALOG_FILES= /usr/bin/xmllint --nonet --xpath 'concat(/study/item = "read", "|", /study/item != "read", "|", boolean("false"))' "XPath 02 Input.xml"

Companion: XPath 02 Input.xml

<study><item>read</item><item>code</item></study>

Typed result contract

variant
xpath
summary
true|true|true
stdout

Type: string

"true|true|true\n"
exit status

Type: integer

0
stderr
match
exact
value

Authored case table

variant
xpath
rows
  1. name
    example
    input

    Type: string

    "<study><item>read</item><item>code</item></study>"
    stdout

    Type: string

    "true|true|true\n"
    exit status

    Type: integer

    0
    stderr contains

    Type: string

    ""
  2. name
    only matching member
    input

    Type: string

    "<study><item>read</item></study>"
    stdout

    Type: string

    "true|false|true\n"
    exit status

    Type: integer

    0
    stderr contains

    Type: string

    ""
  3. name
    no member
    input

    Type: string

    "<study/>"
    stdout

    Type: string

    "false|false|true\n"
    exit status

    Type: integer

    0
    stderr contains

    Type: string

    ""
XPath 03 - Place the predicate

The left expression selects each parent’s first item; parentheses on the right filter the combined selection. This XML tree makes the difference visible.

Filename: XPath 03 Place the predicate.xpath

concat(count(//item[1]), "|", count((//item)[1]))

Expected output:

2|1

Recorded invocation:

XML_CATALOG_FILES= SGML_CATALOG_FILES= /usr/bin/xmllint --nonet --xpath 'concat(count(//item[1]), "|", count((//item)[1]))' "XPath 03 Input.xml"

Companion: XPath 03 Input.xml

<shelves><shelf><item>A</item><item>B</item></shelf><shelf><item>C</item></shelf></shelves>

Typed result contract

variant
xpath
summary
2|1
stdout

Type: string

"2|1\n"
exit status

Type: integer

0
stderr
match
exact
value

Authored case table

variant
xpath
rows
  1. name
    example
    input

    Type: string

    "<shelves><shelf><item>A</item><item>B</item></shelf><shelf><item>C</item></shelf></shelves>"
    stdout

    Type: string

    "2|1\n"
    exit status

    Type: integer

    0
    stderr contains

    Type: string

    ""
  2. name
    one parent
    input

    Type: string

    "<shelves><shelf><item>A</item><item>B</item></shelf></shelves>"
    stdout

    Type: string

    "1|1\n"
    exit status

    Type: integer

    0
    stderr contains

    Type: string

    ""
  3. name
    no items
    input

    Type: string

    "<shelves/>"
    stdout

    Type: string

    "0|0\n"
    exit status

    Type: integer

    0
    stderr contains

    Type: string

    ""
XPath 04 - Select the namespace

The unprefixed selection does not match this namespaced document. The explicit namespace-URI test selects only the study item; the other namespace is deliberately excluded.

Filename: XPath 04 Select the namespace.xpath

concat(count(/study/item), "|", count(/*[local-name()="study" and namespace-uri()="urn:atlas:study"]/*[local-name()="item" and namespace-uri()="urn:atlas:study"]))

Expected output:

0|1

Recorded invocation:

XML_CATALOG_FILES= SGML_CATALOG_FILES= /usr/bin/xmllint --nonet --xpath 'concat(count(/study/item), "|", count(/*[local-name()="study" and namespace-uri()="urn:atlas:study"]/*[local-name()="item" and namespace-uri()="urn:atlas:study"]))' "XPath 04 Input.xml"

Companion: XPath 04 Input.xml

<study xmlns="urn:atlas:study" xmlns:o="urn:atlas:other"><item/><o:item/></study>

Typed result contract

variant
xpath
summary
0|1
stdout

Type: string

"0|1\n"
exit status

Type: integer

0
stderr
match
exact
value

Authored case table

variant
xpath
rows
  1. name
    example
    input

    Type: string

    "<study xmlns=\"urn:atlas:study\" xmlns:o=\"urn:atlas:other\"><item/><o:item/></study>"
    stdout

    Type: string

    "0|1\n"
    exit status

    Type: integer

    0
    stderr contains

    Type: string

    ""
  2. name
    different prefix same namespace
    input

    Type: string

    "<s:study xmlns:s=\"urn:atlas:study\"><s:item/><s:item/></s:study>"
    stdout

    Type: string

    "0|2\n"
    exit status

    Type: integer

    0
    stderr contains

    Type: string

    ""
  3. name
    only other namespace item
    input

    Type: string

    "<study xmlns=\"urn:atlas:study\"><item xmlns=\"urn:atlas:other\"/></study>"
    stdout

    Type: string

    "0|0\n"
    exit status

    Type: integer

    0
    stderr contains

    Type: string

    ""
XPath 05 - Summarize selected tasks

Solution: repeat the same selection for count and sum. This query assumes each selected minutes attribute is numeric; the boundary case shows how invalid text breaks that assumption.

Filename: XPath 05 Summarize selected tasks.xpath

concat(count(/study/item[@kind="practice"]), "|", sum(/study/item[@kind="practice"]/@minutes))

Expected output:

2|30

Recorded invocation:

XML_CATALOG_FILES= SGML_CATALOG_FILES= /usr/bin/xmllint --nonet --xpath 'concat(count(/study/item[@kind="practice"]), "|", sum(/study/item[@kind="practice"]/@minutes))' "XPath 05 Input.xml"

Companion: XPath 05 Input.xml

<study><item kind="practice" minutes="20"/><item kind="rest" minutes="5"/><item kind="practice" minutes="10"/></study>

Typed result contract

variant
xpath
summary
2|30
stdout

Type: string

"2|30\n"
exit status

Type: integer

0
stderr
match
exact
value

Authored case table

variant
xpath
rows
  1. name
    example
    input

    Type: string

    "<study><item kind=\"practice\" minutes=\"20\"/><item kind=\"rest\" minutes=\"5\"/><item kind=\"practice\" minutes=\"10\"/></study>"
    stdout

    Type: string

    "2|30\n"
    exit status

    Type: integer

    0
    stderr contains

    Type: string

    ""
  2. name
    empty selection
    input

    Type: string

    "<study/>"
    stdout

    Type: string

    "0|0\n"
    exit status

    Type: integer

    0
    stderr contains

    Type: string

    ""
  3. name
    nonnumeric minutes
    input

    Type: string

    "<study><item kind=\"practice\" minutes=\"oops\"/></study>"
    stdout

    Type: string

    "1|NaN\n"
    exit status

    Type: integer

    0
    stderr contains

    Type: string

    ""

Practice: Before reading the solution, write a query that reports the number and total minutes of practice tasks. Predict the empty and nonnumeric cases.

XPath 06 - Find a nearby sibling

Solution: the reverse-axis predicate picks the nearest preceding item sibling, ignoring the note. Parenthesizing the entire preceding-sibling selection before [1] would instead choose the first in document order.

Filename: XPath 06 Find a nearby sibling.xpath

string(/study/item[@id="c"]/preceding-sibling::item[1]/@id)

Expected output:

b

Recorded invocation:

XML_CATALOG_FILES= SGML_CATALOG_FILES= /usr/bin/xmllint --nonet --xpath 'string(/study/item[@id="c"]/preceding-sibling::item[1]/@id)' "XPath 06 Input.xml"

Companion: XPath 06 Input.xml

<study><item id="a"/><note/><item id="b"/><item id="c"/></study>

Typed result contract

variant
xpath
summary
b
stdout

Type: string

"b\n"
exit status

Type: integer

0
stderr
match
exact
value

Authored case table

variant
xpath
rows
  1. name
    example
    input

    Type: string

    "<study><item id=\"a\"/><note/><item id=\"b\"/><item id=\"c\"/></study>"
    stdout

    Type: string

    "b\n"
    exit status

    Type: integer

    0
    stderr contains

    Type: string

    ""
  2. name
    only one preceding item
    input

    Type: string

    "<study><item id=\"a\"/><item id=\"c\"/></study>"
    stdout

    Type: string

    "a\n"
    exit status

    Type: integer

    0
    stderr contains

    Type: string

    ""
  3. name
    none preceding
    input

    Type: string

    "<study><item id=\"c\"/></study>"
    stdout

    Type: string

    "\n"
    exit status

    Type: integer

    0
    stderr contains

    Type: string

    ""

Practice: Find the nearest earlier item before c. Predict the result if b is removed, then if every earlier item is removed.

Separate expected outcome: xpath-empty
id
xpath-empty
variant
xpath-cli
category
CLI empty node-set result; expression is valid
inputs
expression

Type: string

"/study/missing"
xml

Type: string

"<study/>"
stdout

Type: string

""
exit status

Type: integer

10
stderr
match
contains
value
XPath set is empty
execution meaning
Recorded command outcome; separate from successful teaching examples.
individual download control
false
Separate expected outcome: xpath-invalid
id
xpath-invalid
variant
xpath-cli
category
Malformed query rejected with argument-count diagnostic on this installed evaluator
inputs
expression

Type: string

"count("
xml

Type: string

"<study/>"
stdout

Type: string

""
exit status

Type: integer

10
stderr
match
contains
value
Invalid number of arguments
execution meaning
Recorded command outcome; separate from successful teaching examples.
individual download control
false

Prompt templates

Planning text only; no AI service or export control is activated here.

Build a small contract
Create an original tiny example with explicit inputs and expected results. Keep the stated version, classify the query or schema role accurately and state what remains outside the introduction. Language: {language}. Goal: {goal}. Runtime: {runtime}. Constraints: {constraints}.
Debug the correct stage
Use the smallest authored reproducer and exact diagnostic. Separate XML parsing, expression/schema preparation and result/instance validation. Do not weaken rules just to make a case pass. Language: {language}. Goal: {goal}. Runtime: {runtime}. Constraints: {constraints}.
Test boundaries
Propose normal, missing, empty, namespace and boundary cases. Keep expected rejection separate from a successful query or valid instance; never claim tests were executed without evidence. Language: {language}. Goal: {goal}. Runtime: {runtime}. Constraints: {constraints}.
Refactor with preserved behavior
Simplify the authored query or schema while preserving explicit accepted/rejected cases and namespace meaning. Do not add external resources or generated query syntax from user text. Language: {language}. Goal: {goal}. Runtime: {runtime}. Constraints: {constraints}.
Document assumptions
Explain the input contract, namespace policy, version, installed tool and actual observed results. Distinguish this introductory subset from deep or full-language coverage. Language: {language}. Goal: {goal}. Runtime: {runtime}. Constraints: {constraints}.

Historical measure scopes

These measures describe separate, overlapping source-example checks. They are not additive totals or website acceptance results.

  1. name
    teaching_examples
    count
    6
    unit
    examples including solutions
    scope
    Historical root module evidence; overlapping measures are not additive.
    tested on
    2026-10-28
    runtime
    xmllint / libxml2 2.9.13; macOS 27.0.1 arm64
  2. name
    exercise_solutions
    count
    2
    unit
    solutions included in examples
    scope
    Historical root module evidence; overlapping measures are not additive.
    tested on
    2026-10-28
    runtime
    xmllint / libxml2 2.9.13; macOS 27.0.1 arm64
  3. name
    query_result_cases
    count
    18
    unit
    query cases
    scope
    Historical root module evidence; overlapping measures are not additive.
    tested on
    2026-10-28
    runtime
    xmllint / libxml2 2.9.13; macOS 27.0.1 arm64
  4. name
    schema_instance_cases
    count
    0
    unit
    schema instance cases
    scope
    Historical root module evidence; overlapping measures are not additive.
    tested on
    2026-10-28
    runtime
    xmllint / libxml2 2.9.13; macOS 27.0.1 arm64
  5. name
    separate_error_outcomes
    count
    2
    unit
    separate error outcomes
    scope
    Historical root module evidence; overlapping measures are not additive.
    tested on
    2026-10-28
    runtime
    xmllint / libxml2 2.9.13; macOS 27.0.1 arm64

Recorded test scope

status
passed for local introductory examples only
tested on
2026-10-28
runtime
xmllint / libxml2 2.9.13; macOS 27.0.1 arm64
teaching examples
6
exercise solutions
2
query result cases
18
schema instance cases
0
separate error outcomes
2
scope
Example results only; browser/export and release gates are open.

These are dated source-example results, not website browser, export, PDF or cross-platform acceptance.

Further reading

References are the reviewed source snapshot; they have not been rechecked at website publication time.