[24626] | 1 | # $Id: NodeSet.pm 7909 2004-08-06 05:11:55Z mdewsnip $
|
---|
| 2 |
|
---|
| 3 | package XML::XPath::NodeSet;
|
---|
| 4 | use strict;
|
---|
| 5 |
|
---|
| 6 | use XML::XPath::Boolean;
|
---|
| 7 |
|
---|
| 8 | use overload
|
---|
| 9 | '""' => \&to_literal,
|
---|
| 10 | 'bool' => \&to_boolean,
|
---|
| 11 | ;
|
---|
| 12 |
|
---|
| 13 | sub new {
|
---|
| 14 | my $class = shift;
|
---|
| 15 | bless [], $class;
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | sub sort {
|
---|
| 19 | my $self = CORE::shift;
|
---|
| 20 | @$self = CORE::sort { $a->get_global_pos <=> $b->get_global_pos } @$self;
|
---|
| 21 | return $self;
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | sub pop {
|
---|
| 25 | my $self = CORE::shift;
|
---|
| 26 | CORE::pop @$self;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | sub push {
|
---|
| 30 | my $self = CORE::shift;
|
---|
| 31 | my (@nodes) = @_;
|
---|
| 32 | CORE::push @$self, @nodes;
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | sub append {
|
---|
| 36 | my $self = CORE::shift;
|
---|
| 37 | my ($nodeset) = @_;
|
---|
| 38 | CORE::push @$self, $nodeset->get_nodelist;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | sub shift {
|
---|
| 42 | my $self = CORE::shift;
|
---|
| 43 | CORE::shift @$self;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | sub unshift {
|
---|
| 47 | my $self = CORE::shift;
|
---|
| 48 | my (@nodes) = @_;
|
---|
| 49 | CORE::unshift @$self, @nodes;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | sub prepend {
|
---|
| 53 | my $self = CORE::shift;
|
---|
| 54 | my ($nodeset) = @_;
|
---|
| 55 | CORE::unshift @$self, $nodeset->get_nodelist;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | sub size {
|
---|
| 59 | my $self = CORE::shift;
|
---|
| 60 | scalar @$self;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | sub get_node { # uses array index starting at 1, not 0
|
---|
| 64 | my $self = CORE::shift;
|
---|
| 65 | my ($pos) = @_;
|
---|
| 66 | $self->[$pos - 1];
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | sub getRootNode {
|
---|
| 70 | my $self = CORE::shift;
|
---|
| 71 | return $self->[0]->getRootNode;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | sub get_nodelist {
|
---|
| 75 | my $self = CORE::shift;
|
---|
| 76 | @$self;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | sub to_boolean {
|
---|
| 80 | my $self = CORE::shift;
|
---|
| 81 | return (@$self > 0) ? XML::XPath::Boolean->True : XML::XPath::Boolean->False;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | sub string_value {
|
---|
| 85 | my $self = CORE::shift;
|
---|
| 86 | return '' unless @$self;
|
---|
| 87 | return $self->[0]->string_value;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | sub to_literal {
|
---|
| 91 | my $self = CORE::shift;
|
---|
| 92 | return XML::XPath::Literal->new(
|
---|
| 93 | join('', map { $_->string_value } @$self)
|
---|
| 94 | );
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | sub to_number {
|
---|
| 98 | my $self = CORE::shift;
|
---|
| 99 | return XML::XPath::Number->new(
|
---|
| 100 | $self->to_literal
|
---|
| 101 | );
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | 1;
|
---|
| 105 | __END__
|
---|
| 106 |
|
---|
| 107 | =head1 NAME
|
---|
| 108 |
|
---|
| 109 | XML::XPath::NodeSet - a list of XML document nodes
|
---|
| 110 |
|
---|
| 111 | =head1 DESCRIPTION
|
---|
| 112 |
|
---|
| 113 | An XML::XPath::NodeSet object contains an ordered list of nodes. The nodes
|
---|
| 114 | each take the same format as described in L<XML::XPath::XMLParser>.
|
---|
| 115 |
|
---|
| 116 | =head1 SYNOPSIS
|
---|
| 117 |
|
---|
| 118 | my $results = $xp->find('//someelement');
|
---|
| 119 | if (!$results->isa('XML::XPath::NodeSet')) {
|
---|
| 120 | print "Found $results\n";
|
---|
| 121 | exit;
|
---|
| 122 | }
|
---|
| 123 | foreach my $context ($results->get_nodelist) {
|
---|
| 124 | my $newresults = $xp->find('./other/element', $context);
|
---|
| 125 | ...
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | =head1 API
|
---|
| 129 |
|
---|
| 130 | =head2 new()
|
---|
| 131 |
|
---|
| 132 | You will almost never have to create a new NodeSet object, as it is all
|
---|
| 133 | done for you by XPath.
|
---|
| 134 |
|
---|
| 135 | =head2 get_nodelist()
|
---|
| 136 |
|
---|
| 137 | Returns a list of nodes. See L<XML::XPath::XMLParser> for the format of
|
---|
| 138 | the nodes.
|
---|
| 139 |
|
---|
| 140 | =head2 string_value()
|
---|
| 141 |
|
---|
| 142 | Returns the string-value of the first node in the list.
|
---|
| 143 | See the XPath specification for what "string-value" means.
|
---|
| 144 |
|
---|
| 145 | =head2 to_literal()
|
---|
| 146 |
|
---|
| 147 | Returns the concatenation of all the string-values of all
|
---|
| 148 | the nodes in the list.
|
---|
| 149 |
|
---|
| 150 | =head2 get_node($pos)
|
---|
| 151 |
|
---|
| 152 | Returns the node at $pos. The node position in XPath is based at 1, not 0.
|
---|
| 153 |
|
---|
| 154 | =head2 size()
|
---|
| 155 |
|
---|
| 156 | Returns the number of nodes in the NodeSet.
|
---|
| 157 |
|
---|
| 158 | =head2 pop()
|
---|
| 159 |
|
---|
| 160 | Equivalent to perl's pop function.
|
---|
| 161 |
|
---|
| 162 | =head2 push(@nodes)
|
---|
| 163 |
|
---|
| 164 | Equivalent to perl's push function.
|
---|
| 165 |
|
---|
| 166 | =head2 append($nodeset)
|
---|
| 167 |
|
---|
| 168 | Given a nodeset, appends the list of nodes in $nodeset to the end of the
|
---|
| 169 | current list.
|
---|
| 170 |
|
---|
| 171 | =head2 shift()
|
---|
| 172 |
|
---|
| 173 | Equivalent to perl's shift function.
|
---|
| 174 |
|
---|
| 175 | =head2 unshift(@nodes)
|
---|
| 176 |
|
---|
| 177 | Equivalent to perl's unshift function.
|
---|
| 178 |
|
---|
| 179 | =head2 prepend($nodeset)
|
---|
| 180 |
|
---|
| 181 | Given a nodeset, prepends the list of nodes in $nodeset to the front of
|
---|
| 182 | the current list.
|
---|
| 183 |
|
---|
| 184 | =cut
|
---|