-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfilter-example2.php
More file actions
40 lines (34 loc) · 892 Bytes
/
filter-example2.php
File metadata and controls
40 lines (34 loc) · 892 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
namespace SimpleXmlReader;
use SimpleXmlReader\PathIterator;
require(__DIR__ . '/../src/SimpleXmlReader/autoload.php');
$xml = SimpleXmlReader::openFromString('
<root>
<group>
<type>pet</type>
<animal type="cat">
<hastail>yes</hastail>
</animal>
<animal type="dog">
<hastail>yes</hastail>
</animal>
</group>
<group>
<type>wild</type>
<animal type="kakariki">
<hastail>no</hastail>
</animal>
</group>
</root>
');
foreach ($xml->path('root/group/animal', SimpleXmlReader::RETURN_SIMPLE_XML, function ($xr, $crumbs) {
$path = implode("/", $crumbs);
if ($path == "root/group/type") {
if ($xr->readString() != 'pet') {
return PathIterator::SIBLINGS_ARE_INVALID;
}
}
return PathIterator::ELEMENT_IS_VALID;
}) as $animal) {
echo "A {$animal->attributes()->type} has a tail? {$animal->hastail}!\n";
}