首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Perl:遍历xml父节点,查找一个子节点的特定值,并比较该父节点下其他子节点的值

Perl:遍历xml父节点,查找一个子节点的特定值,并比较该父节点下其他子节点的值
EN

Stack Overflow用户
提问于 2017-06-02 19:15:30
回答 1查看 65关注 0票数 0

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

代码语言:javascript
复制
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文档

代码语言:javascript
复制
    <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卡。基于这些,我需要查找该接口中的其他节点,并比较值以进行验证

我收到的错误是

代码语言:javascript
复制
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.
EN

回答 1

Stack Overflow用户

发布于 2017-06-02 19:19:25

你的问题是嵌套双引号。您可以对它们进行转义或使用反斜杠:

代码语言:javascript
复制
my $device_name = $doc->findnodes("//networking/interfaces[\@config:type=\"list\"]/interface[$iterator]/device");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44327584

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档