DIfference between Select all href attributes and Select all anchor elements with href attributes using xpath with php
Given that in a html document all anchor elements have a href attribute that is set:
What is the difference between the following xpath queries
Case 1:
//@href
Case 2:
//a/@href
Case 3:
*/a/@href
In my situation:
//@href
returns all the da开发者_高级运维ta contained in the href attribute.
//a/@href
returns nothing but I expect it to return the same as //@href
*/a/@href
returns nothing but I expect it to return the same as //@href
and //a/@href
I fear that I am grossly misunderstanding how these queries work. Is there anybody that can set me straight. Thank you in advance for your assistance.
What is the difference between the following xpath queries
Case 1:
//@href
This selects all href
attribute nodes in the XML document.
Case 2:
//a/@href
This selects all href
attributes belonging to any element named a
that is in no namespace.
Case 3:
*/a/@href
This selects all href
attributes belonging any element named a
that is in no namespace and that is a grandchild of the current (context) node.
Most probably you wanted to write:
//*/a/@href
This selects all href
attributes belonging any element named a
that is in no namespace and whose parent is an element.
In my situation:
//@href
returns all the data contained in thehref
attribute.
//a/@href
returns nothing but I expect it to return the same as//@href
*/a/@href
returns nothing but I expect it to return the same as//@href
and//a/@href
I fear that I am grossly misunderstanding how these queries work. Is there anybody that can set me straight. Thank you in advance for your assistance.
A very frequent situation is that the XML document has a default namespace. This is most probably the cause of your problem. In this case no a
element that is in "no namespace" exists in the document and any XPath expression having a
as a location step selects nothing.
Apart from this, the three expressions are not equivalent.
//@href
and //a/@href
could select different sets of nodes if in the document there are other elements except a
that have an href
attribute, or if the document is in a default namespace. In the last case the second expression selects nothing.
//a/@href
and //*/a/@href
could select different sets of nodes if the top element of the document is an a
that has an href
attribute. The href
attribute of this top element is selected by the first XPath expression but is not selected by the second, because the parent of the top element is not an element (it is the root node /
).
You haven't shown the source document. But I'm prepared to bet you've made the #1 XSLT mistake, which is to forget or fail to notice that it declares a default namespace, which means an unprefixed name like //a will not select any elements.
If you want to select a node that has an attribute, use this syntax: //a[@href]
. I'm not entirely sure why the other method doesn't work, as it makes sense in principle. Just how XPath is I guess.
精彩评论