1

I have to parse following XML:

<reply xmlns="urn::ietf::param" xmlns:element="https://xml.example.net/abc/12.3"
message-id='1'>
<abc>
<xyz> hello </xyz>
</abc>
</reply>

I want the value of xyz node i.e. hello, but findnodes is returning null value. my code is :

 my $xpath=XML::LibXML::XPathContext->new($dom);
    $xpath->registerNs('ns1','urn::ietf::param'); 
    $xpath->registerNs('ns2','https://xml.example.net/abc/12.3');
    $val=$xpath->findnodes('//ns1:reply/ns2:element/abc/xyz'); 
    print $val;

but print statement is returning null value

0

1 Answer 1

2

It looks like you have some misunderstanding about how namespaces work. xmlns:element="https://xml.example.net/abc/12.3" means that there is a prefix element defined with this specific namespace URI. This namespace is actually never used in your XML.

xmlns="urn::ietf::param" defines a default namespaces and also applies to all descendant elements. And actually you have no <element/> element in your XML.

Thus, the following XPath should work as expected:

//ns1:reply/ns1:abc/ns1:xyz
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.