| 1 | # | ||
| 2 | # Copyright (C) 1998 Ken MacLeod | ||
| 3 | # XML::Grove::XPointer is free software; you can redistribute it | ||
| 4 | # and/or modify it under the same terms as Perl itself. | ||
| 5 | # | ||
| 6 | # $Id: XPointer.pm,v 1.2 1999/08/17 15:01:28 kmacleod Exp $ | ||
| 7 | # | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | |||
| 11 | package XML::Grove::XPointer; | ||
| 12 | |||
| 13 | package XML::Grove::Document; | ||
| 14 | |||
| 15 | sub xp_child { | ||
| 16 | goto &XML::Grove::Element::xp_child; | ||
| 17 | } | ||
| 18 | |||
| 19 | package XML::Grove::Element; | ||
| 20 | |||
| 21 | sub xp_child { | ||
| 22 | my $self = shift; | ||
| 23 | my $instance = shift; | ||
| 24 | my $node_type = shift; | ||
| 25 | |||
| 26 | my $look_for; | ||
| 27 | if (defined($node_type) && substr($node_type, 0, 1) eq '#') { | ||
| 28 | $node_type eq '#element' and do { $look_for = 'XML::Grove::Element' }; | ||
| 29 | $node_type eq '#pi' and do { $look_for = 'XML::Grove::PI' }; | ||
| 30 | $node_type eq '#comment' and do { $look_for = 'XML::Grove::Comment' }; | ||
| 31 | $node_type eq '#text' and do { $look_for = 'XML::Grove::Characters' }; | ||
| 32 | $node_type eq '#cdata' and do { $look_for = 'XML::Grove::CData' }; | ||
| 33 | $node_type eq '#any' and do { $node_type = undef }; | ||
| 34 | } elsif (defined($node_type)) { | ||
| 35 | $look_for = 'element-name'; | ||
| 36 | } | ||
| 37 | |||
| 38 | my $contents = $self->{Contents}; | ||
| 39 | my $object = undef; | ||
| 40 | |||
| 41 | $instance--; # 0 based | ||
| 42 | |||
| 43 | if (!defined $node_type) { | ||
| 44 | $object = $contents->[$instance]; | ||
| 45 | } elsif ($look_for eq 'element-name') { | ||
| 46 | my $i_object; | ||
| 47 | foreach $i_object (@$contents) { | ||
| 48 | if (ref($i_object) eq 'XML::Grove::Element' | ||
| 49 | && $i_object->{Name} eq $node_type | ||
| 50 | && $instance-- == 0) { | ||
| 51 | $object = $i_object; | ||
| 52 | last; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | } else { | ||
| 56 | my $i_object; | ||
| 57 | foreach $i_object (@$contents) { | ||
| 58 | if (ref($i_object) eq $look_for | ||
| 59 | && $instance-- == 0) { | ||
| 60 | $object = $i_object; | ||
| 61 | last; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | return $object; | ||
| 67 | } | ||
| 68 | |||
| 69 | 1; | ||
| 70 | |||
| 71 | __END__ |