Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Regarding modules for XML-related work, you may want to try XML::LibXML -- I've found it to be fast, compact, well documented, and fully capable. And it does xpath stuff quite easily. It requires the Gnu LibXML package (available separately, not a Perl thing), which should be readily available for MS systems.

In fact, LibXML is so easy to use, once I started to grok the XPath usage, I just whipped up a quick command line tool on my own, not even thinking to look for something that someone else might have written already, and it's been working just great:

#!/usr/bin/perl =head1 NAME exp -- extract XPath matches from XML data =head1 SYNOPSIS exp [-x] [-r] {-p xpath_spec | -f xpath.list} file.xml =head1 DESCRIPTION This program will print portions (if any) from an XML file that match a given XPath specifier. You can provide an XPath spec on the command line (via the "-p /path" option), or read a list of one or more path specs from a file (via the "-f path.list" option). The matching content is always printed to STDOUT. By default, exp prints just the text content of matches. Use the "-x" option to print full XML strings. In case your "file.xml" happens to be a concatenation of elements without a root tag (e.g. a stream of <tag>...</tag><tag>...</tag>...), you can use the "-r" option to provide a root tag automatically before parsing. =head1 ASSUMPTIONS We assume that the character encoding is utf8 (and we set STDOUT accordingly). We also assume that the entire XML input fits in memory (we use XML::LibXML, which tend to be fairly compact); in the case of using the -r option, the XML source text becomes memory resident as well, before passing it to XML::LibXML for parsing. =head1 AUTHOR David Graff <graff (at) ldc (dot) upenn (dot) edu> =cut use strict; use XML::LibXML; use Getopt::Long; binmode STDOUT,":utf8"; my $Usage = "Usage: $0 [-x] [-r] {-p xpath_spec | -f xpath.list} file +.xml\n"; my %opt; die $Usage unless ( GetOptions( \%opt, 'x', 'r', 'p=s', 'f=s' ) and @ARGV == 1 and -f $ARGV[0] and ( $opt{p} or $opt{f +} )); my $xmlfile = shift; my @paths; push @paths, $opt{p} if ( $opt{p} =~ /\w/ ); if ( $opt{f} ) { open( my $list, "<", $opt{f} ) or die "$opt{f}: $!\n"; while (<$list>) { next if ( /^\s*[;\#-]/ or !/\w/ ); chomp; push @paths, $_; } } my $xml = XML::LibXML->new; my $doc; if ( ! $opt{r} ) { $doc = $xml->parse_file( $xmlfile ); } else { my $xmlstr = "<EXP_ROOT_$$>"; { local $/; open( X, '<:utf8', $xmlfile ) or die "Unable to read $xmlfile: + $!\n"; $xmlstr .= <X>; close X; } $xmlstr .= "</EXP_ROOT_$$>"; s{^(?!//)}{/EXP_ROOT_$$} for ( @paths ); $doc = $xml->parse_string( $xmlstr ); } my $pth = XML::LibXML::XPathContext->new( $doc ); for my $p ( @paths ) { for my $n ( $pth->findnodes( $p )) { if ( $opt{x} ) { print $n->toString, "\n"; } else { print $n->textContent, "\n"; } } }

In reply to Re: XPath command line utility... by graff
in thread XPath command line utility... by biswanath_c

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2023-12-10 04:49 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (38 votes). Check out past polls.