Re: Root[1] vs Root (2)
Brought to you by:
bs_php,
nigelswinson
From: Nigel S. <nig...@us...> - 2002-08-06 20:04:25
|
> > 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 |