Reputation: 3282
Consider this simple XML schema:
<products>
<category name="furniture">
<item name="chair">
<size>
<small>10</small>
...and many more
<large>20</large>
</size>
</item>
</category>
</products>
I'm looking to list the child elements of <size>
without their actual content using XPATH.
$xml=simple_load_file('file.xml');
foreach ($xml->xpath("products/category[@=name'furniture']/item[@name='chair]/size") as $size)
echo $size->????? . '<br>';
$size->children();
outputs the actual text as such:
10
20
I'm looking for the following output:
small
...rest of elements
large
Upvotes: 0
Views: 139
Reputation: 19512
You have some errors in you Xpath expression. The first = is in the wrong position and the chair string is missing the closing quote. Adding /*
You can select the child nodes of size directly.
/products/category[@name='furniture']/item[@name='chair']/size/*
Full source using DOMDocument:
$xml = <<<'XML'
<products>
<category name="furniture">
<item name="chair">
<size>
<small>10</small>
<large>20</large>
</size>
</item>
</category>
</products>
XML;
$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXPath($dom);
$nodes = $xpath->evaluate("/products/category[@name='furniture']/item[@name='chair']/size/*");
foreach ($nodes as $node) {
echo $node->localName, "\n";
}
Upvotes: 0