simpleXML expressions

[email protected] (Jason Barnett) Sat, 03 Jul 2004 04:02:35 -0400
Newsgroups php.xml.dev
Message-ID <[email protected]>
Hi all... I'm trying to understand the new simpleXML model.  How do the 
iterator methods used with simpleXML work?  My guess is something like 
the following, but I'd appreciate it if someone could verify if my 
understanding of XPath is correct (or if this is done in some other way!?)


Jason


<?php

$xml = '<root><foo><bar/><baz/></foo></root>';

$sxe = simpleXML_load_string($xml);

// The normal way of accessing simpleXMLElement
foreach ($sxe->foo as $node)
   foreach ($node as $node2)
     $list1[] = $node2;

// My guess as to what kind of XPath matches this
// But shouldn't the xpath be /foo/* (root->foo->any)
// When I tried /foo/*, I got an error
foreach ($sxe->xpath('foo/*') as $node)
   $list2[] = $node;

// And now we test if the two lists are the same
echo $list1 == $list2 ? "They are equal\n" : "They are not equal\n";
echo '<pre>';
print_r($list1);
print_r($list2);
echo '</pre>';

// Finally, we test that the first element should be:
echo $list1[0] == $sxe->xpath('/foo/*') ? "They are equal\n" : "They are 
not equal\n";

?>