http://www.perlmonks.org?node_id=811602


in reply to XPATH DOM traverse html/xml

use strict; use warnings; use XML::LibXML qw( XML_ELEMENT_NODE ); sub visit { my ($node, $path) = @_; $path = '' if !defined($path); print("$path: ", $node->nodeName(), "\n"); $path .= '/' if length($path) && $path !~ m{/\z}; my @children = grep $_->nodeType() == XML_ELEMENT_NODE, $node->childNodes(); visit($children[$_], "$path*[".($_+1).']') for 0..$#children; } my $parser = XML::LibXML->new(); my $doc = $parser->parse_fh(*STDIN); my $root = $doc->documentElement(); visit($root, '/*');
/*: OTA_AirSeatMapRS /*/*[1]: Success /*/*[2]: SeatMapResponses /*/*[2]/*[1]: SeatMapResponse /*/*[2]/*[1]/*[1]: FlightSegmentInfo /*/*[2]/*[1]/*[1]/*[1]: DepartureAirport /*/*[2]/*[1]/*[1]/*[2]: ArrivalAirport /*/*[2]/*[1]/*[1]/*[3]: OperatingAirline /*/*[2]/*[1]/*[1]/*[4]: MarketingAirline /*/*[2]/*[1]/*[2]: SeatMapDetails /*/*[2]/*[1]/*[2]/*[1]: CabinClass /*/*[2]/*[1]/*[2]/*[1]/*[1]: AirRows /*/*[2]/*[1]/*[2]/*[1]/*[1]/*[1]: AirRow /*/*[2]/*[1]/*[2]/*[1]/*[1]/*[1]/*[1]: AirSeats /*/*[2]/*[1]/*[2]/*[1]/*[1]/*[1]/*[1]/*[1]: AirSeat /*/*[2]/*[1]/*[2]/*[1]/*[1]/*[1]/*[1]/*[2]: AirSeat /*/*[2]/*[1]/*[2]/*[1]/*[1]/*[1]/*[1]/*[3]: AirSeat /*/*[2]/*[1]/*[2]/*[1]/*[1]/*[1]/*[1]/*[4]: AirSeat /*/*[2]/*[1]/*[2]/*[1]/*[1]/*[1]/*[1]/*[5]: AirSeat /*/*[2]/*[1]/*[2]/*[1]/*[1]/*[1]/*[1]/*[6]: AirSeat /*/*[2]/*[1]/*[2]/*[1]/*[1]/*[1]/*[2]: AirRowCharacteristics /*/*[2]/*[1]/*[2]/*[1]/*[1]/*[2]: AirRow /*/*[2]/*[1]/*[2]/*[1]/*[1]/*[2]/*[1]: AirSeats /*/*[2]/*[1]/*[2]/*[1]/*[1]/*[2]/*[1]/*[1]: AirSeat /*/*[2]/*[1]/*[2]/*[1]/*[1]/*[2]/*[1]/*[2]: AirSeat ... /*/*[2]/*[2]/*[2]/*[1]/*[1]/*[21]/*[1]/*[5]: AirSeat /*/*[2]/*[2]/*[2]/*[1]/*[1]/*[21]/*[1]/*[6]: AirSeat /*/*[2]/*[2]/*[2]/*[1]/*[1]/*[21]/*[2]: AirRowCharacteristics /*/*[2]/*[2]/*[2]/*[1]/*[1]/*[22]: AirRow /*/*[2]/*[2]/*[2]/*[1]/*[1]/*[22]/*[1]: AirSeats /*/*[2]/*[2]/*[2]/*[1]/*[1]/*[22]/*[1]/*[1]: AirSeat /*/*[2]/*[2]/*[2]/*[1]/*[1]/*[22]/*[1]/*[2]: AirSeat /*/*[2]/*[2]/*[2]/*[1]/*[1]/*[22]/*[1]/*[3]: AirSeat /*/*[2]/*[2]/*[2]/*[1]/*[1]/*[22]/*[1]/*[4]: AirSeat /*/*[2]/*[2]/*[2]/*[1]/*[1]/*[22]/*[1]/*[5]: AirSeat /*/*[2]/*[2]/*[2]/*[1]/*[1]/*[22]/*[1]/*[6]: AirSeat /*/*[2]/*[2]/*[2]/*[1]/*[1]/*[22]/*[2]: AirRowCharacteristics /*/*[2]/*[2]/*[3]: BookingReferenceID /*/*[2]/*[3]: AirTravelers /*/*[2]/*[3]/*[1]: AirTraveler /*/*[2]/*[3]/*[1]/*[1]: PersonName /*/*[2]/*[3]/*[1]/*[1]/*[1]: GivenName /*/*[2]/*[3]/*[1]/*[1]/*[2]: MiddleName /*/*[2]/*[3]/*[1]/*[1]/*[3]: Surname /*/*[2]/*[3]/*[1]/*[1]/*[4]: NameTitle /*/*[2]/*[3]/*[1]/*[2]: TravelerRefNumber /*/*[2]/*[3]/*[2]: AirTraveler /*/*[2]/*[3]/*[2]/*[1]: PersonName /*/*[2]/*[3]/*[2]/*[1]/*[1]: GivenName /*/*[2]/*[3]/*[2]/*[1]/*[2]: MiddleName /*/*[2]/*[3]/*[2]/*[1]/*[3]: Surname /*/*[2]/*[3]/*[2]/*[1]/*[4]: NameTitle /*/*[2]/*[3]/*[2]/*[2]: TravelerRefNumber /*/*[2]/*[3]/*[3]: AirTraveler /*/*[2]/*[3]/*[3]/*[1]: PersonName /*/*[2]/*[3]/*[3]/*[1]/*[1]: GivenName /*/*[2]/*[3]/*[3]/*[1]/*[2]: MiddleName /*/*[2]/*[3]/*[3]/*[1]/*[3]: Surname /*/*[2]/*[3]/*[3]/*[1]/*[4]: NameTitle /*/*[2]/*[3]/*[3]/*[2]: TravelerRefNumber

You can use other expressions for the path segments if you want. I kept it simple.

And of course, you can do other things other than printing the path and element name.

Update: Fixed bugs.