rexml 3.2.0 → 3.2.5

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rexml might be problematic. Click here for more details.

@@ -0,0 +1,602 @@
1
+ == Class Element
2
+
3
+ Class Element has methods from its superclasses and included modules;
4
+ see:
5
+
6
+ - {Tasks for Parent}[parent_rdoc.html].
7
+ - {Tasks for Child}[child_rdoc.html].
8
+ - {Tasks for Node}[node_rdoc.html].
9
+ - {Module Enumerable}[https://docs.ruby-lang.org/en/master/Enumerable.html].
10
+
11
+ :include: ../tocs/element_toc.rdoc
12
+
13
+ === New Element
14
+
15
+ ==== Task: Create a Default Element
16
+
17
+ Use method
18
+ {Element::new}[../../../../REXML/Element.html#method-c-new]
19
+ with no arguments to create a default element:
20
+
21
+ e = REXML::Element.new
22
+ e.name # => "UNDEFINED"
23
+ e.parent # => nil
24
+ e.context # => nil
25
+
26
+ ==== Task: Create a Named Element
27
+
28
+ Use method
29
+ {Element::new}[../../../../REXML/Element.html#method-c-new]
30
+ with a string name argument
31
+ to create a named element:
32
+
33
+ e = REXML::Element.new('foo')
34
+ e.name # => "foo"
35
+ e.parent # => nil
36
+ e.context # => nil
37
+
38
+ ==== Task: Create an Element with Name and Parent
39
+
40
+ Use method
41
+ {Element::new}[../../../../REXML/Element.html#method-c-new]
42
+ with name and parent arguments
43
+ to create an element with name and parent:
44
+
45
+ p = REXML::Parent.new
46
+ e = REXML::Element.new('foo', p)
47
+ e.name # => "foo"
48
+ e.parent # => #<REXML::Parent @parent=nil, @children=[<foo/>]>
49
+ e.context # => nil
50
+
51
+ ==== Task: Create an Element with Name, Parent, and Context
52
+
53
+ Use method
54
+ {Element::new}[../../../../REXML/Element.html#method-c-new]
55
+ with name, parent, and context arguments
56
+ to create an element with name, parent, and context:
57
+
58
+ p = REXML::Parent.new
59
+ e = REXML::Element.new('foo', p, {compress_whitespace: :all})
60
+ e.name # => "foo"
61
+ e.parent # => #<REXML::Parent @parent=nil, @children=[<foo/>]>
62
+ e.context # => {:compress_whitespace=>:all}
63
+
64
+ ==== Task: Create a Shallow Clone
65
+
66
+ Use method
67
+ {Element#clone}[../../../../REXML/Element.html#method-i-clone]
68
+ to create a shallow clone of an element,
69
+ copying only the name, attributes, and context:
70
+
71
+ e0 = REXML::Element.new('foo', nil, {compress_whitespace: :all})
72
+ e0.add_attribute(REXML::Attribute.new('bar', 'baz'))
73
+ e0.context = {compress_whitespace: :all}
74
+ e1 = e0.clone # => <foo bar='baz'/>
75
+ e1.name # => "foo"
76
+ e1.context # => {:compress_whitespace=>:all}
77
+
78
+ === Attributes
79
+
80
+ ==== Task: Create and Add an Attribute
81
+
82
+ Use method
83
+ {Element#add_attribute}[../../../../REXML/Element.html#method-i-add_attribute]
84
+ to create and add an attribute:
85
+
86
+ e = REXML::Element.new
87
+ e.add_attribute('attr', 'value') # => "value"
88
+ e['attr'] # => "value"
89
+ e.add_attribute('attr', 'VALUE') # => "VALUE"
90
+ e['attr'] # => "VALUE"
91
+
92
+ ==== Task: Add an Existing Attribute
93
+
94
+ Use method
95
+ {Element#add_attribute}[../../../../REXML/Element.html#method-i-add_attribute]
96
+ to add an existing attribute:
97
+
98
+ e = REXML::Element.new
99
+ a = REXML::Attribute.new('attr', 'value')
100
+ e.add_attribute(a)
101
+ e['attr'] # => "value"
102
+ a = REXML::Attribute.new('attr', 'VALUE')
103
+ e.add_attribute(a)
104
+ e['attr'] # => "VALUE"
105
+
106
+ ==== Task: Add Multiple Attributes from a Hash
107
+
108
+ Use method
109
+ {Element#add_attributes}[../../../../REXML/Element.html#method-i-add_attributes]
110
+ to add multiple attributes from a hash:
111
+
112
+ e = REXML::Element.new
113
+ h = {'foo' => 0, 'bar' => 1}
114
+ e.add_attributes(h)
115
+ e['foo'] # => "0"
116
+ e['bar'] # => "1"
117
+
118
+ ==== Task: Add Multiple Attributes from an Array
119
+
120
+ Use method
121
+ {Element#add_attributes}[../../../../REXML/Element.html#method-i-add_attributes]
122
+ to add multiple attributes from an array:
123
+
124
+ e = REXML::Element.new
125
+ a = [['foo', 0], ['bar', 1]]
126
+ e.add_attributes(a)
127
+ e['foo'] # => "0"
128
+ e['bar'] # => "1"
129
+
130
+ ==== Task: Retrieve the Value for an Attribute Name
131
+
132
+ Use method
133
+ {Element#[]}[../../../../REXML/Element.html#method-i-5B-5D]
134
+ to retrieve the value for an attribute name:
135
+
136
+ e = REXML::Element.new
137
+ e.add_attribute('attr', 'value') # => "value"
138
+ e['attr'] # => "value"
139
+
140
+ ==== Task: Retrieve the Attribute Value for a Name and Namespace
141
+
142
+ Use method
143
+ {Element#attribute}[../../../../REXML/Element.html#method-i-attribute]
144
+ to retrieve the value for an attribute name:
145
+
146
+ xml_string = "<root xmlns:a='a' a:x='a:x' x='x'/>"
147
+ d = REXML::Document.new(xml_string)
148
+ e = d.root
149
+ e.attribute("x") # => x='x'
150
+ e.attribute("x", "a") # => a:x='a:x'
151
+
152
+ ==== Task: Delete an Attribute
153
+
154
+ Use method
155
+ {Element#delete_attribute}[../../../../REXML/Element.html#method-i-delete_attribute]
156
+ to remove an attribute:
157
+
158
+ e = REXML::Element.new('foo')
159
+ e.add_attribute('bar', 'baz')
160
+ e.delete_attribute('bar')
161
+ e.delete_attribute('bar')
162
+ e['bar'] # => nil
163
+
164
+ ==== Task: Determine Whether the Element Has Attributes
165
+
166
+ Use method
167
+ {Element#has_attributes?}[../../../../REXML/Element.html#method-i-has_attributes-3F]
168
+ to determine whether the element has attributes:
169
+
170
+ e = REXML::Element.new('foo')
171
+ e.has_attributes? # => false
172
+ e.add_attribute('bar', 'baz')
173
+ e.has_attributes? # => true
174
+
175
+ === Children
176
+
177
+ <em>Element Children</em>
178
+
179
+ ==== Task: Create and Add an Element
180
+
181
+ Use method
182
+ {Element#add_element}[../../../../REXML/Element.html#method-i-add_element]
183
+ to create a new element and add it to this element:
184
+
185
+ e0 = REXML::Element.new('foo')
186
+ e0.add_element('bar')
187
+ e0.children # => [<bar/>]
188
+
189
+ ==== Task: Add an Existing Element
190
+
191
+ Use method
192
+ {Element#add_element}[../../../../REXML/Element.html#method-i-add_element]
193
+ to add an element to this element:
194
+
195
+ e0 = REXML::Element.new('foo')
196
+ e1 = REXML::Element.new('bar')
197
+ e0.add_element(e1)
198
+ e0.children # => [<bar/>]
199
+
200
+ ==== Task: Create and Add an Element with Attributes
201
+
202
+ Use method
203
+ {Element#add_element}[../../../../REXML/Element.html#method-i-add_element]
204
+ to create a new element with attributes, and add it to this element:
205
+
206
+ e0 = REXML::Element.new('foo')
207
+ e0.add_element('bar', {'name' => 'value'})
208
+ e0.children # => [<bar name='value'/>]
209
+
210
+ ==== Task: Add an Existing Element with Added Attributes
211
+
212
+ Use method
213
+ {Element#add_element}[../../../../REXML/Element.html#method-i-add_element]
214
+ to add an element to this element:
215
+
216
+ e0 = REXML::Element.new('foo')
217
+ e1 = REXML::Element.new('bar')
218
+ e0.add_element(e1, {'name' => 'value'})
219
+ e0.children # => [<bar name='value'/>]
220
+
221
+ ==== Task: Delete a Specified Element
222
+
223
+ Use method
224
+ {Element#delete_element}[../../../../REXML/Element.html#method-i-delete_element]
225
+ to remove a specified element from this element:
226
+
227
+ e0 = REXML::Element.new('foo')
228
+ e1 = REXML::Element.new('bar')
229
+ e0.add_element(e1)
230
+ e0.children # => [<bar/>]
231
+ e0.delete_element(e1)
232
+ e0.children # => []
233
+
234
+ ==== Task: Delete an Element by Index
235
+
236
+ Use method
237
+ {Element#delete_element}[../../../../REXML/Element.html#method-i-delete_element]
238
+ to remove an element from this element by index:
239
+
240
+ e0 = REXML::Element.new('foo')
241
+ e1 = REXML::Element.new('bar')
242
+ e0.add_element(e1)
243
+ e0.children # => [<bar/>]
244
+ e0.delete_element(1)
245
+ e0.children # => []
246
+
247
+ ==== Task: Delete an Element by XPath
248
+
249
+ Use method
250
+ {Element#delete_element}[../../../../REXML/Element.html#method-i-delete_element]
251
+ to remove an element from this element by XPath:
252
+
253
+ e0 = REXML::Element.new('foo')
254
+ e1 = REXML::Element.new('bar')
255
+ e0.add_element(e1)
256
+ e0.children # => [<bar/>]
257
+ e0.delete_element('//bar/')
258
+ e0.children # => []
259
+
260
+ ==== Task: Determine Whether Element Children
261
+
262
+ Use method
263
+ {Element#has_elements?}[../../../../REXML/Element.html#method-i-has_elements-3F]
264
+ to determine whether the element has element children:
265
+
266
+ e0 = REXML::Element.new('foo')
267
+ e0.has_elements? # => false
268
+ e0.add_element(REXML::Element.new('bar'))
269
+ e0.has_elements? # => true
270
+
271
+ ==== Task: Get Element Descendants by XPath
272
+
273
+ Use method
274
+ {Element#get_elements}[../../../../REXML/Element.html#method-i-get_elements]
275
+ to fetch all element descendant children by XPath:
276
+
277
+ xml_string = <<-EOT
278
+ <root>
279
+ <a level='1'>
280
+ <a level='2'/>
281
+ </a>
282
+ </root>
283
+ EOT
284
+ d = REXML::Document.new(xml_string)
285
+ d.root.get_elements('//a') # => [<a level='1'> ... </>, <a level='2'/>]
286
+
287
+ ==== Task: Get Next Element Sibling
288
+
289
+ Use method
290
+ {Element#next_element}[../../../../REXML/Element.html#method-i-next_element]
291
+ to retrieve the next element sibling:
292
+
293
+ d = REXML::Document.new '<a><b/>text<c/></a>'
294
+ d.root.elements['b'].next_element #-> <c/>
295
+ d.root.elements['c'].next_element #-> nil
296
+
297
+ ==== Task: Get Previous Element Sibling
298
+
299
+ Use method
300
+ {Element#previous_element}[../../../../REXML/Element.html#method-i-previous_element]
301
+ to retrieve the previous element sibling:
302
+
303
+ d = REXML::Document.new '<a><b/>text<c/></a>'
304
+ d.root.elements['c'].previous_element #-> <b/>
305
+ d.root.elements['b'].previous_element #-> nil
306
+
307
+ <em>Text Children</em>
308
+
309
+ ==== Task: Add a Text Node
310
+
311
+ Use method
312
+ {Element#add_text}[../../../../REXML/Element.html#method-i-add_text]
313
+ to add a text node to the element:
314
+
315
+ d = REXML::Document.new('<a>foo<b/>bar</a>')
316
+ e = d.root
317
+ e.add_text(REXML::Text.new('baz'))
318
+ e.to_a # => ["foo", <b/>, "bar", "baz"]
319
+ e.add_text(REXML::Text.new('baz'))
320
+ e.to_a # => ["foo", <b/>, "bar", "baz", "baz"]
321
+
322
+ ==== Task: Replace the First Text Node
323
+
324
+ Use method
325
+ {Element#text=}[../../../../REXML/Element.html#method-i-text-3D]
326
+ to replace the first text node in the element:
327
+
328
+ d = REXML::Document.new('<root><a/>text<b/>more<c/></root>')
329
+ e = d.root
330
+ e.to_a # => [<a/>, "text", <b/>, "more", <c/>]
331
+ e.text = 'oops'
332
+ e.to_a # => [<a/>, "oops", <b/>, "more", <c/>]
333
+
334
+ ==== Task: Remove the First Text Node
335
+
336
+ Use method
337
+ {Element#text=}[../../../../REXML/Element.html#method-i-text-3D]
338
+ to remove the first text node in the element:
339
+
340
+ d = REXML::Document.new('<root><a/>text<b/>more<c/></root>')
341
+ e = d.root
342
+ e.to_a # => [<a/>, "text", <b/>, "more", <c/>]
343
+ e.text = nil
344
+ e.to_a # => [<a/>, <b/>, "more", <c/>]
345
+
346
+ ==== Task: Retrieve the First Text Node
347
+
348
+ Use method
349
+ {Element#get_text}[../../../../REXML/Element.html#method-i-get_text]
350
+ to retrieve the first text node in the element:
351
+
352
+ d = REXML::Document.new('<root><a/>text<b/>more<c/></root>')
353
+ e = d.root
354
+ e.to_a # => [<a/>, "text", <b/>, "more", <c/>]
355
+ e.get_text # => "text"
356
+
357
+ ==== Task: Retrieve a Specific Text Node
358
+
359
+ Use method
360
+ {Element#get_text}[../../../../REXML/Element.html#method-i-get_text]
361
+ to retrieve the first text node in a specified element:
362
+
363
+ d = REXML::Document.new "<root>some text <b>this is bold!</b> more text</root>"
364
+ e = d.root
365
+ e.get_text('//root') # => "some text "
366
+ e.get_text('//b') # => "this is bold!"
367
+
368
+ ==== Task: Determine Whether the Element has Text Nodes
369
+
370
+ Use method
371
+ {Element#has_text?}[../../../../REXML/Element.html#method-i-has_text-3F]
372
+ to determine whethe the element has text:
373
+
374
+ e = REXML::Element.new('foo')
375
+ e.has_text? # => false
376
+ e.add_text('bar')
377
+ e.has_text? # => true
378
+
379
+ <em>Other Children</em>
380
+
381
+ ==== Task: Get the Child at a Given Index
382
+
383
+ Use method
384
+ {Element#[]}[../../../../REXML/Element.html#method-i-5B-5D]
385
+ to retrieve the child at a given index:
386
+
387
+ d = REXML::Document.new '><root><a/>text<b/>more<c/></root>'
388
+ e = d.root
389
+ e[0] # => <a/>
390
+ e[1] # => "text"
391
+ e[2] # => <b/>
392
+
393
+ ==== Task: Get All CDATA Children
394
+
395
+ Use method
396
+ {Element#cdatas}[../../../../REXML/Element.html#method-i-cdatas]
397
+ to retrieve all CDATA children:
398
+
399
+ xml_string = <<-EOT
400
+ <root>
401
+ <![CDATA[foo]]>
402
+ <![CDATA[bar]]>
403
+ </root>
404
+ EOT
405
+ d = REXML::Document.new(xml_string)
406
+ d.root.cdatas # => ["foo", "bar"]
407
+
408
+ ==== Task: Get All Comment Children
409
+
410
+ Use method
411
+ {Element#comments}[../../../../REXML/Element.html#method-i-comments]
412
+ to retrieve all comment children:
413
+
414
+ xml_string = <<-EOT
415
+ <root>
416
+ <!--foo-->
417
+ <!--bar-->
418
+ </root>
419
+ EOT
420
+ d = REXML::Document.new(xml_string)
421
+ d.root.comments.map {|comment| comment.to_s } # => ["foo", "bar"]
422
+
423
+ ==== Task: Get All Processing Instruction Children
424
+
425
+ Use method
426
+ {Element#instructions}[../../../../REXML/Element.html#method-i-instructions]
427
+ to retrieve all processing instruction children:
428
+
429
+ xml_string = <<-EOT
430
+ <root>
431
+ <?target0 foo?>
432
+ <?target1 bar?>
433
+ </root>
434
+ EOT
435
+ d = REXML::Document.new(xml_string)
436
+ instructions = d.root.instructions.map {|instruction| instruction.to_s }
437
+ instructions # => ["<?target0 foo?>", "<?target1 bar?>"]
438
+
439
+ ==== Task: Get All Text Children
440
+
441
+ Use method
442
+ {Element#texts}[../../../../REXML/Element.html#method-i-texts]
443
+ to retrieve all text children:
444
+
445
+ xml_string = '<root><a/>text<b/>more<c/></root>'
446
+ d = REXML::Document.new(xml_string)
447
+ d.root.texts # => ["text", "more"]
448
+
449
+ === Namespaces
450
+
451
+ ==== Task: Add a Namespace
452
+
453
+ Use method
454
+ {Element#add_namespace}[../../../../REXML/Element.html#method-i-add_namespace]
455
+ to add a namespace to the element:
456
+
457
+ e = REXML::Element.new('foo')
458
+ e.add_namespace('bar')
459
+ e.namespaces # => {"xmlns"=>"bar"}
460
+
461
+ ==== Task: Delete the Default Namespace
462
+
463
+ Use method
464
+ {Element#delete_namespace}[../../../../REXML/Element.html#method-i-delete_namespace]
465
+ to remove the default namespace from the element:
466
+
467
+ d = REXML::Document.new "<a xmlns:foo='bar' xmlns='twiddle'/>"
468
+ d.to_s # => "<a xmlns:foo='bar' xmlns='twiddle'/>"
469
+ d.root.delete_namespace # => <a xmlns:foo='bar'/>
470
+ d.to_s # => "<a xmlns:foo='bar'/>"
471
+
472
+ ==== Task: Delete a Specific Namespace
473
+
474
+ Use method
475
+ {Element#delete_namespace}[../../../../REXML/Element.html#method-i-delete_namespace]
476
+ to remove a specific namespace from the element:
477
+
478
+ d = REXML::Document.new "<a xmlns:foo='bar' xmlns='twiddle'/>"
479
+ d.to_s # => "<a xmlns:foo='bar' xmlns='twiddle'/>"
480
+ d.root.delete_namespace # => <a xmlns:foo='bar'/>
481
+ d.to_s # => "<a xmlns:foo='bar'/>"
482
+ d.root.delete_namespace('foo')
483
+ d.to_s # => "<a/>"
484
+
485
+ ==== Task: Get a Namespace URI
486
+
487
+ Use method
488
+ {Element#namespace}[../../../../REXML/Element.html#method-i-namespace]
489
+ to retrieve a speficic namespace URI for the element:
490
+
491
+ xml_string = <<-EOT
492
+ <root>
493
+ <a xmlns='1' xmlns:y='2'>
494
+ <b/>
495
+ <c xmlns:z='3'/>
496
+ </a>
497
+ </root>
498
+ EOT
499
+ d = REXML::Document.new(xml_string)
500
+ b = d.elements['//b']
501
+ b.namespace # => "1"
502
+ b.namespace('y') # => "2"
503
+
504
+ ==== Task: Retrieve Namespaces
505
+
506
+ Use method
507
+ {Element#namespaces}[../../../../REXML/Element.html#method-i-namespaces]
508
+ to retrieve all namespaces for the element:
509
+
510
+ xml_string = '<a xmlns="foo" xmlns:x="bar" xmlns:y="twee" z="glorp"/>'
511
+ d = REXML::Document.new(xml_string)
512
+ d.root.attributes.namespaces # => {"xmlns"=>"foo", "x"=>"bar", "y"=>"twee"}
513
+
514
+ ==== Task: Retrieve Namespace Prefixes
515
+
516
+ Use method
517
+ {Element#prefixes}[../../../../REXML/Element.html#method-i-prefixes]
518
+ to retrieve all prefixes (namespace names) for the element:
519
+
520
+ xml_string = <<-EOT
521
+ <root>
522
+ <a xmlns:x='1' xmlns:y='2'>
523
+ <b/>
524
+ <c xmlns:z='3'/>
525
+ </a>
526
+ </root>
527
+ EOT
528
+ d = REXML::Document.new(xml_string, {compress_whitespace: :all})
529
+ d.elements['//a'].prefixes # => ["x", "y"]
530
+ d.elements['//b'].prefixes # => ["x", "y"]
531
+ d.elements['//c'].prefixes # => ["x", "y", "z"]
532
+
533
+ === Iteration
534
+
535
+ ==== Task: Iterate Over Elements
536
+
537
+ Use method
538
+ {Element#each_element}[../../../../REXML/Element.html#method-i-each_element]
539
+ to iterate over element children:
540
+
541
+ d = REXML::Document.new '<a><b>b</b><c>b</c><d>d</d><e/></a>'
542
+ d.root.each_element {|e| p e }
543
+
544
+ Output:
545
+
546
+ <b> ... </>
547
+ <c> ... </>
548
+ <d> ... </>
549
+ <e/>
550
+
551
+ ==== Task: Iterate Over Elements Having a Specified Attribute
552
+
553
+ Use method
554
+ {Element#each_element_with_attribute}[../../../../REXML/Element.html#method-i-each_element_with_attribute]
555
+ to iterate over element children that have a specified attribute:
556
+
557
+ d = REXML::Document.new '<a><b id="1"/><c id="2"/><d id="1"/><e/></a>'
558
+ a = d.root
559
+ a.each_element_with_attribute('id') {|e| p e }
560
+
561
+ Output:
562
+
563
+ <b id='1'/>
564
+ <c id='2'/>
565
+ <d id='1'/>
566
+
567
+ ==== Task: Iterate Over Elements Having a Specified Attribute and Value
568
+
569
+ Use method
570
+ {Element#each_element_with_attribute}[../../../../REXML/Element.html#method-i-each_element_with_attribute]
571
+ to iterate over element children that have a specified attribute and value:
572
+
573
+ d = REXML::Document.new '<a><b id="1"/><c id="2"/><d id="1"/><e/></a>'
574
+ a = d.root
575
+ a.each_element_with_attribute('id', '1') {|e| p e }
576
+
577
+ Output:
578
+
579
+ <b id='1'/>
580
+ <d id='1'/>
581
+
582
+ ==== Task: Iterate Over Elements Having Specified Text
583
+
584
+ Use method
585
+ {Element#each_element_with_text}[../../../../REXML/Element.html#method-i-each_element_with_text]
586
+ to iterate over element children that have specified text:
587
+
588
+
589
+ === Context
590
+
591
+ #whitespace
592
+ #ignore_whitespace_nodes
593
+ #raw
594
+
595
+ === Other Getters
596
+
597
+ #document
598
+ #root
599
+ #root_node
600
+ #node_type
601
+ #xpath
602
+ #inspect