[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/se3-unattended/var/se3/unattended/install/linuxaux/opt/perl/bin/ -> xpath (source)

   1  #!/opt/perl/bin/perl -w
   2  
   3  eval 'exec /opt/perl/bin/perl -w -S $0 $1+"$@"}'
   4      if 0; # not running under some shell
   5  use strict;
   6  
   7  $| = 1;
   8  
   9  unless (@ARGV >= 1) {
  10      print STDERR qq(Usage:
  11  $0 [filename] query
  12                  
  13      If no filename is given, supply XML on STDIN.
  14  );
  15      exit;
  16  }
  17  
  18  use XML::XPath;
  19  
  20  my $xpath;
  21  
  22  my $pipeline;
  23  
  24  if ($ARGV[0] eq '-p') {
  25      # pipeline mode
  26      $pipeline = 1;
  27      shift @ARGV;
  28  }
  29  if (@ARGV >= 2) {
  30      $xpath = XML::XPath->new(filename => shift(@ARGV));
  31  }
  32  else {
  33      $xpath = XML::XPath->new(ioref => \*STDIN);
  34  }
  35  
  36  my $nodes = $xpath->find(shift @ARGV);
  37  
  38  unless ($nodes->isa('XML::XPath::NodeSet')) {
  39  NOTNODES:
  40      print STDERR "Query didn't return a nodeset. Value: ";
  41      print $nodes->value, "\n";
  42      exit;
  43  }
  44  
  45  if ($pipeline) {
  46      $nodes = find_more($nodes);
  47      goto NOTNODES unless $nodes->isa('XML::XPath::NodeSet');
  48  }
  49  
  50  if ($nodes->size) {
  51      print STDERR "Found ", $nodes->size, " nodes:\n";
  52      foreach my $node ($nodes->get_nodelist) {
  53          print STDERR "-- NODE --\n";
  54          print $node->toString;
  55      }
  56  }
  57  else {
  58      print STDERR "No nodes found";
  59  }
  60  
  61  print STDERR "\n";
  62  
  63  exit;
  64  
  65  sub find_more {
  66      my ($nodes) = @_;
  67      if (!@ARGV) {
  68          return $nodes;
  69      }
  70      
  71      my $newnodes = XML::XPath::NodeSet->new;
  72      
  73      my $find = shift @ARGV;
  74      
  75      foreach my $node ($nodes->get_nodelist) {
  76          my $new = $xpath->find($find, $node);
  77          if ($new->isa('XML::XPath::NodeSet')) {
  78              $newnodes->append($new);
  79          }
  80          else {
  81              warn "Not a nodeset: ", $new->value, "\n";
  82          }
  83      }
  84      
  85      return find_more($newnodes);
  86  }


Generated: Tue Mar 17 22:47:18 2015 Cross-referenced by PHPXref 0.7.1