我有像这样的xml数据
<ce:refers type="book">DHO</ce:refers>
<ce:refers type="journal" num="ref0005">RTO</ce:refers>
<ce:refers type="book">DHO</ce:refers>
<ce:refers type="journal">RHO</ce:refers>
<ce:refers type="book">DHO</ce:refers>
<ce:refers type="book" num="ref0010">EHO</ce:refers>
现在我想查找没有num属性的标签并生成num值,num值增加5并且需要更改num而不影响退出num并且不允许相同的num值。
我尝试使用正则表达式,但我不能这样做。我期待像这样的输出
<ce:refers type="book" num="ref0015">DHO</ce:refers>
<ce:refers type="journal" num="ref0005">RTO</ce:refers>
<ce:refers type="book" num="ref0025">DHO</ce:refers>
<ce:refers type="journal" num="ref0030">RHO</ce:refers>
<ce:refers type="book" num="ref0035">DHO</ce:refers>
<ce:refers type="book" num="ref0010">EHO</ce:refers>
答案 0 :(得分:0)
使用xsh,XML::LibXML的包装:
open data.xml ;
my $s := sort :k @num //ce:refers/@num ;
my $last = { $s->[-1] } ;
my $maxnum = xsh:match($last, '(\d+)') ;
for //ce:refers[not(count(@num))] {
$maxnum += 5 ;
set @num xsh:sprintf('ref%04d', $maxnum) ;
}
save :b ;
请注意,输出与样本略有不同(样本中缺少ref0005
)。