Skip to main content

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 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xpath_builder-0.1.1.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xpath_builder-0.1.1-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

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

Hashes for xpath_builder-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2218d79d480c1414a5ddae20e957737df66e513ba958221f5bb819b8229d96f3
MD5 53419ba896416bfd5fdb309cec3b6310
BLAKE2b-256 badee0eb2413366397b8f4bd2f639560b660f04a4243e456ed87eef9f2a81216

See more details on using hashes here.

Provenance

The following attestation bundles were made for xpath_builder-0.1.1.tar.gz:

Publisher: publish.yaml on KaiErikNiermann/xpath-builder

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for xpath_builder-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2a5632328c7da47fb03e1799b7a565fc9a21c5a843cf176d5cab25441581ef8b
MD5 edcf2294f5d93f0196e96a8154645b49
BLAKE2b-256 7692b049e7617fd06f266fbf3b42058fb2803ece64d838812212db7c58348ee7

See more details on using hashes here.

Provenance

The following attestation bundles were made for xpath_builder-0.1.1-py3-none-any.whl:

Publisher: publish.yaml on KaiErikNiermann/xpath-builder

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page