Skip to content

Commit 827014e

Browse files
jakzalweaverryan
authored andcommitted
Documented xml namespace autodiscovery.
1 parent afdb708 commit 827014e

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

components/dom_crawler.rst

+46
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,52 @@ To remove a node the anonymous function must return false.
9595
All filter methods return a new :class:`Symfony\\Component\\DomCrawler\\Crawler`
9696
instance with filtered content.
9797

98+
Both :method:`Symfony\\Component\\DomCrawler\\Crawler::filterXPath` and
99+
:method:`Symfony\\Component\\DomCrawler\\Crawler::filter` methods work with
100+
XML namespaces, which are automatically registered.
101+
102+
.. versionadded:: 2.4
103+
Auto discovery of namespaces was introduced in Symfony 2.4.
104+
105+
Consider an XML below:
106+
107+
<?xml version="1.0" encoding="UTF-8"?>
108+
<entry
109+
xmlns="http://www.w3.org/2005/Atom"
110+
xmlns:media="http://search.yahoo.com/mrss/"
111+
xmlns:yt="http://gdata.youtube.com/schemas/2007">
112+
<id>tag:youtube.com,2008:video:kgZRZmEc9j4</id>
113+
<yt:accessControl action="comment" permission="allowed"/>
114+
<yt:accessControl action="videoRespond" permission="moderated"/>
115+
<media:group>
116+
<media:title type="plain">Chordates - CrashCourse Biology #24</media:title>
117+
<yt:aspectRatio>widescreen</yt:aspectRatio>
118+
</media:group>
119+
</entry>
120+
121+
It can be filtered with ``DomCrawler`` without a need to register namespace
122+
aliases both with :method:`Symfony\\Component\\DomCrawler\\Crawler::filterXPath`::
123+
124+
$crawler = $crawler->filterXPath('//default:entry/media:group//yt:aspectRatio');
125+
126+
and :method:`Symfony\\Component\\DomCrawler\\Crawler::filter`::
127+
128+
use Symfony\Component\CssSelector\CssSelector;
129+
130+
CssSelector::disableHtmlExtension();
131+
$crawler = $crawler->filter('default|entry media|group yt|aspectRatio');
132+
133+
.. note::
134+
135+
The default namespace is registered with a name "default".
136+
137+
.. caution::
138+
139+
To query an XML with a CSS selector, the HTML extension needs to be disabled with
140+
:method:`Symfony\\Component\\CssSelector\\CssSelector::disableHtmlExtension`
141+
to avoid converting the selector to lowercase.
142+
143+
98144
Node Traversing
99145
~~~~~~~~~~~~~~~
100146

0 commit comments

Comments
 (0)