A fluent, type-safe Python DSL for building XPath 2.0 expressions
Project description
xpath-builder
A fluent, type-safe Python DSL for building XPath 2.0 expressions without string concatenation.
Installation
pip install xpath-builder
Quick Start
from xpath_builder import E, STAR, Pred
# Find all divs with class containing "container"
xpath = E("div").any().where(
Pred.attr("class").contains.any_of("container")
)
print(xpath.compile())
# //div[contains(@class, 'container')]
Features
- Fluent API — chain methods to build complex XPath expressions naturally
- Zero dependencies — pure Python, no runtime dependencies
- Type-safe comparisons — generic
Ops[T]for numeric and string comparisons - XPath 2.0 — full support including
matches(), token matching, and more - Optional validation — validate expressions with
elementpathorlxml
Usage
Nodes and Paths
from xpath_builder import E, STAR, TEXT, COMMENT
E("div").any() # //div
E("div").root() # /div
E("ul").child(E("li")) # ul/li
E("div").desc(E("span")) # div//span
STAR.any() # //*
Predicates
from xpath_builder import Pred
# Attribute checks
Pred.attr("class").contains.any_of("btn", "button")
Pred.attr("id").startswith.any_of("vue-", "react-")
Pred.attr("disabled").exists()
Pred.attr("hidden").missing()
# Text content
Pred.text_contains("Hello")
Pred.text_matches(r"^\d+$")
# Numeric comparisons
Pred.attr("data-count").as_num.gt(10)
Pred.attr("price").as_num.between(5, 99)
# Case-insensitive matching
Pred.attr("type", case_insensitive=True).as_str.eq("submit")
Combining Predicates
# AND / OR / NOT
visible = Pred.attr("hidden").missing() & Pred.attr("display").as_str.ne("none")
clickable = Pred.attr("href").exists() | Pred.attr("onclick").exists()
not_disabled = Pred.attr("disabled").exists().neg()
Chaining
selector = (
E("div")
.any()
.where(Pred.attr("class").contains_tokens.any_of("card"))
.child(E("a"))
.where(Pred.attr("href").startswith.any_of("https://"))
.first()
)
Positional Filtering
E("li").any().first() # //li[position() = 1]
E("li").any().nth(3) # //li[position() = 3]
Example: Finding Ad Elements
from xpath_builder import STAR, Pred
ad_selector = (
STAR.any()
.where(
Pred.attr("class").contains.any_of(
"ads", "ad", "advert", "sponsored", "promo"
)
)
)
Documentation
Full documentation is available at xpath-builder.readthedocs.io.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file xpath_builder-0.1.1.tar.gz.
File metadata
- Download URL: xpath_builder-0.1.1.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2218d79d480c1414a5ddae20e957737df66e513ba958221f5bb819b8229d96f3
|
|
| MD5 |
53419ba896416bfd5fdb309cec3b6310
|
|
| BLAKE2b-256 |
badee0eb2413366397b8f4bd2f639560b660f04a4243e456ed87eef9f2a81216
|
Provenance
The following attestation bundles were made for xpath_builder-0.1.1.tar.gz:
Publisher:
publish.yaml on KaiErikNiermann/xpath-builder
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
xpath_builder-0.1.1.tar.gz -
Subject digest:
2218d79d480c1414a5ddae20e957737df66e513ba958221f5bb819b8229d96f3 - Sigstore transparency entry: 985919025
- Sigstore integration time:
-
Permalink:
KaiErikNiermann/xpath-builder@943138a3d3ed3246de1cdec7b87d3e01c4429c06 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/KaiErikNiermann
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@943138a3d3ed3246de1cdec7b87d3e01c4429c06 -
Trigger Event:
release
-
Statement type:
File details
Details for the file xpath_builder-0.1.1-py3-none-any.whl.
File metadata
- Download URL: xpath_builder-0.1.1-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2a5632328c7da47fb03e1799b7a565fc9a21c5a843cf176d5cab25441581ef8b
|
|
| MD5 |
edcf2294f5d93f0196e96a8154645b49
|
|
| BLAKE2b-256 |
7692b049e7617fd06f266fbf3b42058fb2803ece64d838812212db7c58348ee7
|
Provenance
The following attestation bundles were made for xpath_builder-0.1.1-py3-none-any.whl:
Publisher:
publish.yaml on KaiErikNiermann/xpath-builder
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
xpath_builder-0.1.1-py3-none-any.whl -
Subject digest:
2a5632328c7da47fb03e1799b7a565fc9a21c5a843cf176d5cab25441581ef8b - Sigstore transparency entry: 985919083
- Sigstore integration time:
-
Permalink:
KaiErikNiermann/xpath-builder@943138a3d3ed3246de1cdec7b87d3e01c4429c06 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/KaiErikNiermann
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@943138a3d3ed3246de1cdec7b87d3e01c4429c06 -
Trigger Event:
release
-
Statement type: