Dom and SimpleXml Xpath return types
| From: | Philip Fletcher | Date: | Wed, 03 Mar 2004 17:00:44 +0000 |
| Subject: | Dom and SimpleXml Xpath return types | ||
| Groups: | php.xml.dev | ||
| Request: | Send a blank email to php-xml-dev+get-342@lists.php.net to get a copy of this message | ||
I obviously have too much time on my hands today...
From my understanding of the XPath spec, there should be four return types:
NodeSet, number, string or boolean. Using either Dom or SimpleXml I don't
seem to be able to get anything except a NodeSet (either an object or an
array respectively).
Example: (Please enlighten me if my xpath is as bad as I think it is:)
$xml = '<entity1 name="fred">Some Text<entity2 name="bert"
/></entity1>';
$sxml = simplexml_load_string($xml);
$dom = new DomDocument();
$dom->loadXML($xml);
$xpath = new DomXpath($dom);
$xp = '//entity1[1]/@name'; //Should return string 'fred'?
//$xp = 'string(//entity1[1])'; //Should return string 'Some Text'?
//$xp = 'count(//entity1)'; //Should return int 1?
echo 'Simple Result: ' . ( $sxml->xpath($xp) ) . '<br />'; //Returns
array
echo 'Dom Result: ' . ( $xpath->query($xp) ) . '<br />'; //Returns
object
$sresult = $sxml->xpath($xp);
echo 'Simple Result[0]: ' . $sresult[0] . '<br />';
$dresult = $xpath->query($xp);
echo 'Dom Result[0]: ' . ( $dresult->firstChild->data ) . '<br />';
I don't think all the functions have been implemented yet, although string()
and count() do not error like first() does, yet returns null.
I wouldn't mind if I could do $sxml->xpath($xp)[0] for the simplexml :)
Philip