신입생 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>
:
은이 같은 출력을해야합니까?
구조를 보여 제대로 프로그램을 들여하십시오. 그대로 읽을 수 없습니다. – Borodin