Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 39 ยท XML, XPath and XSLT

XPath Basics

Learn XPath to select elements, attributes and text from XML: path steps, descendants, predicates, wildcards, axes, functions and common patterns, with a runnable mini evaluator.

XPath is a small query language for XML. Like a file path locates a file in folders, an XPath expression locates nodes in the document tree. It is the foundation of XSLT, XQuery, XML Schema assertions and the selectors of many testing and scraping tools (Selenium, Scrapy, browsers' document.evaluate).

Browsers offer document.evaluate, but the Node runner has no XPath engine. The examples use a compact evaluator written for this course. It supports /, //, names, *, @attr, text() and simple predicates, enough to demonstrate the ideas. Real XPath 1.0/3.1 is much bigger; use a real engine (libxml2, Saxon, browser) in practice.

The sample document

We will query this catalog for all examples.

XML
<catalog>
  <book id="b1" lang="en"><title>The Little Compiler</title><price>29.90</price><genre>tech</genre></book>
  <book id="b2" lang="es"><title>Sistemas de tipos</title><price>35.00</price><genre>tech</genre></book>
  <book id="b3" lang="en"><title>Garden Notes</title><price>12.50</price><genre>hobby</genre></book>
</catalog>

Path steps

Expression Selects
/catalog the root element named catalog
/catalog/book every book child of catalog
//title every title anywhere in the document
/catalog/book/title/text() the text of each title
//book/@id the id attribute of every book
//book/* all element children of each book

A leading / starts at the document root; // means "at any depth".

Node.js Selecting with path steps

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Predicates

A predicate in square brackets filters the nodes selected so far.

Expression Meaning
//book[1] the first book (XPath positions start at 1)
//book[last()] the last book
//book[@lang='en'] books whose lang attribute is "en"
//book[genre='tech'] books with a genre child equal to "tech"
//book[price>20] books whose price is greater than 20
//book[@id and price] books with both an id and a price
Node.js Filtering with predicates

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

(The last line chains predicates: first filter by language, then pick the second of the matches.)

Axes

A step has the form axis::node-test[predicate]. The path syntax above uses shorthand for the default child axis. The full set lets you walk in any direction:

Axis Selects Shorthand
child:: children (default)
descendant:: all descendants //
parent:: the parent ..
ancestor:: all ancestors
following-sibling:: later siblings
preceding-sibling:: earlier siblings
attribute:: attributes @
self:: the node itself .

Example: //title[.='Garden Notes']/../price finds the price of the book titled Garden Notes, by climbing to the parent with ...

Functions

XPath 1.0 has a library of functions usable in predicates and expressions:

  • Nodes: count(//book), position(), last(), name()
  • Strings: contains(title,'Compiler'), starts-with(@id,'b'), string-length(), normalize-space(), concat(), substring()
  • Numbers: sum(//price), round(), floor(), ceiling()
  • Booleans: not(), true(), false()
TEXT
count(//book[genre='tech'])        -> 2
sum(//book/price)                  -> 77.4
//book[contains(title,'Notes')]    -> the Garden Notes book

Evaluating functions in practice

The stand-in evaluator does not parse function calls, but the results above are easy to reproduce with the node lists it returns, which shows what the functions compute.

Node.js What count() and sum() compute

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Namespaces in XPath

If the document uses a default namespace, plain names like //title match nothing, because the elements are not in "no namespace". Register a prefix for the URI in your host program (ns for Atom, say) and write //ns:title, or match by local-name(): //*[local-name()='title']. This is the number-one XPath surprise.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does //title select?

    The double slash means "at any depth below the context".
  2. 2. In XPath, what is the position of the first item?

    XPath positions start at 1, so //book[1] is the first book.
  3. 3. Which expression selects the id attributes of all books?

    Attributes are selected with the at sign.
  4. 4. Why does //title return nothing in a document with xmlns="http://example.com/ns"?

    Register a prefix for the namespace or use local-name().

Summary

  • XPath selects nodes with path steps: / for children, // for descendants, @ for attributes.
  • Predicates filter by position, attribute or child values; positions start at 1.
  • Axes (parent::, following-sibling::...) walk the tree in any direction.
  • Functions such as count, sum, contains and not compute over node sets.
  • Default namespaces need prefixes or local-name().

Next: XSLT basics