I have two DOMNodeLists
$textNodes = $xpath->query('//text()');
and
$titleNodes = $xpath->query('//@title');
How can I merge those to DOMNodeLists so I can use it with a foreach
loop?
XPath supports the |
operator for combining two node sets:
$textNodes = $xpath->query('//text() | //@title');
Imagine this simple example :
$xml = '<?xml version="1.0"?>
<person>
<name>joe</name>
<age>99</age>
</person>';
$doc = new DOMDocument();
$doc->loadXml($xml);
$selector = new DOMXPath($doc);
$nodes = $selector->query('//name | //age');
foreach($nodes as $node) {
echo $node->nodeName, PHP_EOL;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With