Skip to content

Commit 5fdfc58

Browse files
committed
minor #10945 Clarify using chained filterXPath (benito103e)
This PR was merged into the 3.4 branch. Discussion ---------- Clarify using chained filterXPath filterXpath() return a crawler that is considered as a fake parent of the elements inside it. So we must re-specify the parent node to access direct children. <!-- If your pull request fixes a BUG, use the oldest maintained branch that contains the bug (see https://symfony.com/roadmap for the list of maintained branches). If your pull request documents a NEW FEATURE, use the same Symfony branch where the feature was introduced (and `master` for features of unreleased versions). --> Commits ------- 17e3d81 Clarify using chained filterXPath
2 parents 3bb644b + 17e3d81 commit 5fdfc58

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

components/dom_crawler.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ Node Filtering
7676
Using XPath expressions is really easy::
7777

7878
$crawler = $crawler->filterXPath('descendant-or-self::body/p');
79+
80+
// To chained "filterXPath" : be aware that filterXPath is evaluated in the context of the crawler
81+
$crawler->filterXPath('parent')->each(function (Crawler $parentCrawler, $i) {
82+
// NOK : Direct child can not be found
83+
$childCrawler = $parentCrawler->filterXPath('child-tag/sub-child-tag');
84+
85+
// OK : You must specify parent tag
86+
$subChildCrawler = $parentCrawler->filterXPath('parent/child-tag/sub-child-tag');
87+
$subChildCrawler = $parentCrawler->filterXPath('node()/child-tag/sub-child-tag');
88+
});
89+
7990

8091
.. tip::
8192

0 commit comments

Comments
 (0)