Perl:通过xml父节点迭代一个子节点的特定值,并比较该父节点下的其他子节点的值

时间:2017-06-02 11:15:30

标签: xml perl iterator

我有一个autoyast文件(SLES配置)。它有多个债券和网卡的条目。我需要找到它的类型bond或NIC,然后需要迭代其余的参数来验证它们是否正确。

use strict;
use XML::LibXML;

my $parser = XML::LibXML->new;
my $doc = $parser->parse_file("setuplan_1.xml");

my $count_interface = $doc->findvalue("count(//interface)");
print "total interface $count_interface\n";
for ( my $iterator = 1; $iterator < $count_interface; $iterator++){
print "Iterator value $iterator";
my $device_name = $doc->findnodes("//networking/interfaces[\@config:type="list"]/interface[$iterator]/device");
if ( $device_name =~ m/bond(\d+)/){
print $device_name;
}
elsif ( $device_name =~ m/(p(\d+)p(\d+)|em(\d+))/){
print "alom device";
}
}

autoyast的示例XML文档

    <networking>
    .........
    <interfaces config:type="list">
    <interface>
    <bonding_master>yes</bonding_master>
    <bonding_module_opts>xxxxx</bonding_module_opts>
    <bonding_slave0>emxx</bonding_slave0>
    <bonding_slave1>pxpy</bonding_slave1>
    <bootproto>static</bootproto>
    <device>bondx</device>
    <ipaddr>x.x.x.x</ipaddr>
    <name>Management/Quorum</name>
    <netmask>255.255.255.0</netmask>
    <startmode>auto</startmode>
    <usercontrol>no</usercontrol>
    </interface>
    <interface>
    <bonding_master>yes</bonding_master>
    <bonding_module_opts>xxxxxxxx</bonding_module_opts>
    <bonding_slave0>pxpy</bonding_slave0>
                <bonding_slave1>pxpy</bonding_slave1>
                <bootproto>static</bootproto>
                <device>bondx</device>
                <ipaddr>x.x.x.x</ipaddr>
                <name>xxxx</name>
                <netmask>255.255.255.0</netmask>
                <startmode>auto</startmode>
                <usercontrol>no</usercontrol>
         </interface>
             ..........
       <interface>
                <bootproto>none</bootproto>
                <device>pxpy</device>
                <name>This adapter is part of BOND and is disabled</name>
                <startmode>hotplug</startmode>
                <usercontrol>NO</usercontrol>
            </interface>
            <interface>
                <bootproto>none</bootproto>
                <device>pxpy</device>
                <name>This adapter is part of BOND and is disabled</name>
                <startmode>hotplug</startmode>
                <usercontrol>NO</usercontrol>
            </interface>
        .........
   </networking>

在此XML文件中,有多个接口标记,每个标记用于绑定设备和NIC卡。基于这些,我需要在该接口中查找其他节点并比较值以验证

我收到的错误是

Bareword found where operator expected at autoyastcheck.pl line 13, near ""//networking/interfaces[\@config:type="list"
    (Missing operator before list?)
String found where operator expected at autoyastcheck.pl line 13, near "list"]/interface[$iterator]/device""
syntax error at autoyastcheck.pl line 13, near ""//networking/interfaces[\@config:type="list"
Execution of autoyastcheck.pl aborted due to compilation errors.

1 个答案:

答案 0 :(得分:0)

你的问题是nestes双引号。你可以逃避它们或使用反斜杠:

my $device_name = $doc->findnodes("//networking/interfaces[\@config:type=\"list\"]/interface[$iterator]/device");