Reputation:
I have a root element with the following xmlns:
<Status xmlns="http://www.tandberg.com/XML/CUIL/2.0"
product="TANDBERG Codec" version="1.0.0">
It has no short name for the xmlns - yet I need to query the document for information using xpath. I am unable to edit the xml document itself.
I'm using a variety of parser and some are able to handle the xmlns without a short name and some are not.
The XPath below will give me the data I require - the question is - should xpath break in the above scenario by design?
//*[local-name()='Hardware']/*[local-name()='Temperature']
IE: Notepad++ will NOT handle it without adding xmlns:xs="..."
The following website DOES handle it and gives me what I expect.
http://www.xpathtester.com/xpath
Which is the correct implementation?
Upvotes: 1
Views: 1493
Reputation: 52888
The function local-name()
is in section 4.1 of the XPath 1.0 spec. Section 4 is the Core Function Library. Every function listed there is required to be included in all XPath implementations. If Notepad++ does not support it, it is a non-conformant processor.
Using local-name()
when there is no namespace prefix should not break any of your XPaths.
However, it would be a good idea to also use namespace-uri()
to match the exact node you're intending to match. It's very verbose, but also very accurate.
Example:
//*[local-name()='Hardware' and namespace-uri()='http://www.tandberg.com/XML/CUIL/2.0']/*[local-name()='Temperature' and namespace-uri()='http://www.tandberg.com/XML/CUIL/2.0']
Upvotes: 2
Reputation: 111726
By shortname you mean a namespace prefix.
XPath itself does not have a mechanism to bind namespace prefixes to namespaces. You have to rely on XPath's greater context (XSLT or other hosting language) to declare the connection between a namespace prefix and a namespace.
Your local-name()
approach should work in any conformant XPath implementation. It is often offered in answers to pure XPath questions which do not otherwise state the XPath context or library. Unfortunately, it also is offered by people who do not understand or otherwise unnecessarily shun namespaces.
Ideally, when the hosting context of XPath is known, namespace prefixes should be properly declared and used rather than dodged via local-name()
.
Upvotes: 3