#!/usr/bin/perl -w
use strict;

$| = 1;

unless (@ARGV >= 1) {
	print STDERR qq(Usage:
$0 [filename] query
				
	If no filename is given, supply XML on STDIN.
);
	exit;
}

use XML::XPath;

my $xpath;
if (@ARGV == 2) {
	$xpath = XML::XPath->new(filename => shift(@ARGV));
}
else {
	$xpath = XML::XPath->new(ioref => \*STDIN);
}
	
my $nodes = $xpath->find($ARGV[0]);

unless ($nodes->isa('XML::XPath::NodeSet')) {
	print STDERR "Query didn't return a nodeset. Value: ";
	print $nodes->value, "\n";
	exit;
}

if ($nodes->size) {
	print "Found ", $nodes->size, " nodes with query '$ARGV[0]':\n";
	foreach my $node ($nodes->get_nodelist) {
		print STDERR "-- NODE --\n";
		print XML::XPath::XMLParser::as_string($node);
	}
}
else {
	print STDERR "No nodes found for query '$ARGV[0]'";
}

print STDERR "\n";

exit;
