exp -p "//info_name | //it_size" test.xml # output: FZGA34177.b1 35000 FZGA34178.b1 12000 FZGA34179.b1 7000 FZGA34180.b1 3000 FZGA34181.b1 7000 #### #!/usr/bin/perl use strict; use XML::LibXML; use Getopt::Long; binmode STDOUT,":utf8"; my $Usage = "Usage: $0 [-x] [-r] -p xpath_spec file.xml\n"; my %opt; die $Usage unless ( GetOptions( \%opt, 'x', 'r', 'p=s' ) and @ARGV == 1 and -f $ARGV[0] and $opt{p} =~ /\w/ ); my $xmlfile = shift; my $xml = XML::LibXML->new; my $doc; if ( ! $opt{r} ) { $doc = $xml->parse_file( $xmlfile ); } else { my $xmlstr = ""; $opt{p} = "/EXP_ROOT_$$" . $opt{p}; { local $/; open( X, '<:utf8', $xmlfile ) or die "Unable to read $xmlfile: $!\n"; $xmlstr .= ; close X; } $xmlstr .= ""; $doc = $xml->parse_string( $xmlstr ); } my $pth = XML::LibXML::XPathContext->new( $doc ); for my $n ( $pth->findnodes( $opt{p} )) { if ( $opt{x} ) { print $n->toString, "\n"; } else { print $n->textContent, "\n"; } } =head1 NAME exp -- extract XPath matches from XML data =head1 SYNOPSIS exp [-r] [-x] -p xpath_spec file.xml -r : supply a root node for the xml stream -x : output the matching content as xml elements =head1 DESCRIPTION This program will print portions (if any) from an XML file that match a given XPath specifier. =head1 AUTHOR David Graff =cut