Merge two DOMNodeLists into xpath

I have two lists of DOMNodeLists

$textNodes = $xpath->query('//text()'); 

and

 $titleNodes = $xpath->query('//@title'); 

How can I combine them with DOMNodeLists to use it with a foreach ?

+4
source share
1 answer

XPath supports operator | to combine 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; } 
+4
source

All Articles