Skip to main content

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 elementpath or lxml

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

Release files for xpath-builder 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xpath-builder 0.1.1
File Size Uploaded
xpath_builder-0.1.1.tar.gz 7.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for xpath-builder 0.1.1
File Interpreter ABI Platform
xpath_builder-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 15.3 kB

Release files / xpath_builder-0.1.1.tar.gz

Download URL xpath_builder-0.1.1.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
2218d79d480c1414a5ddae20e957737df66e513ba958221f5bb819b8229d96f3
BLAKE2b-256 checksum
How to use checksums
badee0eb2413366397b8f4bd2f639560b660f04a4243e456ed87eef9f2a81216
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 24, 2026.

Transparency log

Release files / xpath_builder-0.1.1-py3-none-any.whl

Download URL xpath_builder-0.1.1-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2a5632328c7da47fb03e1799b7a565fc9a21c5a843cf176d5cab25441581ef8b
BLAKE2b-256 checksum
How to use checksums
7692b049e7617fd06f266fbf3b42058fb2803ece64d838812212db7c58348ee7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 24, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page