@@ -15,9 +15,258 @@ module REXML
15
15
# context node and convert it back when we write it.
16
16
@@namespaces = { }
17
17
18
- # Represents a tagged XML element. Elements are characterized by
19
- # having children, attributes, and names, and can themselves be
20
- # children.
18
+ # An \REXML::Element object represents an XML element.
19
+ #
20
+ # An element:
21
+ #
22
+ # - Has a name (string).
23
+ # - May have a parent (another element).
24
+ # - Has zero or more children
25
+ # (other elements, text, CDATA, processing instructions, and comments).
26
+ # - Has zero or more siblings
27
+ # (other elements, text, CDATA, processing instructions, and comments).
28
+ # - Has zero or more named attributes.
29
+ #
30
+ # === Name
31
+ #
32
+ # An element has a name, which is initially set when the element is created:
33
+ #
34
+ # e = REXML::Element.new('foo')
35
+ # e.name # => "foo"
36
+ #
37
+ # The name may be changed:
38
+ #
39
+ # e.name = 'bar'
40
+ # e.name # => "bar"
41
+ #
42
+ #
43
+ # === \Parent
44
+ #
45
+ # An element may have a parent.
46
+ #
47
+ # Its parent may be assigned explicitly when the element is created:
48
+ #
49
+ # e0 = REXML::Element.new('foo')
50
+ # e1 = REXML::Element.new('bar', e0)
51
+ # e1.parent # => <foo> ... </>
52
+ #
53
+ # Note: the representation of an element always shows the element's name.
54
+ # If the element has children, the representation indicates that
55
+ # by including an ellipsis (<tt>...</tt>).
56
+ #
57
+ # The parent may be assigned explicitly at any time:
58
+ #
59
+ # e2 = REXML::Element.new('baz')
60
+ # e1.parent = e2
61
+ # e1.parent # => <baz/>
62
+ #
63
+ # When an element is added as a child, its parent is set automatically:
64
+ #
65
+ # e1.add_element(e0)
66
+ # e0.parent # => <bar> ... </>
67
+ #
68
+ # For an element that has no parent, method +parent+ returns +nil+.
69
+ #
70
+ # === Children
71
+ #
72
+ # An element has zero or more children.
73
+ # The children are an ordered collection
74
+ # of all objects whose parent is the element itself.
75
+ #
76
+ # The children may include any combination of elements, text, comments,
77
+ # processing instructions, and CDATA.
78
+ # (This example keeps things clean by controlling whitespace
79
+ # via a +context+ setting.)
80
+ #
81
+ # xml_string = <<-EOT
82
+ # <root>
83
+ # <ele_0/>
84
+ # text 0
85
+ # <!--comment 0-->
86
+ # <?target_0 pi_0?>
87
+ # <![CDATA[cdata 0]]>
88
+ # <ele_1/>
89
+ # text 1
90
+ # <!--comment 1-->
91
+ # <?target_0 pi_1?>
92
+ # <![CDATA[cdata 1]]>
93
+ # </root>
94
+ # EOT
95
+ # context = {ignore_whitespace_nodes: :all, compress_whitespace: :all}
96
+ # d = REXML::Document.new(xml_string, context)
97
+ # root = d.root
98
+ # root.children.size # => 10
99
+ # root.each {|child| p "#{child.class}: #{child}" }
100
+ #
101
+ # Output:
102
+ #
103
+ # "REXML::Element: <ele_0/>"
104
+ # "REXML::Text: \n text 0\n "
105
+ # "REXML::Comment: comment 0"
106
+ # "REXML::Instruction: <?target_0 pi_0?>"
107
+ # "REXML::CData: cdata 0"
108
+ # "REXML::Element: <ele_1/>"
109
+ # "REXML::Text: \n text 1\n "
110
+ # "REXML::Comment: comment 1"
111
+ # "REXML::Instruction: <?target_0 pi_1?>"
112
+ # "REXML::CData: cdata 1"
113
+ #
114
+ # A child may be added using inherited methods
115
+ # Parent#insert_before or Parent#insert_after:
116
+ #
117
+ # xml_string = '<root><a/><c/><d/></root>'
118
+ # d = REXML::Document.new(xml_string)
119
+ # root = d.root
120
+ # c = d.root[1] # => <c/>
121
+ # root.insert_before(c, REXML::Element.new('b'))
122
+ # root.to_a # => [<a/>, <b/>, <c/>, <d/>]
123
+ #
124
+ # A child may be replaced using Parent#replace_child:
125
+ #
126
+ # root.replace_child(c, REXML::Element.new('x'))
127
+ # root.to_a # => [<a/>, <b/>, <x/>, <d/>]
128
+ #
129
+ # A child may be removed using Parent#delete:
130
+ #
131
+ # x = root[2] # => <x/>
132
+ # root.delete(x)
133
+ # root.to_a # => [<a/>, <b/>, <d/>]
134
+ #
135
+ # === Siblings
136
+ #
137
+ # An element has zero or more siblings,
138
+ # which are the other children of the element's parent.
139
+ #
140
+ # In the example above, element +ele_1+ is between a CDATA sibling
141
+ # and a text sibling:
142
+ #
143
+ # ele_1 = root[5] # => <ele_1/>
144
+ # ele_1.previous_sibling # => "cdata 0"
145
+ # ele_1.next_sibling # => "\n text 1\n "
146
+ #
147
+ # === \Attributes
148
+ #
149
+ # An element has zero or more named attributes.
150
+ #
151
+ # A new element has no attributes:
152
+ #
153
+ # e = REXML::Element.new('foo')
154
+ # e.attributes # => {}
155
+ #
156
+ # Attributes may be added:
157
+ #
158
+ # e.add_attribute('bar', 'baz')
159
+ # e.add_attribute('bat', 'bam')
160
+ # e.attributes.size # => 2
161
+ # e['bar'] # => "baz"
162
+ # e['bat'] # => "bam"
163
+ #
164
+ # An existing attribute may be modified:
165
+ #
166
+ # e.add_attribute('bar', 'bad')
167
+ # e.attributes.size # => 2
168
+ # e['bar'] # => "bad"
169
+ #
170
+ # An existing attribute may be deleted:
171
+ #
172
+ # e.delete_attribute('bar')
173
+ # e.attributes.size # => 1
174
+ # e['bar'] # => nil
175
+ #
176
+ # == What's Here
177
+ #
178
+ # To begin with, what's elsewhere?
179
+ #
180
+ # \Class \REXML::Element inherits from its ancestor classes:
181
+ #
182
+ # - REXML::Child
183
+ # - REXML::Parent
184
+ #
185
+ # \REXML::Element itself and its ancestors also include modules:
186
+ #
187
+ # - {Enumerable}[https://docs.ruby-lang.org/en/master/Enumerable.html]
188
+ # - REXML::Namespace
189
+ # - REXML::Node
190
+ # - REXML::XMLTokens
191
+ #
192
+ # === Methods for Creating an \Element
193
+ #
194
+ # ::new:: Returns a new empty element.
195
+ # #clone:: Returns a clone of another element.
196
+ #
197
+ # === Methods for Attributes
198
+ #
199
+ # {[attribute_name]}[#method-i-5B-5D]:: Returns an attribute value.
200
+ # #add_attribute:: Adds a new attribute.
201
+ # #add_attributes:: Adds multiple new attributes.
202
+ # #attribute:: Returns the attribute value for a given name and optional namespace.
203
+ # #delete_attribute:: Removes an attribute.
204
+ #
205
+ # === Methods for Children
206
+ #
207
+ # {[index]}[#method-i-5B-5D]:: Returns the child at the given offset.
208
+ # #add_element:: Adds an element as the last child.
209
+ # #delete_element:: Deletes a child element.
210
+ # #each_element:: Calls the given block with each child element.
211
+ # #each_element_with_attribute:: Calls the given block with each child element
212
+ # that meets given criteria,
213
+ # which can include the attribute name.
214
+ # #each_element_with_text:: Calls the given block with each child element
215
+ # that meets given criteria,
216
+ # which can include text.
217
+ # #get_elements:: Returns an array of element children that match a given xpath.
218
+ #
219
+ # === Methods for \Text Children
220
+ #
221
+ # #add_text:: Adds a text node to the element.
222
+ # #get_text:: Returns a text node that meets specified criteria.
223
+ # #text:: Returns the text string from the first node that meets specified criteria.
224
+ # #texts:: Returns an array of the text children of the element.
225
+ # #text=:: Adds, removes, or replaces the first text child of the element
226
+ #
227
+ # === Methods for Other Children
228
+ #
229
+ # #cdatas:: Returns an array of the cdata children of the element.
230
+ # #comments:: Returns an array of the comment children of the element.
231
+ # #instructions:: Returns an array of the instruction children of the element.
232
+ #
233
+ # === Methods for Namespaces
234
+ #
235
+ # #add_namespace:: Adds a namespace to the element.
236
+ # #delete_namespace:: Removes a namespace from the element.
237
+ # #namespace:: Returns the string namespace URI for the element.
238
+ # #namespaces:: Returns a hash of all defined namespaces in the element.
239
+ # #prefixes:: Returns an array of the string prefixes (names)
240
+ # of all defined namespaces in the element
241
+ #
242
+ # === Methods for Querying
243
+ #
244
+ # #document:: Returns the document, if any, that the element belongs to.
245
+ # #root:: Returns the most distant element (not document) ancestor of the element.
246
+ # #root_node:: Returns the most distant ancestor of the element.
247
+ # #xpath:: Returns the string xpath to the element
248
+ # relative to the most distant parent
249
+ # #has_attributes?:: Returns whether the element has attributes.
250
+ # #has_elements?:: Returns whether the element has elements.
251
+ # #has_text?:: Returns whether the element has text.
252
+ # #next_element:: Returns the next sibling that is an element.
253
+ # #previous_element:: Returns the previous sibling that is an element.
254
+ # #raw:: Returns whether raw mode is set for the element.
255
+ # #whitespace:: Returns whether whitespace is respected for the element.
256
+ # #ignore_whitespace_nodes:: Returns whether whitespace nodes
257
+ # are to be ignored for the element.
258
+ # #node_type:: Returns symbol <tt>:element</tt>.
259
+ #
260
+ # === One More Method
261
+ #
262
+ # #inspect:: Returns a string representation of the element.
263
+ #
264
+ # === Accessors
265
+ #
266
+ # #elements:: Returns the REXML::Elements object for the element.
267
+ # #attributes:: Returns the REXML::Attributes object for the element.
268
+ # #context:: Returns or sets the context hash for the element.
269
+ #
21
270
class Element < Parent
22
271
include Namespace
23
272
0 commit comments