Re: Root[1] vs Root (2)
Brought to you by:
bs_php,
nigelswinson
From: Branko N. <bra...@in...> - 2002-08-06 22:11:43
|
Thank you for nice explanation, Nigel. I'm also very thankful fur code modification. Unfortunately my test shows error. After downloading the latest XPath from CVS, I tried to use $set = $x1->match("//document", "/root/folder[3]"); but results have been from whole document, not only from folder[3], with php error *"Notice*: Undefined offset: 1 in *XPath.class.php* on line *2126"*. Cheers, Branko Nigel Swinson wrote: >>>I've noticed that root absolute path need to be numbered as root[1]: >>> $set = $x1->match("//document", "/root[1]/folder[3]"); >>> >>>In case without [1] after root >>> $set = $x1->match("//document", "/root/folder[3]"); >>>have got error: >>>*XPath error in XPath.class.php:2121* The supplied xPath '//document' >>>does not *uniquely* describe a node in the xml document. >>> >>>Seems strange to me, as root is always the only one. >>> >>> > >Wherever the class takes an xPathQuery, you can miss out the [] if there is >only one relevant node, but where the class takes an absoluteXPath, that >particular argument isn't "pre evaluated" so you have to provide an >absoluteXPath. "/root" isn't a node in the document, but "/root[1]" is. >"/root" is an XPath expression (an xPathQuery) that evaluates to precisely >one node it so happens, so what we can do is to evaluate() on the /root, >then check that it returns only one node. > >Your code will be faster if you supply the [] though, as it means that the >class does not needlessly have to evaluate the /root XPath expression only >to find out that there is only one root node. > >Never-the-less to make the class easier to use, I've upgraded the second >parameter of match() for you to an xPathQuery rathar than an absoluteXPath, >and demand that the xPathQuery evaluate to a single node. > >Cheers > >Nigel > > > |