codealfa/css2xpath

CSS 转 XPath 转换器

1.0 2024-09-30 20:32 UTC

This package is auto-updated.

Last update: 2024-10-02 15:41:35 UTC


README

安装

运行以下命令以在您的项目中使用库。

composer require codealfa/css2xpath

基本用法

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

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

输出

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

注意事项

  • 选择器类是可扩展的,如果您需要添加或更改功能,只需创建自己的 SelectorFactory 或扩展现有的一个以注入到 Css2XpathConverter 类中。
  • 一些 CSS 伪选择器仅在网页浏览器上下文中有意义,因此仅实现了以下伪选择器。所有其他的选择器都被忽略
    • :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
    • :not
    • :has

许可证

GPL-3.0 或更高版本