Home > Software engineering >  namespace: Proper xpath for libxml2
namespace: Proper xpath for libxml2

Time:10-19

I am trying to use xpath1, on the following SOAP XML file:

I tried a simple one:

% ./xpath1 SR_ADULT_007.ExamCard "//SOAP-ENC:Array[@id='ref-297']"
XPath error : Undefined namespace prefix
Error: unable to evaluate xpath expression "//SOAP-ENC:Array[@id='ref-297']"
Usage: ./xpath1 <xml-file> <xpath-expr> [<known-ns-list>]
where <known-ns-list> is a list of known namespaces
in "<prefix1>=<href1> <prefix2>=href2> ..." format

However my XPath perl command seems to contradict the above:

% xpath -e "//SOAP-ENC:Array[@id='ref-297']" SR_ADULT_007.ExamCard > /dev/null
Found 1 nodes in SR_ADULT_007.ExamCard:
-- NODE --

What did I misunderstood from libxml2/xpath/namespace convention ?

CodePudding user response:

It may be that the Perl module you are using takes the set of namespace declarations from the root element of the source document.

In general though, with most XPath software I've seen, when you are executing an XPath that uses namespace prefixes, it's necessary to declare the bindings between those namespace prefixes and the corresponding namespace URIs.

CodePudding user response:

The correct syntax for xpath1 example is simply (thanks Conal!):

% ./xpath1 SR_ADULT_007.ExamCard "//SOAP-ENC:Array[@id='ref-297']" SOAP-ENC=http://schemas.xmlsoap.org/soap/encoding/
Result (1 nodes):
= element node "http://schemas.xmlsoap.org/soap/encoding/:Array"
  • Related