How XPath Works
XPath uses path expressions to navigate through the hierarchical structure of XML/HTML documents. You can select elements, attributes, text nodes, and more using a syntax similar to file system paths. Key Benefits:- Extract text nodes directly (without HTML tags)
- Navigate to parent elements
- Use advanced conditional logic
- Perfect for RSS/XML feeds
- Support for regex matching
XPath Versions in changedetection.io
changedetection.io supports two XPath implementations:- XPath 2.0/3.0 (Default)
- XPath 1.0
Prefix:
xpath: (or no prefix for // syntax)Engine: elementpath libraryFeatures:- XPath 2.0 and 3.0 support
- Better namespace handling
- Automatic default namespace support for RSS/Atom feeds
- Modern expression syntax
Basic XPath Syntax
Selecting Elements
<div> elements anywhere in the document.
<div> elements that are direct children of <body>.
Selecting by Attributes
class="price".
<a> elements that have an href attribute.
Extracting Text
<h1> elements (without HTML tags).
Extracting Attributes
src attribute from all images.
Practical Examples
Monitor Product Price
Extract Multiple Fields
Monitor RSS Feed Items
- XPath 2.0/3.0
- XPath 1.0
Automatic default namespace handling:Works directly with RSS feeds without namespace handling.
Advanced Techniques
Conditional Selection
Contains Text
Contains Text
Logical Operators
Logical Operators
Position-based Selection
Position-based Selection
<li> element.<li> element.<li> elements after the second one.Parent and Sibling Navigation
Regular Expression Matching
changedetection.io supports EXSLT regex functions:Working with Namespaces
RSS/Atom Feeds
Handling CDATA Sections
Some RSS feeds wrap content in CDATA://description/text() will extract the content.
Combining Include and Remove Filters
Remove filters must use the
xpath: or xpath1: prefix explicitly.Testing XPath Expressions
Using Browser DevTools
- Open DevTools Console (F12)
- Use
$x()to test XPath: - Verify the results
Online XPath Testers
- XPath Tester
- Code Beautify XPath
- Use sample HTML/XML to test expressions
Common Patterns
Pattern: Extract Plain Text
Pattern: Monitor Table Data
Pattern: Get Meta Description
Pattern: Track Stock Status
Pattern: Extract Link URLs
Common Pitfalls
When to Use XPath
Good for:
- Monitoring RSS/Atom feeds
- Extracting text without HTML tags
- Complex conditional filtering
- Navigating to parent elements
- XML documents
- When you need regex matching
- Extracting specific attributes
Not ideal for:
- JSON APIs (use JSON filtering instead)
- When CSS selectors are sufficient (simpler syntax)
- When you want visual selector support
XPath vs CSS Selectors
| Feature | XPath | CSS Selectors |
|---|---|---|
| Text extraction | //div/text() | Not possible |
| Parent selection | //span/parent::div | Not possible |
| Attribute extraction | //@href | Not directly |
| Conditional logic | [contains(@class, 'x')] | Limited |
| Visual selector | ❌ Not available | ✅ Available |
| RSS/XML feeds | ✅ Excellent | ❌ Not suitable |
| Learning curve | Steeper | Easier |
Real-World Examples
Example: Monitor Product Reviews
Example: Monitor Product Reviews
Example: Track News Headlines from RSS
Example: Track News Headlines from RSS
Example: Extract JSON-LD Price
Example: Extract JSON-LD Price
Example: Monitor Table Changes
Example: Monitor Table Changes
Example: Get All Links in a Section
Example: Get All Links in a Section
Debugging Tips
Returns Empty Results
- Check if you’re using the right XPath version
- For RSS/XML, try switching between
xpath:andxpath1: - Use
local-name()for namespaced elements - Verify element exists (check browser’s element inspector)
- Test in browser console:
$x('your-xpath-here')
Returns Unexpected Content
- Add
/text()to extract only text content - Use
[1]or[last()]to get specific positions - Add more specific conditions with
[@attribute='value'] - Check if you need
//(any level) vs/(direct child)
Related Topics
- CSS Selectors - Simpler alternative for HTML content
- JSON Filtering - Extract data from JSON responses
- RSS Monitoring - Specific guide for RSS feeds