2017-06-02 81 views
0

我有一個autoyast文件(SLES配置)。它有多個債券和網卡的入口。我需要找到它是鍵入綁定還是NIC,然後需要遍歷其餘參數以驗證它們是否正確。Perl:通過xml父節點遍歷一個子節點的特定值並比較該父節點下其他子節點的值

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

<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文件中的示例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. 
+0

請格式化你的代碼properbly – Jens

回答

0

您的問題是nestes雙引號。你可以逃脫他們或用反斜槓:

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

由於錯過了....... – rahulruns

+0

@rahulruns歡迎您 – Jens

+1

或者用勁兒引號的屬性值,XPath的同時接受。 – choroba

相關問題