在XML文件中,如何使用perl替换一个属性取决于某个其他属性的模式匹配?

时间:2015-10-23 08:47:03

标签: regex xml perl libxml2

我有类似这样的XML文件

    <staticResources><staticMMOResource language="eng" variant="default" version="5" id="../../../shared_ip/pex/pex_shared/images/compatible_config_header-type_1_non_cust.svg">
</staticMMOResource>
        </staticXMLResource><staticXMLResource language="eng" variant="default" version="9" id="../../../shared_ip/pex/pci_express_1/topics/pci_express_extended_configuration_space.xml">
    </staticXMLResource><staticMMOResource language="eng" variant="default" version="3" id="../../../shared_ip/pex/pci_express_1/images/b2ac21.svg></staticMMOResource></staticResources>

如果ID与特定的ID集匹配,我需要更新版本属性(我已将所有必需的ID存储在数组中)。 数组的ID类似

pci_express_1/topics/pci_express_extended_configuration_space.xml
pci_express_1/images/b2ac21.svg

我需要看起来像

的输出
<staticResources><staticMMOResource language="eng" variant="default" version="5" id="../../../shared_ip/pex/pex_shared/images/compatible_config_header-type_1_non_cust.svg">
    </staticMMOResource>
            </staticXMLResource><staticXMLResource language="eng" variant="default" version="11" id="../../../shared_ip/pex/pci_express_1/topics/pci_express_extended_configuration_space.xml">
        </staticXMLResource><staticMMOResource language="eng" variant="default" version="5" id="../../../shared_ip/pex/pci_express_1/images/b2ac21.svg></staticMMOResource></staticResources>

请帮忙!!我是Perl的新人。

1 个答案:

答案 0 :(得分:2)

你需要做的是这样的事情:

#!/usr/bin/env perl
use strict;
use warnings;

use XML::Twig;

my @ids = ( "../../../shared_ip/pex/pci_express_1/topics/pci_express_extended_configuration_space.xml" );

my $twig = XML::Twig -> parse ( \*DATA ); 
foreach my $id ( @ids ) { 
   foreach my $match ( $twig -> findnodes("staticXMLResource[\@id=\"$id\"]") ) {
       $match -> set_att('version', $match->att('version') + 1 );
   }
}
$twig -> print;

__DATA__
<XML>
<staticXMLResource language="eng" variant="default" version="9" id="../../../shared_ip/pex/pci_express_1/topics/pci_express_extended_configuration_space.xml">
</staticXMLResource>
</XML>
  • 我们加载您的XML。 (我使用DATA进行说明,因为我不得不模拟一些类似于你的XML。你可能不应该这样做。
  • 我们会迭代您的每个ID。
  • 我们会查找具有属性匹配该ID的元素。
  • 我们检索版本,递增版本,然后设置新值。

然后我们打印XML。

您可能会发现set_pretty_print对格式化非常有用。

E.g:

$twig -> set_pretty_print('indented_a');

编辑:鉴于您已经包含了一些数组内容 - 这些内容与您的属性并不完全匹配,因此您需要稍微改变一下:

#!/usr/bin/env perl
use strict;
use warnings;

use XML::Twig;

my @ids = qw ( pci_express_1/topics/pci_express_extended_configuration_space.xml 
        pci_express_1/images/b2ac21.svg );

#assemble a regex from the search elements. 
my $search = join ( "|", @ids ); 
   $search = qr/($search)/; 

#parse XML - you'll probably want "parsefile" or "parse" depending on your 
#XML source. 
my $twig = XML::Twig -> parse ( \*DATA ); 

#iterate the children of the root (staticResources) node 
# NB - this might not match your larger file. 
foreach my $resource ( $twig -> root -> children ) {
   #test if the 'id' attribute matches our regex. 
   #note - regex is unanchored, so substring matches work. 
   if ( $resource -> att('id') =~ m/$search/ ) { 
      #increment version id. 
      $resource -> set_att('version', $resource->att('version') + 1 ); 
   }
}
#set output format
$twig -> set_pretty_print('indented_a'); 
#print to stdout. 
#To print to a file, you may want print {$opened_fh} $twig -> sprint; 
$twig -> print; 

__DATA__
<staticResources>
<staticXMLResource language="eng" variant="default" version="9" id="../../../shared_ip/pex/pci_express_1/topics/pci_express_extended_configuration_space.xml">
    </staticXMLResource>
    <staticXMLResource language="eng" variant="default" version="9" id="../../../shared_ip/pex/pci_express_1/topics/pci_express_extended_configuration_space.xml">
</staticXMLResource>
<staticMMOResource language="eng" variant="default" version="3" id="../../../shared_ip/pex/pci_express_1/images/b2ac21.svg"></staticMMOResource>
</staticResources>

我们根据您拥有的模式构建搜索正则表达式$search。然后我们迭代child的{​​{1}}个节点(如果您的XML更大,那么您可能还需要使用root)。