Many thanks to all ( esp. mirod ) - this was *extremley* usefull info.
To refine my origional request, I wanted to see elements ( with their attributes ) and any corresponding values ( but with no recursion ) - if such values exist.
( This is because the element attributes themselves sometimes contain 'data' by their very name / existance )
My finished code, with an example of requirement / result :
foreach my $file ( @filelist ) {
my $twig= new XML::Twig();
$twig->parsefile($file);
my $twig_root = $twig->root;
foreach my $xpath_arg ( @xpath_args ) {
my @xpath_result = $twig_root->get_xpa
+th($xpath_arg);
foreach ( @xpath_result ) {
print &complete_path($
+_);
$_->contains_only_text
+ ? print ' : ' . $_->text . "\n" : print "\n";
}
}
}
sub complete_path() {
my( $elt )= @_;
return '/' . join( '/', map { tag_desc($_) } reverse $elt->anc
+estors_or_self);
}
sub tag_desc {
my( $elt )= @_;
my %atts= %{$elt->atts};
my $atts= %atts ? '[' . join( " ", map { qq{$_="$atts{$_}"} }
+ sort keys %atts) . ']' : '';
return $elt->tag . $atts;
}
Running : ./show_xpath_value.pl -x '//*[@keep="1"]'
/doc/elt[att1="1" keep="1"]
/doc/elt[att1="1" keep="1"]/elt2[att2="2" keep="1"] : foo
/doc/elt[att1="1"]/elt2[att2="2" keep="1"] : bar2
/doc/elt[att1="1" keep="1"]
/doc/elt[att1="1" keep="1"]/elt2[att2="2" keep="1"] : foo3
/doc/elt[att1="1" keep="1"]/elt2[att2="2" keep="1"] : bar3
Giving me just what I wanted !
Thanks again.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.