Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #19430 [DomCrawler] Add support for XPath expression evaluati…
…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
- Loading branch information