Fundamentals 6 min read

Master XPath: Essential Node Selection, Predicates, Axes, and Functions

This guide explains core XPath concepts—including node selection shortcuts, predicate filters, wildcard usage, multiple path unions, axis navigation, and built‑in functions—providing clear examples for effective XML/HTML data extraction.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master XPath: Essential Node Selection, Predicates, Axes, and Functions

1. Selecting Nodes

Common XPath expressions for node selection include: nodename – selects all child nodes of the given node name, e.g., xpath('//div') selects all div children. / – selects from the root node, e.g., xpath('/div') selects the div node at the document root. // – selects nodes anywhere in the document regardless of position, e.g., xpath('//div') selects all div elements. . – selects the current node, e.g., xpath('./div') selects div children of the current node. .. – selects the parent of the current node, e.g., xpath('..') moves up one level. @ – selects attributes, e.g., xpath('//@class') selects all class attributes.

2. Predicates

Predicates are placed inside square brackets to filter nodes by position, attribute values, or other conditions. xpath('/body/div[1]') – first div under body. xpath('/body/div[last()]') – last div under body. xpath('/body/div[last()-1]') – second‑last div under body. xpath('/body/div[position()<3]') – the first two div elements. xpath('/body/div[@class]')div elements that have a class attribute. xpath('/body/div[@class="main"]')div with class="main". xpath('/body/div[price>35.00]')div whose price child value exceeds 35.

3. Wildcards

Wildcard expressions allow selection of unknown elements: xpath('/div/*') – all child nodes of any div. xpath('/div[@*]') – all div elements that have any attribute.

4. Selecting Multiple Paths

The pipe operator | combines paths, e.g., xpath('//div|//table') selects all div and table nodes.

5. XPath Axes

Axes define node sets relative to the current node: ancestorxpath('./ancestor::*') selects all ancestor nodes. ancestor-or-selfxpath('./ancestor-or-self::*') selects ancestors and the node itself. attributexpath('./attribute::*') selects all attributes of the current node. childxpath('./child::*') selects all child nodes. descendantxpath('./descendant::*') selects all descendant nodes. followingxpath('./following::*') selects nodes after the current node’s end tag. following-siblingxpath('./following-sibling::*') selects following sibling nodes. parentxpath('./parent::*') selects the parent node. precedingxpath('./preceding::*') selects nodes before the current node’s start tag. preceding-siblingxpath('./preceding-sibling::*') selects preceding sibling nodes. selfxpath('./self::*') selects the current node itself.

6. Functions

Functions enable more flexible matching: starts-withxpath('//div[starts-with(@id,"ma")]') selects div elements whose id starts with “ma”. containsxpath('//div[contains(@id,"ma")]') selects div elements whose id contains “ma”. andxpath('//div[contains(@id,"ma") and contains(@id,"in")]') selects div elements whose id contains both “ma” and “in”. text()xpath('//div[contains(text(),"ma")]') selects div elements whose text contains “ma”.

For further reference, see the Scrapy XPath documentation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

XMLData ExtractionWeb ScrapingXPathSelectors
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.