Perl 如何插入子xml细枝
我需要在child元素中插入一个子元素。我有两个孩子,第一个孩子剪切粘贴到第二个孩子插入作为第一个孩子 xml: 我无法处理它。如何将标签剪切并将标签标签粘贴到p标签作为第一个子项 我需要:Perl 如何插入子xml细枝,perl,xml-twig,Perl,Xml Twig,我需要在child元素中插入一个子元素。我有两个孩子,第一个孩子剪切粘贴到第二个孩子插入作为第一个孩子 xml: 我无法处理它。如何将标签剪切并将标签标签粘贴到p标签作为第一个子项 我需要: <fn id="fn1_1"> <p><label>1</label> The distinguished as &#x2018;bisexuation.&#x2019;</p> </fn> 1杰
<fn id="fn1_1">
<p><label>1</label> The distinguished as &#x2018;bisexuation.&#x2019;</p>
</fn>
1杰出的as&#x2018;"二分法",#x2019
您的代码有几个问题:首先,处理程序应该应用于fn
,而不是fngroup
,然后您测试的是$fn=~/p/
,而不是$fn->name=~/p/
因此,这将起作用:
#!/usr/bin/perl
use strict;
use warnings;
use XML::Twig;
XML::Twig->new( twig_handlers => { fn => \&fn})
->parse( \*DATA)
->print;
sub fn {
my ($xml_twig_content, $fn) = @_;
my @text = $fn->children;
my $cut;
foreach my $fn (@text){
$cut = $fn->cut if ($fn->name =~ /label/);
if ($fn->name =~ /p/){
$cut->paste(first_child => $fn);
}
}
}
__DATA__
<foo>
<fngroup>
<fn id="fn1_1">
<label>1</label>
<p>The distinguished as &#x2018;bisexuation.&#x2019;</p>
</fn>
</fngroup>
</foo>
<fn id="fn1_1">
<p><label>1</label> The distinguished as &#x2018;bisexuation.&#x2019;</p>
</fn>
#!/usr/bin/perl
use strict;
use warnings;
use XML::Twig;
XML::Twig->new( twig_handlers => { fn => \&fn})
->parse( \*DATA)
->print;
sub fn {
my ($xml_twig_content, $fn) = @_;
my @text = $fn->children;
my $cut;
foreach my $fn (@text){
$cut = $fn->cut if ($fn->name =~ /label/);
if ($fn->name =~ /p/){
$cut->paste(first_child => $fn);
}
}
}
__DATA__
<foo>
<fngroup>
<fn id="fn1_1">
<label>1</label>
<p>The distinguished as &#x2018;bisexuation.&#x2019;</p>
</fn>
</fngroup>
</foo>
sub fn {
my ($twig, $fn) = @_;
$fn->first_child( 'label')->move( first_child => $fn->first_child( 'p'));
}