I.e. DOMXPath::query won't return the context node itself as part of the result. So for example, you would get int(1) for <xml><xml/></xml> in my above code, rather than int(2) ... I think it should be int(2) in that case, otherwise there's no way of selecting the context node itself using XPath. I guess it all depends on how ".//{name}" is supposed to be interpreted.
PHP bug, I think ... am I right?
Last edited by XmisterIS; 02-09-2012 at 11:11 PM..
Tag XML doesn't contain any child nodes XML, so the count of an .//XML on the XML element will yield no results. So an output of 0 for your count is correct.
No, that's the same thing. You are still working from $xml which has no children.
but, but, but ... I thought the queries "xml" and ".//xml" would look for all elements among the context node and it's children, not just among the context node's children? I am wrong in thinking that?
EDIT: Lamped - the return value of DOMXPath::query is a DOMNodeList, which you can iterate over in a loop.
Last edited by XmisterIS; 02-09-2012 at 11:30 PM..
Correct, it will flow from parent through child within that context. That thinking is correct.
But your perception of the context is wrong. The highest you can go is documentElement, which is the root. <XML> is your root so it can only find children below this level:
PHP Code:
<?php $doc = new DOMDocument(); $doc->loadXML("<?xml version='1.0' ?><xml><xml /><xml /></xml>");
$xpath = new DOMXpath($doc);
$xmlQ = $xpath->query(".//xml"); printf('Total for .//xml: %d' . PHP_EOL, $xmlQ->length);
$xmlQ = $xpath->query(".//xml"); printf('Total for .//xml: %d' . PHP_EOL, $xmlQ->length);
This will produce the results:
Code:
Total for .//xml: 2
Total for .//xml: 3
The first XML contains three nodes, the root under XML and two children to root under XML. So the count of .//xml on documentElement produces two results.
The second contains the same as above, but wrapped in <root>. Root is now the documentElement, so the context can now count all children of documentRoot which is one XML with two nested XML tags. That count is therefore 3.
EDIT: Lamped - the return value of DOMXPath::query is a DOMNodeList, which you can iterate over in a loop.
You know, I've spent too long relying on xdebug, that a non-inspectable resource like DOMNodeList tends to throw me.
Being stubborn as I am, rather than correct myself, I think I'll contact Derek Rethans and ActiveState to see if I can get an automated solution to this sort of thing.
Yes, I understand now. I've never before tried to extract the context node itself with a query, so it never occurred to me that my thinking was wrong!! Thanks.
Yes, I understand now. I've never before tried to extract the context node itself with a query, so it never occurred to me that my thinking was wrong!! Thanks.
The thinking isn't wrong, its just where you think you are that's wrong. Easiest way I can think of this is to completely ignore the documentElement if you are working relative, and forget that its there. Absolute is fine, seeking //xml for example will find all three in that first block, but using .//xml will only find the two children (since the first XML is the parent of the document).