Search code examples
xmlperlxml-libxml

get all namespaces in scope with XML::LibXML


I need to get all of the namespaces in the scope of an XML::LibXML::Node. The module has a getNamespaces() method, but the documentation explicitly says that it

will not return all namespaces that are in scope, but only the ones declared explicitly for that node.

So how do I get all of the namespaces in the scope of a particular node?


Solution

  • I didn't find any functions to do this, but it is certainly possible with XPath:

    @nodes = $node->findnodes('namespace::*');
    

    That returns all of the namespaces in a scope visible to $node. You can then get prefixes and URIs from the returned XML::LibXML::Node::Namespace objects returned:

    @prefix_uris = map {[ $_->getLocalName, $_->getData]} @nodes;