XPath Syntax and Expressions

Estimated study time: 16 minutes.

XPath expressions describe a path through an XML document's tree, combining simple building blocks to select exactly the nodes you need.

Absolute vs Relative Paths

/course/modules/module        <!-- absolute: starts from the root -->
modules/module                <!-- relative: starts from the current node -->
//module                      <!-- selects module anywhere in the document -->

Selecting Attributes

//course/@id

The @ symbol selects an attribute rather than an element.

Predicates (Filtering)

//module[1]                       <!-- the first module element -->
//course[@level='beginner']       <!-- course with a specific attribute value -->
//module[text()='SQL for Analysis']  <!-- module matching exact text -->

Predicates, written in square brackets, filter the selected nodes down to only those matching a condition.

Wildcards

//*            <!-- every element in the document -->
/course/*      <!-- every direct child of course -->
//module/@*    <!-- every attribute on every module element -->

Combining Conditions

//course[@level='beginner' and @id]
💡 Tip: Start with // while exploring an unfamiliar document — it finds matching elements anywhere, which is more forgiving than getting an absolute path exactly right on the first try.

Ready to go beyond the basics?

Get hands-on training, live mentorship, and placement support with the Data Analytics Course at Uncodemy.

Explore XML Course →