Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (9)
    As of 2015-07-29 10:28 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (263 votes), past polls