gpt4 book ai didi

xml - 如何在perl中使用xpath分割xml?

转载 作者:行者123 更新时间:2023-12-02 22:24:01 25 4
gpt4 key购买 nike

我有一个输入 xml,我必须根据 doc 和 delt 进行拆分,并将其保存为这种格式 delt_0001.xml

这是我的代码

    #!/usr/bin/perl
use XML::XPath;

my $file = 'file.xml';

my $xp = XML::XPath->new(filename=>$file);

foreach my $entry ( $xp->findnodes('/xml/service/main/doc') ) {
my $filename = $entry->findvalue('./delt/@id');
foreach my $entry1( $entry->findnodes('//delt')){

my $filename = $entry1->findvalue('/delt/@id');
my $content = $entry1->toString;
open(wr,">delt_$filename.xml");
print wr "$content\n";
close wr;

}

当我运行该程序时,所有 delt 部分都会打印在一个 xml 中。

输入xmldelt.xml

  <xml>
<service>
<title>split xml</title>
<main>
<doc id="001">
<title>doc1</title>
<delt id="0001">
<title>delt1</title>
<text>num1</text>``
<text>num1</text>
</delt>
<delt id="0002-A">
<title>delt1</title>
<text>num1</text>
<text>num1</text>
</delt>
</doc>
<doc id="002">
<title>doc2</title>
<delt id="0003">
<title>delt1</title>
<text>num1</text>
<text>num1</text>
</delt>
<delt id="0004">
<title>delt1</title>
<text>num1</text>
<text>num1</text>
</delt>
</doc>
</main>
</service>
</xml>

输出正在获取

         <delt id="0001">
<title>delt1</title>
<text>num1</text>``
<text>num1</text>
</delt>
<delt id="0002-A">
<title>delt1</title>
<text>num1</text>
<text>num1</text>
</delt>
<delt id="0003">
<title>delt1</title>
<text>num1</text>
<text>num1</text>
</delt>
<delt id="0004">
<title>delt1</title>
<text>num1</text>
<text>num1</text>
</delt>

需要输出

分割1号delt_0001.xml

<xml>
<service>
<title>split xml</title>
<main>
<doc id=001>
<title>doc1</title>
<delt id=0001>
<title>delt1</title>
<text>num1</text>``
<text>num1</text>
</delt>
</doc>
</main>
</service>
</xml>

拆分 2 个 delt_0002-A.xml

<xml>
<service>
<title>split xml</title>
<main>
<doc id=001>
<title>doc1</title>
<delt id=0002=A>
<title>delt1</title>
<text>num1</text>
<text>num1</text>
</delt>
</doc>
</main>
</service>
</xml>

拆分 3 个 delt_0003.xml

<xml>
<service>
<title>split xml</title>
<main>
<doc id=002>
<title>doc2</title>
<delt id=0003>
<title>delt1</title>
<text>num1</text>
<text>num1</text>
</delt>
</doc>
</main>
</service>
</xml>

分割4号delt_0004.xml

<xml>
<service>
<title>split xml</title>
<main>
<doc id=002>
<title>doc2</title>
<delt id=0004>
<title>delt1</title>
<text>num1</text>
<text>num1</text>
<delt>
</doc>
</main>
</service>
</xml>

提前致谢

最佳答案

使用 XML::Twig 来做到这一点相当简单(我很高兴我不久前得到了“在解析期间删除当前元素”的功能):

#!/usr/bin/perl

use strict;
use warnings;

use XML::Twig;

my $delt= 'delt.xml';

XML::Twig->new( twig_handlers => { delt => \&delt },
pretty_print => 'indented',
)
->parsefile( $delt);

exit;

sub delt
{ my( $t, $delt)= @_;

my $delt_file= sprintf( 'delt_%s.xml', $delt->id);

# the only tricky part: remove previous doc if needed
if( my $prev_doc= $delt->parent( 'doc')->prev_sibling( 'doc'))
{ $prev_doc->delete; }

$t->print_to_file( $delt_file);

$delt->delete;
}

关于xml - 如何在perl中使用xpath分割xml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27019775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com