Xpath is very powerful query language. But sometimes, you do not need this power. You need just to grab some page in simple way - using css selectors. Css selectors are widely used. They are simple.
You need additional library called xparse/css-expression-translator
Install it via composer:
composer require xparse/css-expression-translator
Configure element finder
$finder->setExpressionTranslator(new CssExpressionTranslator());
Here is full working example:
require 'vendor/autoload.php';
use Xparse\CssExpressionTranslator\CssExpressionTranslator;
use Xparse\ElementFinder\ElementFinder;
$finder = new ElementFinder('<div>
<a href="#page">123</a>
<a href="#second" class="test">321<span>ad</span></a>
</div>', ElementFinder::DOCUMENT_HTML, new CssExpressionTranslator());
# 321<span>ad</span>
echo $finder->content('a.test')->first();
This library build on top of the symfony/css-selector
https://github.com/symfony/css-selector
Add space before attribute name.
$finder->attributes('a @href');
$finder->attributes('a.test @class');
// slect node text
$finder->value('a.test node()');
There are some limits.
- Xpath is more powerful than css.
- you cant select attributes with
or
operator - fetch function result
a concat('text:', text())