jxh has asked for the wisdom of the Perl Monks concerning the following question:

Consider :

my @res = $twig_root->get_xpath($xpath_arg); foreach ( @res ) { print $_->xpath . " : "; print $_->text . "\n"; }

However if an element contains attribute=value, these are not shown on the xpath string.

print XML::XPath::XMLParser::as_string( $_ ) will show this, but I cannont see the full 'path' in this manner..

any suggestions ?

Replies are listed 'Best First'.
Re: newbie : XPath - including element attributes
by Tanktalus (Canon) on Jan 25, 2006 at 02:24 UTC

    Looking at the docs for XML::Twig, it says:

    Return a unique XPath expression that can be used to find the element again.
    (emphasis added). To me that says simply that it may not be the same xpath that you used to get there, just one that will work to get this one again.

    In fact, if your get_xpath returns multiple entries, I would expect each one to give a different xpath answer.

Re: newbie : XPath - including element attributes
by mirod (Canon) on Jan 25, 2006 at 09:04 UTC

    The thing is that listing the attributes would not necessary create a unique path to the element. Using the position is the only way that's always garanteed to work. If you look at the example below, you will see that indeed several distinct results have the same complete_path output.

    So for your specific case, the solution is to actually write code that does what you want. Or at least to cut-n-paste the code below ;--) Basically it subclasses XML::Twig::Elt to add the complete_xpath method, that does what you want (you could also do it without subclassing, by just calling a function on the element).

    #!/usr/bin/perl use strict; use warnings; use XML::Twig; my $xpath_arg= '//*[@keep="1"]'; my $twig= XML::Twig->new( elt_class => 'my_elt')->parse( \*DATA); my $twig_root= $twig->root; my @res = $twig_root->get_xpath($xpath_arg); foreach ( @res ) { print $_->complete_path . " : "; print $_->text . "\n"; } package my_elt; use base 'XML::Twig::Elt'; sub complete_path { my( $elt)= @_; return '/' . join( '/', map { $_->tag_desc } reverse $elt->ancesto +rs_or_self); } sub tag_desc { my( $elt)= @_; my %atts= %{$elt->atts}; my $atts= %atts ? '[' . join( " ", map { qq{$_="$atts{$_}"} } sor +t keys %atts) . ']' : ''; return $elt->tag . $atts; } package main; __DATA__ <doc> <elt att1="1" keep="1"> <elt2 att2="2" keep="1">foo</elt2> <elt2 att2="2" keep="0">bar</elt2> </elt> <elt att1="1"> <elt2 att2="2" keep="0">foo2</elt2> <elt2 att2="2" keep="1">bar2</elt2> </elt> <elt att1="1" keep="1"> <elt2 att2="2" keep="1">foo3</elt2> <elt2 att2="2" keep="1">bar3</elt2> </elt> </doc>
      Blanked this reply - Attempt 2 with correct formatting to follow :
        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.