Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 如何插入子xml细枝_Perl_Xml Twig - Fatal编程技术网

Perl 如何插入子xml细枝

Perl 如何插入子xml细枝,perl,xml-twig,Perl,Xml Twig,我需要在child元素中插入一个子元素。我有两个孩子,第一个孩子剪切粘贴到第二个孩子插入作为第一个孩子 xml: 我无法处理它。如何将标签剪切并将标签标签粘贴到p标签作为第一个子项 我需要: <fn id="fn1_1"> <p><label>1</label> The distinguished as &amp;#x2018;bisexuation.&amp;#x2019;</p> </fn> 1杰

我需要在child元素中插入一个子元素。我有两个孩子,第一个孩子剪切粘贴到第二个孩子插入作为第一个孩子

xml:

我无法处理它。如何将标签剪切并将标签标签粘贴到p标签作为第一个子项

我需要:

<fn id="fn1_1">
 <p><label>1</label> The distinguished as &amp;#x2018;bisexuation.&amp;#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 &amp;#x2018;bisexuation.&amp;#x2019;</p>
    </fn>
  </fngroup>
</foo>
<fn id="fn1_1">
 <p><label>1</label> The distinguished as &amp;#x2018;bisexuation.&amp;#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 &amp;#x2018;bisexuation.&amp;#x2019;</p>
    </fn>
  </fngroup>
</foo>
sub fn {
    my ($twig, $fn) = @_;
    $fn->first_child( 'label')->move( first_child => $fn->first_child( 'p'));
}