XML::LibXML issue finding XML nodes with a namespace

后端 未结 2 877
北荒
北荒 2021-01-21 20:41

I am writing an XML parser and am having an issue with the program handling a link. I am attempting to parse an XML hierarchy Settings/Setting then findnode \'Value\'. The follo

相关标签:
2条回答
  • 2021-01-21 21:04

    I was able to get this working using this code.

    my $dom = XML::LibXML->load_xml(location => $filename);
    
    my $xpc = XML::LibXML::XPathContext->new($dom);
    $xpc->registerNs('xsd',  'http://hme.com/Settings.xsd');
    
    my($match1) = $xpc->findnodes('//xsd:Settings/xsd:Setting[@SID="8"]/xsd:Value');
    $match1->removeChildNodes();
    $match1->appendText('23400');
    
    0 讨论(0)
  • 2021-01-21 21:22

    You ask to find nodes with namespace null and with name Settings. There are no such nodes in the document, so findnodes correctly returns nothing.

    You want to find the nodes with namespace http://hme.com/Settings.xsd and with name Settings. You can use the following to achieve that:

    my $xpc = XML::LibXML::XPathContext->new();
    $xpc->registerNs( s => 'http://hme.com/Settings.xsd' );
    
    for ($xpc->findnodes('/s:Settings/s:Setting[@SID="8"]'), $doc) {
       ...
    }
    
    0 讨论(0)
提交回复
热议问题