문제

I need to display the "parent" from the first Element of input.xml

Also I must display the id from the parent

Here is my script Perl: C:\lng> C:\lng\uniq.pl

The output is displaying unfortunately the children nodes:

 <source id="berlin"> 
   <target id="karlsruhe" method="delete"/> 
   <target id="Stuttgart4" method="delete"/>
</source>

The expected output is:

<source id="berlin"> 

Please help.

Here is uniq.pl:

#!/usr/bin/perl -w
use warnings;
use XML::Twig;
my $t= XML::Twig->new;
my $v= XML::Twig::Elt->new;

$t-> parsefile ('input.xml');
$v= $t->first_elt('[@method]');

$w= $v->parent;
$w->print;

The input file: input.xml

<Germany>
 <airport id1="1">
  <plane id="3">
   <source id="berlin">
     <target id="karlsruhe" method="delete"/>
     <target id="Stuttgart4" method="delete"/>
   </source>
  </plane>
 </airport>
</Germany>
도움이 되었습니까?

해결책

When you call print on an element, it prints the element, the entire element, from start tag to end tag, including all children and their descendants.

What you want it just the start tag, using the start_tag method:

print $w->start_tag;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top