4

下面是一些需要处理的xml文件的摘录:

<BirimAdi>Adet</BirimAdi>
<BirimCarpan>1</BirimCarpan>
<HavaleFiyati>0</HavaleFiyati>
<HavaleFiyatiParaBirimi>TL</HavaleFiyatiParaBirimi>
<Price1>0</Price1>
<SatisFiyati1ParaBirimi>TL</SatisFiyati1ParaBirimi>
<Isk1>0</Isk1>
<SatisFiyati2>0</SatisFiyati2>

我需要做的是取标签之间的值并对其进行以下数学运算。

Price1 = round(Price1)-0.1;

该脚本应该对指定路径中的所有 xml 文件执行此操作。

我考虑过使用“sed”或“awk”,但我不确定这是否可以在 sed 中轻松完成。使用 xmllint 对我来说太过分了。有任何想法吗?我是这些实用程序的新手,所以不能指望找到我正在寻找的位的正则表达式是:

/<\s*Price1[^>]*>([^<]*)<\s*\/\s*Price1\s*>/
4

2 回答 2

7

我会使用XML解析器来完成这项工作。例如,XML::Twig。这里有一个例子:

#!/usr/bin/env perl

use warnings;
use strict;
use XML::Twig;

for my $f ( @ARGV ) {
        my $twig = XML::Twig->new(
                twig_handlers => {
                        'Price1' => sub { $_->set_text( sprintf( "%.1f", int( $_->text) - 0.1 ) ) },
                },
                pretty_print => 'indented',
        )->parsefile( $f )->print;
}

假设文件名是script.pl一个包含内容的测试文件xmlfile

<root>
<BirimAdi>Adet</BirimAdi>
<BirimCarpan>1</BirimCarpan>
<HavaleFiyati>0</HavaleFiyati>
<HavaleFiyatiParaBirimi>TL</HavaleFiyatiParaBirimi>
<Price1>3.3</Price1>
<SatisFiyati1ParaBirimi>TL</SatisFiyati1ParaBirimi>
<Isk1>0</Isk1>
<SatisFiyati2>0</SatisFiyati2>
</root>

像这样运行它:

perl script.pl xmlfile

这会产生:

<root>
  <BirimAdi>Adet</BirimAdi>
  <BirimCarpan>1</BirimCarpan>
  <HavaleFiyati>0</HavaleFiyati>
  <HavaleFiyatiParaBirimi>TL</HavaleFiyatiParaBirimi>
  <Price1>2.9</Price1>
  <SatisFiyati1ParaBirimi>TL</SatisFiyati1ParaBirimi>
  <Isk1>0</Isk1>
  <SatisFiyati2>0</SatisFiyati2>
</root>
于 2013-05-17T12:13:04.643 回答
1

一个快速肮脏的解决方案:

perl -pe 's!<(Price1)>(\d+(?:\.\d*)?)</\1>!"<$1>".(int($2+0.5)-0.1)."</$1>"!e'<<XXX
<HavaleFiyatiParaBirimi>TL</HavaleFiyatiParaBirimi>
<Price1>2.3</Price1>
<SatisFiyati1ParaBirimi>TL</SatisFiyati1ParaBirimi>
<Price1>2.5</Price1>
XXX

输出:

<HavaleFiyatiParaBirimi>TL</HavaleFiyatiParaBirimi>
<Price1>1.9</Price1>
<SatisFiyati1ParaBirimi>TL</SatisFiyati1ParaBirimi>
<Price1>2.9</Price1>

但到目前为止,Birei的解决方案更好......

于 2013-05-17T12:30:23.693 回答