Skip to content

Commit 17e3d81

Browse files
authored
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.
1 parent da51a30 commit 17e3d81

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
@@ -78,6 +78,17 @@ Node Filtering
7878
Using XPath expressions is really easy::
7979

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

8293
.. tip::
8394

0 commit comments

Comments
 (0)