perl script to iterate over xml nodes using XML::LibXML

倾然丶 夕夏残阳落幕 提交于 2019-12-04 10:27:25
ikegami

The XML doesn't contain any element named WO in the null namespace. You want to match the elements named WO in the http://www.example.com/yyyy namespace.

#!/usr/bin/perl

use strict;
use warnings;

use XML::LibXML               qw( );
use XML::LibXML::XPathContext qw( );

my $file = 'spec.xml';

my $parser = XML::LibXML->new();
my $doc = $parser->parse_file($file);
my $root = $doc->getDocumentElement;

my $xpc = XML::LibXML::XPathContext->new($doc);
$xpc->registerNs(y => 'http://www.example.com/yyyy');

for my $atrid ( $xpc->findnodes('y:WO/y:WOSet/y:SR/y:SPEC') ) {
    my $name  = $xpc->findvalue('y:ATTRID', $atrid);
    my $value = $xpc->findvalue('y:AVALUE', $atrid);
    print "$name = $value\n";
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!