簡體   English   中英

Perl:在xml父節點中迭代一個子節點的特定值,並比較該父節點下其他子節點的值

[英]Perl: Iterating through an xml parent node for a particular value of one child node and comparing values for other child nodes under that parent node

我有一個autoyast文件(SLES配置)。 它具有用於多個綁定和網卡的條目。 我需要找到每種類型的綁定或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.

您的問題是嵌套雙引號。 您可以轉義它們,也可以使用反斜杠:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM