Find if node has siblings using simplexml

I am trying to find if a particular node has brothers and sisters, and if so, I would like to know what kind of brothers and sisters it is.

Is it possible?

+4
source share
3 answers

To select a brother, node, you must use the appropriate XPath ax. Here's how to select all <= w630> siblings (ignoring node itself)

$siblings = $node->xpath('preceding-sibling::* | following-sibling::*'); 

That is all you have to do.

+8
source

I think using xpath is the best choice here:

 <?php $string = <<<XML <?xml version='1.0'?> <document> <title>Forty What?</title> <from>Joe</from> <to>Jane</to> <body> I know that the answer -- but what the question? </body> </document> XML; function get_all_siblings(SimpleXMLElement $node) { return $node->xpath('preceding-sibling::* | following-sibling::*'); } $xml = simplexml_load_string($string); foreach (get_all_siblings($xml->to) as $e) echo $e->getName()."\n"; ?> 

Results in:

 title from body 
+1
source
 $xml = new SimpleXMLElement($xmlstr); $xmlNode = $xml->xpath('root/yourNodeName'); $nodeCount = count($xmlNode); 

Not sure if this is still useful to you.

0
source

Source: https://habr.com/ru/post/1299921/


All Articles