With this Xpath:
//*[@data-auto='citation_field_value'][@xmlns:extendedmarkupcontroller='urn:ExtendedMarkupController']
I was hoping to retrieve this element:
<dd data-auto="citation_field_value" xmlns:extendedmarkupcontroller="urn:ExtendedMarkupController">Freeman, Michael K., author</dd>
However, nothing was returned with:
$xpatho = new DOMXpath($doc);
$elementsn = $xpatho->query($xpath);
$elementsn->length gave me 0.
Why is this so? Is it because of the colon? and how can I solve it?
The ':' means that your document is using namespaces, and namespaces change the world.
For starters, a namespace declaration in the source XML (like xmlns:extendedmarkupcontroller="urn:ExtendedMarkupController"
) does not become an attribute node in the XDM data model, so you cannot access it using the attribute axis (or the '@' shorthand for the attribute axis). If you want to find an element whose name is in this namespace, then the correct predicate to use is
[namespace-uri()='urn:ExtendedMarkupController']
In your example, though, the element declares the namespace, but it doesn't actually use the namespace. Testing whether an element declares a particular namespace, when that namespace isn't actually used, is a rather unusual requirement. You can do it with the namespace axis:
[namespace::extendedmarkupcontroller="urn:ExtendedMarkupController"]
though some XPath processors don't fully implement the spec in this area.
This should be enough for this particular problem, but if you're starting to work with namespaces I would recommend reading around the subject before you write any more code, because there are lots of pitfalls.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.