Pregunta

I do not know how to display the ancestors_or_self of one Element.

Here is the error message I get when using the method ancestors_or_self(): Can't call method "print" without a package or object reference at xxxx

#!/usr/bin/perl -w
use warnings;
use XML::Twig;

my $t= XML::Twig->new;
my $v= XML::Twig::Elt->new;

$v= $t->first_elt('[@id]');
$v->print;
print ("\n\n");
$v->ancestors_or_self->print;

thanks for your help on Perl XML::Twig

¿Fue útil?

Solución

ancestors_or_self returns a list -- assuming you want the path to the element, you also want ->path not ->print. And as ->path returns the path you will have to do something like this:

#!/usr/bin/perl -w
use warnings;
use XML::Twig;

my $t= XML::Twig->new;
my $v= XML::Twig::Elt->new;

$v= $t->first_elt('[@id]');
print $v->path . "\n";
print ("\n\n");
print $_->path . "\n" foreach $v->ancestors_or_self;

Otros consejos

That's because ancestors_or_self returns a list, and you cannot call a method of list. Use this instead:

$_->print for $v->ancestors_or_self; 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top