XPath without using local-name() or name() functio

2020-03-31 02:32发布

问题:

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>

回答1:

Is it possible to parse oprResult without local-name?

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

//*[local-name()='oprResult']

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, say ex: for the http://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:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
                xmlns:ex="http://www.example.com">
   ...

Python (LibXML):

my $xc = XML::LibXML::XPathContext->new($xhtml_doc);
$xc->registerNs('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
$xc->registerNs('ex', 'http://www.example.com');

Javas (SAX):

NamespaceSupport support = new NamespaceSupport();
support.pushContext();
support.declarePrefix("soap", "http://schemas.xmlsoap.org/soap/envelope/");
support.declarePrefix("ex", "http://www.example.com");

Once you've declared namespace prefixes, your XPath can be:

/soap:Envelope/soap:Body/ex:ChangeResponse/oprResult/@code

* ...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