I have to parse oprResult/@code
from below XML using XPath.
The XPath expression
//*[local-name()='oprResult']/@code
is working as expected, BUT
I could not use name
or local-name
functions as '('
')'
are used as delimiter in my parsing function.
Is it possible to parse oprResult
without local-name
?
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ChangeResponse xmlns="http://www.example.com" code="0" message="Success">
<oprResult code="0" message="Success"/>
</ChangeResponse>
</soap:Body>
</soap:Envelope>
Yes*
Not only is it possible to select an XML element without using
local-name()
, it is actually preferred.One common reason you see example XPaths with constructs such as
is that namespace declarations are not an intrinsic part of XPath; namespace and namespace prefix declarations occur in the greater context of the system or language using an XPath library.
If you cannot make your system tolerate the parentheses of
local-name()
, if//oprResult/@code
doesn't suite, or if you are simply interested in using namespaces properly rather than defeating them, figure out how to declare a namespace prefix, sayex:
for thehttp://www.example.com
namespace, and use the namespace prefix in the XPath. Read on for how...You do not say what the language and/or library you're using for XPath, but here are a few examples to give a feel for what to look for...
XSLT:
Python (LibXML):
Javas (SAX):
Once you've declared namespace prefixes, your XPath can be:
* ...but the reason should not be to avoid parentheses because they "are used as delimiters in a parsing function." First of all, if an XPath parser is having trouble with embedded parens, get a new XPath parser; if a tool that uses an XPath library is preventing you from using parens, get a new tool (or investigate deeper to be sure the tool is not being used as well as it can be). Secondly, as @predi observed in the question comments, if there's only a single
oprResult
element and your response is small enough that you don't mind the inefficiency, in your case you can use an XPath without namespaces://oprResult/@code