XML::LibXML, namespaces and findvalue

后端 未结 2 1559
滥情空心
滥情空心 2020-12-10 18:15

I\'m using XML::LibXML to parse an XML document with a namespace. I therefore use XML::LibXML::XPathContext to findnodes using the XPath //u:model

2条回答
  •  隐瞒了意图╮
    2020-12-10 18:43

    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

提交回复
热议问题