What it means
A language for locating nodes and evaluating expressions in XML-style documents, commonly used to parse HTML and extract web data.
XPath can describe target locations through node hierarchies, attributes, conditions, and axes. It expresses relationships differently than CSS selectors, making it suitable for certain extraction tasks around adjacent text or parent-child relationships.
The same expression may produce different results in different document structures, namespaces, or XPath implementations. You should first confirm the input document and then check whether the returned type is a node, string, or other type.
Focused check, 2026-12-03: in XPath 1.0, //p[1] selects each parent’s first p child; (//p)[1] selects the first match in document order from the complete set. Hypothetically, if two sections each contain a p, the former can return two nodes and the latter one. Predicate placement changes the filtering scope; these expressions are not interchangeable. Check the parser’s XPath version too.
In a scraping task
Read the address of the product link
The example looks for a link with class name title in the article element and returns the href attribute. If the real page has multiple class names or different node structures, the conditions need to be adjusted.
//article//a[@class='title']/@hrefCommon misconceptions
- Hard-coding the full path from the root node is often fragile, and node insertion can break the match.
- When matching class names with values such as @class, you should not ignore that an element may have multiple classes.
References
Organized by Northleaf Studio. The source link retains the original address to facilitate continued reading.
- MDN:XPath
Original data · Verified on 2026-11-19
- W3C · XPath 1.0 · Predicates and abbreviated syntax
Original sources · Checked 2026-12-03