XML::LibXML, namespaces and findvalue

有些话、适合烂在心里 提交于 2019-11-28 11:32:57

You can't use $node->findvalue() because of the whole default namespace thing. However, you can reuse your XML::LibXML::XPathContext object to find the values you want:

for my $node ( $context->findnodes('//u:model') ) {
   my $mh   = $context->findvalue('u:mh', $node);
   my $attr = $context->findvalue('u:attribute', $node);
   print "mh = $mh, attr = $attr\n";
}

XPath allows ignoring namespaces by using the function local-name:

use XML::LibXML;

my $dom = XML::LibXML->load_xml( IO => \*DATA );

for my $node ( $dom->findnodes('//*[local-name()="model"]') ) {
    my $mh   = $node->findvalue('*[local-name()="mh"]');
    my $attr = $node->findvalue('*[local-name()="attribute"]');

    print "mh = $mh, attr = $attr\n";
}

This removes the need to specify an context for a single namespace document like in the question.

Reference: Re^2: XML::LibXML and namespaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!