<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl

# $Id$

use XML::LibXML;

use strict;
use warnings;

my $parser = XML::LibXML-&gt;new();
my $xpath = shift @ARGV;

if ( scalar @ARGV ) {
    foreach ( @ARGV ) {
        my $doc = $parser-&gt;parse_file( $_ );
        my $result = $doc-&gt;find( $xpath );
        handle_result( $result );
        undef $doc;
    }
}
else {
    # read from std in
    my @doc = &lt;STDIN&gt;;
    my $string = join "", @doc;
    my $doc = $parser-&gt;parse_string( $string );
    my $result = $doc-&gt;find( $xpath );
    exit handle_result( $result );
}

sub handle_result {
    my $result = shift;

    return 1 unless defined $result;

    if ( $result-&gt;isa( 'XML::LibXML::NodeList' ) ) {
        foreach ( @$result ) {
            print $_-&gt;toString(1) , "\n";
        }
        return 0;
    }

    if ( $result-&gt;isa( 'XML::LibXML::Literal' ) ) {
        print $result-&gt;value , "\n";
        return 0;
    }

    if ( $result-&gt;isa( 'XML::LibXML::Boolean' ) ){
        print $result-&gt;to_literal , "\n";
        return 0;
    }

    return 1;
}
</pre></body></html>