我是新手XML::Twig
。
我需要将子标签移动到子标签。
我怎样才能做到这一点?
当我notes
将父标签与子标签匹配到之前的部分时。
我的 XML 如下所示:
<book>
<sec>
<p>The indicated something</p>
<p>The something</p>
</sec>
<sec>
<notes>note</notes>
<p>text</p>
</sec>
<sec>
<p>The indicated</p>
<p>The something</p>
</sec>
<sec>
<notes>note</notes>
<p>text1</p>
</sec>
</book>
我试过:
use XML::Twig;
open(my $output, ">output.xml") || die "can't open the output.xml$!\n";
my $story_file = XML::Twig->new(
keep_encoding => 1,
twig_handlers => { 'book' => \&book, },
pretty_print => 'indented',
);
$story_file->parse("sample.xml");
$story_file->print($output);
$story_file->purge;
sub book {
my ($stroy_file, $book) = @_;
my @sub_elmt = $book->children;
Get_children(\@sub_elmt) if ($#sub_elmt >= 0);
}
sub Get_children {
my ($ref) = @_;
foreach my $tagg (@$ref) {
my @children = $tagg->children;
my $tagName = $tagg->name;
if ($tagName =~ /^sec$/) {
my $f = $tagg->first_child;
if ($f->name =~ /^notes$/) {
$tagg->move('last_child', $tagg);
}
}
Get_children(\@children) if ($#children >= 0);
}
}
它不能工作,我该怎么做?
我需要这样的输出:
<book>
<sec>
<p>The indicated something</p>
<p>The something</p>
<sec>
<notes>note</notes>
<p>text</p>
</sec>
</sec>
<sec>
<p>The indicated</p>
<p>The something</p>
<sec>
<notes>note</notes>
<p>text1</p>
</sec>
</sec>
</book>
我该怎么做?