| 1 | package XML::XPath::LocationPath; | ||
| 2 | |||
| 3 | $VERSION = '1.49'; | ||
| 4 | |||
| 5 | use Scalar::Util qw(blessed); | ||
| 6 | use XML::XPath::Root; | ||
| 7 | use strict; use warnings; | ||
| 8 | |||
| 9 | sub new { | ||
| 10 | my $class = shift; | ||
| 11 | my $self = []; | ||
| 12 | bless $self, $class; | ||
| 13 | } | ||
| 14 | |||
| 15 | sub as_string { | ||
| 16 | my $self = shift; | ||
| 17 | my $string; | ||
| 18 | for (my $i = 0; $i < @$self; $i++) { | ||
| 19 | $string .= $self->[$i]->as_string if defined $self->[$i]->as_string; | ||
| 20 | $string .= "/" if $self->[$i+1]; | ||
| 21 | } | ||
| 22 | return $string; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub as_xml { | ||
| 26 | my $self = shift; | ||
| 27 | my $string = "<LocationPath>\n"; | ||
| 28 | |||
| 29 | for (my $i = 0; $i < @$self; $i++) { | ||
| 30 | $string .= $self->[$i]->as_xml; | ||
| 31 | } | ||
| 32 | |||
| 33 | $string .= "</LocationPath>\n"; | ||
| 34 | return $string; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub set_root { | ||
| 38 | my $self = shift; | ||
| 39 | unshift @$self, XML::XPath::Root->new(); | ||
| 40 | } | ||
| 41 | |||
| 42 | sub evaluate { | ||
| 43 | my $self = shift; | ||
| 44 | # context _MUST_ be a single node | ||
| 45 | my $context = shift; | ||
| 46 | die "No context" unless $context; | ||
| 47 | |||
| 48 | # I _think_ this is how it should work :) | ||
| 49 | |||
| 50 | my $nodeset = XML::XPath::NodeSet->new(); | ||
| 51 | $nodeset->push($context); | ||
| 52 | foreach my $step (@$self) { | ||
| 53 | next unless (defined $step && blessed($step)); | ||
| 54 | # For each step | ||
| 55 | # evaluate the step with the nodeset | ||
| 56 | my $pos = 1; | ||
| 57 | $nodeset = $step->evaluate($nodeset); | ||
| 58 | } | ||
| 59 | |||
| 60 | return $nodeset->remove_duplicates; | ||
| 61 | } | ||
| 62 | |||
| 63 | 1; |