| 1 | package XML::DOM2::XPath; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | our $VERSION = '0.01'; | ||
| 7 | |||
| 8 | =pod | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | XML::DOM2::XPath | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | Provides the required methods for XPath | ||
| 17 | |||
| 18 | =head1 TODO | ||
| 19 | |||
| 20 | see http://www.w3.org/TR/xpath | ||
| 21 | |||
| 22 | =head1 METHODS | ||
| 23 | =cut | ||
| 24 | |||
| 25 | =head2 getElementByXPath | ||
| 26 | |||
| 27 | Returns an element by XPath, This is still under development not | ||
| 28 | all xpath rules may work correctly. | ||
| 29 | |||
| 30 | $element->getElementByXPath('/Blah/Foo/Bar'); | ||
| 31 | |||
| 32 | =cut | ||
| 33 | sub getElementByXPath | ||
| 34 | { | ||
| 35 | my ($self, $path) = @_; | ||
| 36 | # Remove double directories, prevents repathing to root | ||
| 37 | $path =~ s/\/\//\// if not ref($path); | ||
| 38 | # Aquire Next steps in Path | ||
| 39 | my @path = ref($path) ? @{$path} : split(/\//, $path); | ||
| 40 | return if not @path; | ||
| 41 | my $this = shift @path; | ||
| 42 | my $next; | ||
| 43 | if($this eq '') { | ||
| 44 | # Root Document | ||
| 45 | push @next, $self->document->documentElement; | ||
| 46 | } elsif($this eq '..') { | ||
| 47 | # Parent Element or self (top level reached) | ||
| 48 | push @next, $self->getParent ? $self->getParent : $self; | ||
| 49 | } elsif($this eq '.') { | ||
| 50 | # Current Element (this is almost useless) | ||
| 51 | push @next, $self; | ||
| 52 | } else { | ||
| 53 | # Return Children with this name | ||
| 54 | @next = $self->getChildrenByName( $this ); | ||
| 55 | } | ||
| 56 | return @next if not @path; | ||
| 57 | return map { $_->getElementByXPath( \@path ) }, @next; | ||
| 58 | } | ||
| 59 | |||
| 60 | =head1 AUTHOR | ||
| 61 | |||
| 62 | Martin Owens, doctormo@postmaster.co.uk | ||
| 63 | |||
| 64 | =head1 SEE ALSO | ||
| 65 | |||
| 66 | perl(1), L<XML::DOM2>, L<XML::DOM2::Element> | ||
| 67 | |||
| 68 | L<http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/level-one-core.html> DOM at the W3C | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | return 1; | ||