space_monk has asked for the wisdom of the Perl Monks concerning the following question:
I am in receipt of an xml message. What is the best way to retrieve the xmlns (namespace) attribute from the root element of that message using XML::LibXML?? If it helps, I am expecting GovTalkMessages of the format:
For bonus points, please tell me if there is any way of avoiding using XPathContext and adding the namespace identifier to all xpath queries on the resulting DOM. e.g. do I really have to do:<?xml version="1.0"?> <GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope"> <EnvelopeVersion>2.0</EnvelopeVersion> <Header> <MessageDetails> ..... </MessageDetails> </Header> <GovTalkDetails> ..... </GovTalkDetails> <Body> <!-- A valid Body payload with a namespace declaration on the first el +ement --> </Body> </GovTalkMessage>
my $dom = XML::LibXML->load_xml( string => $xml); my $xc = XML::LibXML::XPathContext->new($dom); $xc->registerNs( 'gt', 'http://www.govtalk.co.uk/CM/envelope'); $envelopeVersion = $xc->findvalue( '//gt:EnvelopeVersion');
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: LibXML, XPath and Namespaces
by choroba (Cardinal) on Mar 21, 2013 at 15:27 UTC | |
by space_monk (Chaplain) on Mar 21, 2013 at 15:46 UTC | |
|
Re: LibXML, XPath and Namespaces
by tobyink (Canon) on Mar 21, 2013 at 21:32 UTC | |
by space_monk (Chaplain) on Mar 22, 2013 at 09:28 UTC | |
by tobyink (Canon) on Mar 22, 2013 at 16:46 UTC | |
by space_monk (Chaplain) on Mar 25, 2013 at 10:07 UTC | |
by ikegami (Patriarch) on Mar 24, 2013 at 07:12 UTC | |
by space_monk (Chaplain) on Mar 25, 2013 at 10:02 UTC | |
by ikegami (Patriarch) on Apr 08, 2013 at 03:30 UTC | |
|
Re: LibXML, XPath and Namespaces (name(), local-name())
by Anonymous Monk on Mar 21, 2013 at 22:23 UTC | |
by space_monk (Chaplain) on Mar 22, 2013 at 09:32 UTC |