DomCrawler Component
The DomCrawler component eases DOM navigation for HTML and XML documents.
…on (jakzal)
This PR was merged into the 3.2-dev branch.
Discussion
----------
[DomCrawler] Add support for XPath expression evaluation
| Q | A
| ------------- | ---
| Branch? | master
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #19162
| License | MIT
| Doc PR | TODO
Example usage:
```php
<?php
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\VarDumper\VarDumper;
require_once __DIR__.'/vendor/autoload.php';
$html = '<html>
<body>
<span id="article-100" class="article">Article 1</span>
<span id="article-101" class="article">Article 2</span>
<span id="article-102" class="article">Article 3</span>
</body>
</html>';
$crawler = new Crawler();
$crawler->addHtmlContent($html);
VarDumper::dump($crawler->filterXPath('//span[contains(@id, "article-")]')->evaluate('substring-after(@id, "-")'));
// array:3 [
// 0 => "100"
// 1 => "101"
// 2 => "102"
// ]
VarDumper::dump($crawler->evaluate('substring-after(//span[contains(@id, "article-")]/@id, "-")'));
// array:1 [
// 0 => "100"
// ]
VarDumper::dump($crawler->filterXPath('//span[@class="article"]')->evaluate('count(@id)'));
// array:3 [
// 0 => 1.0
// 1 => 1.0
// 2 => 1.0
// ]
VarDumper::dump($crawler->evaluate('count(//span[@class="article"])'));
// array:1 [
// 0 => 3.0
// ]
VarDumper::dump($crawler->evaluate('//span[1]'));
// Symfony\Component\DomCrawler\Crawler { }
```
Commits
-------
3148fad [DomCrawler] Add support for XPath expression evaluation| Failed to load latest commit information. | |||
|
|
Field |
|
|
|
|
Tests |
|
|
|
|
.gitignore |
|
|
|
|
AbstractUriElement.php |
|
|
|
|
CHANGELOG.md |
|
|
|
|
Crawler.php |
|
|
|
|
Form.php |
|
|
|
|
FormFieldRegistry.php |
|
|
|
|
Image.php |
|
|
|
|
LICENSE |
|
|
|
|
Link.php |
|
|
|
|
README.md |
|
|
|
|
composer.json |
|
|
|
|
phpunit.xml.dist |
|
|
The DomCrawler component eases DOM navigation for HTML and XML documents.