XPath

Addressing nodes inside an XML document.

XPath is taught here in 14 lessons, running from Location paths and axes through to CSS selectors, JSONPath and jq as alternatives. Each lesson takes one topic, shows the working code, and links onward to the next, so the course can be read straight through in order.

TrackXML & Web Services
Lessons14
LevelBeginner to intermediate
Reading timeabout 3 hours
PrerequisitesHelpful, but not required: XML

Lessons

  1. Location paths and axesThe XPath data model, absolute and relative paths, the axes you will actually use, and node tests that select the right node type.
  2. Predicates and functionsFiltering node-sets with predicates, positional versus value tests, and the string, number and boolean functions you will use most.
  3. XPath in XSLT and in codeHow XPath drives XSLT select and match attributes, and the same expressions in Python, Java, JavaScript and the shell.
  4. The XPath data model: nodes, order and valuesXPath does not see your file, it sees a tree of seven node types. Knowing what each node contains explains most surprising query results.
  5. Namespaces and the default namespace trapWhy //book matches nothing in a document that looks perfectly fine, and how to register prefixes in every library you are likely to use.
  6. Selecting attributes, text and special nodesAttributes, text nodes, comments and processing instructions each behave differently in a predicate, and whitespace makes text nodes trickier than they look.
  7. XPath from the shell: xmllint and xmlstarletTwo command-line tools turn XPath into something you can pipe, diff and run in a build script, including on namespaced and HTML input.
  8. XPath in the browser: document.evaluateThe DOM XPath API is older than querySelector but still the only way to walk upwards and to match by text, and its result types have real consequences.
  9. XPath in Python: lxml and ElementTreeElementTree's subset, lxml's complete XPath 1.0 with extensions, and the namespace and return-type differences between the two.
  10. XPath in Java and .NETThe JDK and the BCL both ship a complete XPath 1.0 engine: compile once, reuse the object, and configure namespaces before you evaluate anything.
  11. XPath 1.0, 2.0 and 3.1: what changedThe version you are allowed to use is decided by your engine, not your preferences, and the jump from 1.0 changes what a query can even express.
  12. Robust XPath for scraping and testingPaths built from structure break the day a designer adds a wrapper. Anchor selectors to content and attributes instead, and detect breakage early.
  13. XPath performance: cost, indexes and //Descendant search is the expensive part of XPath. Knowing which constructs force a full scan tells you where to spend your optimisation effort.
  14. CSS selectors, JSONPath and jq as alternativesXPath is not always the right tool: CSS is faster and simpler for downward-only selection, and JSON needs an entirely different query language.

More in XML & Web Services

XML XSLT SOAP RESTful APIs RSS & Atom

FAQ

How long does the XPath course take?
It has 14 lessons, about 3 hours of reading. Expect roughly twice that if you type out and run every example.
Do I need prior experience for XPath?
Not strictly. It helps to have read XML first, because some lessons build on it, but every lesson explains its own assumptions.
What should I read after XPath?
Continue with XSLT (14 lessons), the next course in XML & Web Services.