Search by

codealfa / css2xpath

codealfa

CSS to XPath Converter

Package info

github.com/codealfa/css2xpath

pkg:composer/codealfa/css2xpath

Statistics

Installs: 14

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

3.0.1 2026-09-01 16:02 UTC

This package is auto-updated.

Last update: 2026-09-01 16:03:54 UTC


README

Installation

Run the following command to use the library in your projects.

composer require codealfa/css2xpath

Basic Usage

use CodeAlfa\Css2Xpath\Css2XpathConverter;
use CodeAlfa\Css2Xpath\SelectorFactory;

$converter = new Css2XpathConverter(new SelectorFactory());
$xPath = $converter->convert('p#main, div.container');
var_dump($xPath);

Output:

descendant-or-self::p[@id="main"]|descendant-or-self::div[@class and contains(concat(" ", normalize-space(@class), " "), " container ")]

Notes

  • The Selector classes are extendable if you need to add or change functionality. You'll just need to create your own SelectorFactory or extend the existing one to inject into the Css2XpathConverter class.

  • Some CSS pseudo-selectors only make sense in the context of a web browser. The following pseudo-classes are supported:

    • :enabled
    • :disabled
    • :read-only
    • :read-write
    • :checked
    • :required
    • :root
    • :empty
    • :first-child
    • :last-child
    • :only-child
    • :first-of-type
    • :last-of-type
    • :only-of-type
    • :nth-child()
    • :nth-last-child()
    • :nth-of-type()
    • :nth-last-of-type()
    • :not
    • :has
    • :is
    • :where

    Unsupported pseudo-classes are ignored. CSS pseudo-elements such as ::before, ::after, ::first-line, and ::first-letter are recognized but do not add a node to the XPath result.

License

GPL-3.0 or later