source: trunk/gsdl/perllib/cpan/XML/XPath/NodeSet.pm@ 7909

Last change on this file since 7909 was 7909, checked in by mdewsnip, 17 years ago

CPAN module for processing XPath expressions.

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