I have a problem parsing Xml from a third party; there are several different versions of the xml which they send, with overlapping namespaces;
Version 1
foo="urn:bar.org/version-1"
far="urn:gle.org/version-1"
Version 2
foo="bar:a.org/version-2"
far="gle:a.org/version-2"
Previously (when I was only had to handle one version) I'd register the namespaces from a hardcoded hash like so:
#!/usr/bin/perl
use strict;
use XML::LibXML ;
my $cfg->{namespace} = {
foo=>"urn:bar.org/version-1",
far=>"urn:gle.org/version-1",
};
my $parser = XML::LibXML->new({recover => '1'});
my $doc = $parser->parse_string($inputHash->{$key}->{xml});
my $xc = XML::LibXML::XPathContext->new( $doc->documentElement() );
for my $ns (keys %{$cfg->{namespace}})
{
$xc->registerNs($ns => $cfg->{namespace}->{$ns});
}
obviously this will only work on version 1..
Have scoured the LibXML documentation but can't see a way to extract the namespaces used in a doc and register them; could anyone give me a pointer please?
Pseudo doc:
<?xml version="1.0"?>
<foo:Parent xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:foo="bar:a.org/version-2">
<far:Child xmlns:gle="gle:a.org/version-2">
{horrific structure with more ns declarations all of which need registering}
</far:Child>
</foo:Parent>