Coding/Learn/xml/xpath syntax and axes
XML & Schemas Foundational Course
01.Introduction & Tree Structure
02.Syntax & Well-Formedness Rules
03.Elements vs. Attributes & Best Practices
04.Namespaces (xmlns) & Prefix Resolution
05.CDATA Sections & XML Comments
06.DTD (Document Type Definition) & Entities
07.Schema Definition (XSD) & Simple Elements
08.XSD Complex Types & Order Indicators (sequence, choice, all)
09.XSD Restrictions, Facets & Pattern Matching (Regex)
10.DOM Architecture & Node Types
11.Accessing & Traversing XML DOM Nodes
12.Modifying, Creating & Deleting XML Nodes
13.DOMParser, XMLSerializer & Parsing Errors
14.AJAX & XMLHttpRequest (responseXML)
15.XPath Syntax, Predicates & Axes
16.XSLT Transformations & Templates (<xsl:template>)
17.XSLT Logic, Loops & Sorting (for-each, if, choose)
18.XQuery & FLWOR Expressions (For, Let, Where, Order, Return)
19.in Web Services: SOAP, WSDL, RSS & SVG
20.Capstone: Enterprise XML Data Pipeline & Validator
Course Certificate0%

Complete all lessons to earn

LearnXPath Syntax, Predicates & Axes
15 / 20 0%
Lesson 15 of 20
6 mins read

XPath Syntax, Predicates & Axes

PreviousNext Lesson
In Plain English

XPath (XML Path Language) uses path expressions to select nodes or node-sets in an XML document. It supports absolute (/root) and relative (//child) paths, attribute selection (@attr), filtering predicates ([price > 30]), and directional axes (parent::, child::, following-sibling::).

The Mental Model

"XPath is like a precise GPS navigation coordinate system for XML documents: instead of traversing line by line, you type a short path expression and jump directly to the exact target node anywhere in the tree."

Deep Dive: How It Works

Path Operators: / selects from the root; // selects matching nodes anywhere in the document; . is current node; .. is parent.

Attribute Selection: @id matches attribute named id.

Predicates ([...]): //book[price > 20] filters only books with price greater than 20; //book[1] selects the first book.

XPath Axes: ancestor::, descendant::, following-sibling::, parent:: navigate relative directions.

Syntax Blueprint

//book[@category="web"][price < 50]/title

Selects the <title> of all web category books whose price is under $50 anywhere in the tree.

PointCore Rules to Remember

Point star
Zero-Code Querying: Extract complex node subsets in a single query expression.
Point star
Predicates: Filter by index [1], attribute [@id="x"], or numeric condition [price > 10].
Point star
Foundation for XSLT & XQuery: XPath forms the core expression language used inside XSLT and XQuery.

Common Beginner Traps & How to Fix Them

Mistake:Assuming XPath indices start at 0.

Why it happens: In XPath, indexing starts at 1, not 0: //book[1] selects the first book, while //book[0] matches nothing.

How to fix: Remember that XPath uses 1-based indexing.

Pro Tip: In browser JavaScript, you can execute XPath queries directly using document.evaluate(xpathExpr, xmlDoc, null, XPathResult.ANY_TYPE, null)!

Live Interactive Example

Hit Run Code to see it live
XPath Queries on a Bookstore
XML 1.0 / XSD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Live Browser Preview
Click "Preview / Apply" to view the rendered output.
How it works: Shows standard XPath path expressions and filtering predicates.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Write an XPath predicate for active users

  • PointWrite the XPath expression that selects all `<username>` elements for users where attribute `@active="true"`.
  • PointPath format: `//user[@active="true"]/username`
1
2
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates