diff options
author | Lorry Tar Creator <lorry-tar-importer@baserock.org> | 2003-01-26 19:35:03 +0000 |
---|---|---|
committer | <> | 2015-02-23 10:18:26 +0000 |
commit | a2d12bc84fb2af87dd1c0c6e5bc854554902cd67 (patch) | |
tree | 7665979c7c281b21971de576d93246a022bff649 /examples | |
download | perl-xml-xpath-a2d12bc84fb2af87dd1c0c6e5bc854554902cd67.tar.gz |
Imported from /home/lorry/working-area/delta_perl-xml-xpath/XML-XPath-1.13.tar.gz.HEADXML-XPath-1.13master
Diffstat (limited to 'examples')
-rw-r--r-- | examples/test.xml | 34 | ||||
-rwxr-xr-x | examples/xpath | 83 |
2 files changed, 117 insertions, 0 deletions
diff --git a/examples/test.xml b/examples/test.xml new file mode 100644 index 0000000..7a26967 --- /dev/null +++ b/examples/test.xml @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<timesheet xmlns:a="www" xmlns:b="xxx" xmlns="fred"> + <employee> + <name> + <forename>Matt</forename> + <surname>Sergeant</surname> + </name> + <department>Development IT</department> + </employee> + <rules> + <rule>NextRule1</rule> + <rule>NextRule2</rule> + </rules> + <projects> + <project a:Name="Consultancy > fred" b:Name="Fred"> + <sunday>0.00</sunday> + <monday>0.00</monday> + <tuesday>7.75</tuesday> + <wednesday>8.75</wednesday> + <thursday>7.75</thursday> + <friday>6.5</friday> + <saturday>0.00</saturday> + </project> + <project Name="Holiday"> + <sunday>0.00</sunday> + <monday>7.75</monday> + <tuesday>0.00</tuesday> + <wednesday>0.00</wednesday> + <thursday>0.00</thursday> + <friday>0.00</friday> + <saturday>0.00</saturday> + </project> + </projects> +</timesheet> 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); +} |