From a2d12bc84fb2af87dd1c0c6e5bc854554902cd67 Mon Sep 17 00:00:00 2001 From: Lorry Tar Creator Date: Sun, 26 Jan 2003 19:35:03 +0000 Subject: Imported from /home/lorry/working-area/delta_perl-xml-xpath/XML-XPath-1.13.tar.gz. --- examples/xpath | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100755 examples/xpath (limited to 'examples/xpath') diff --git a/examples/xpath b/examples/xpath new file mode 100755 index 0000000..bb7f0fc --- /dev/null +++ b/examples/xpath @@ -0,0 +1,83 @@ +#!/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; + +my $pipeline; + +if ($ARGV[0] eq '-p') { + # pipeline mode + $pipeline = 1; + shift @ARGV; +} +if (@ARGV >= 2) { + $xpath = XML::XPath->new(filename => shift(@ARGV)); +} +else { + $xpath = XML::XPath->new(ioref => \*STDIN); +} + +my $nodes = $xpath->find(shift @ARGV); + +unless ($nodes->isa('XML::XPath::NodeSet')) { +NOTNODES: + print STDERR "Query didn't return a nodeset. Value: "; + print $nodes->value, "\n"; + exit; +} + +if ($pipeline) { + $nodes = find_more($nodes); + goto NOTNODES unless $nodes->isa('XML::XPath::NodeSet'); +} + +if ($nodes->size) { + print STDERR "Found ", $nodes->size, " nodes:\n"; + foreach my $node ($nodes->get_nodelist) { + print STDERR "-- NODE --\n"; + print $node->toString; + } +} +else { + print STDERR "No nodes found"; +} + +print STDERR "\n"; + +exit; + +sub find_more { + my ($nodes) = @_; + if (!@ARGV) { + return $nodes; + } + + my $newnodes = XML::XPath::NodeSet->new; + + my $find = shift @ARGV; + + foreach my $node ($nodes->get_nodelist) { + my $new = $xpath->find($find, $node); + if ($new->isa('XML::XPath::NodeSet')) { + $newnodes->append($new); + } + else { + warn "Not a nodeset: ", $new->value, "\n"; + } + } + + return find_more($newnodes); +} -- cgit v1.2.1