summaryrefslogtreecommitdiff
path: root/vendor/bundle/ruby/3.4.0/gems/rexml-3.4.4/doc/rexml/tasks/rdoc/element.rdoc
blob: 4b3609b02660459db7423f40d1bd8e9589aeda7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
== Class Element

Class Element has methods from its superclasses and included modules;
see:

- {Tasks for Parent}[parent_rdoc.html].
- {Tasks for Child}[child_rdoc.html].
- {Tasks for Node}[node_rdoc.html].
- {Module Enumerable}[https://docs.ruby-lang.org/en/master/Enumerable.html].

:include: ../tocs/element_toc.rdoc

=== New Element

==== Task: Create a Default Element

Use method
{Element::new}[../../../../REXML/Element.html#method-c-new]
with no arguments to create a default element:

  e = REXML::Element.new
  e.name    # => "UNDEFINED"
  e.parent  # => nil
  e.context # => nil

==== Task: Create a Named Element

Use method
{Element::new}[../../../../REXML/Element.html#method-c-new]
with a string name argument
to create a named element:

  e = REXML::Element.new('foo')
  e.name    # => "foo"
  e.parent  # => nil
  e.context # => nil

==== Task: Create an Element with Name and Parent

Use method
{Element::new}[../../../../REXML/Element.html#method-c-new]
with name and parent arguments
to create an element with name and parent:

  p = REXML::Parent.new
  e = REXML::Element.new('foo', p)
  e.name    # => "foo"
  e.parent  # => #<REXML::Parent @parent=nil, @children=[<foo/>]>
  e.context # => nil

==== Task: Create an Element with Name, Parent, and Context

Use method
{Element::new}[../../../../REXML/Element.html#method-c-new]
with name, parent, and context arguments
to create an element with name, parent, and context:

  p = REXML::Parent.new
  e = REXML::Element.new('foo', p, {compress_whitespace: :all})
  e.name    # => "foo"
  e.parent  # => #<REXML::Parent @parent=nil, @children=[<foo/>]>
  e.context # => {:compress_whitespace=>:all}

==== Task: Create a Shallow Clone

Use method
{Element#clone}[../../../../REXML/Element.html#method-i-clone]
to create a shallow clone of an element,
copying only the name, attributes, and context:

  e0 = REXML::Element.new('foo', nil, {compress_whitespace: :all})
  e0.add_attribute(REXML::Attribute.new('bar', 'baz'))
  e0.context = {compress_whitespace: :all}
  e1 = e0.clone # => <foo bar='baz'/>
  e1.name # => "foo"
  e1.context # => {:compress_whitespace=>:all}

=== Attributes

==== Task: Create and Add an Attribute

Use method
{Element#add_attribute}[../../../../REXML/Element.html#method-i-add_attribute]
to create and add an attribute:

  e = REXML::Element.new
  e.add_attribute('attr', 'value') # => "value"
  e['attr'] # => "value"
  e.add_attribute('attr', 'VALUE') # => "VALUE"
  e['attr'] # => "VALUE"

==== Task: Add an Existing Attribute

Use method
{Element#add_attribute}[../../../../REXML/Element.html#method-i-add_attribute]
to add an existing attribute:

  e = REXML::Element.new
  a = REXML::Attribute.new('attr', 'value')
  e.add_attribute(a)
  e['attr'] # => "value"
  a = REXML::Attribute.new('attr', 'VALUE')
  e.add_attribute(a)
  e['attr'] # => "VALUE"

==== Task: Add Multiple Attributes from a Hash

Use method
{Element#add_attributes}[../../../../REXML/Element.html#method-i-add_attributes]
to add multiple attributes from a hash:

  e = REXML::Element.new
  h = {'foo' => 0, 'bar' => 1}
  e.add_attributes(h)
  e['foo'] # => "0"
  e['bar'] # => "1"

==== Task: Add Multiple Attributes from an Array

Use method
{Element#add_attributes}[../../../../REXML/Element.html#method-i-add_attributes]
to add multiple attributes from an array:

  e = REXML::Element.new
  a = [['foo', 0], ['bar', 1]]
  e.add_attributes(a)
  e['foo'] # => "0"
  e['bar'] # => "1"

==== Task: Retrieve the Value for an Attribute Name

Use method
{Element#[]}[../../../../REXML/Element.html#method-i-5B-5D]
to retrieve the value for an attribute name:

  e = REXML::Element.new
  e.add_attribute('attr', 'value') # => "value"
  e['attr'] # => "value"

==== Task: Retrieve the Attribute Value for a Name and Namespace

Use method
{Element#attribute}[../../../../REXML/Element.html#method-i-attribute]
to retrieve the value for an attribute name:

  xml_string = "<root xmlns:a='a' a:x='a:x' x='x'/>"
  d = REXML::Document.new(xml_string)
  e = d.root
  e.attribute("x")      # => x='x'
  e.attribute("x", "a") # => a:x='a:x'

==== Task: Delete an Attribute

Use method
{Element#delete_attribute}[../../../../REXML/Element.html#method-i-delete_attribute]
to remove an attribute:

  e = REXML::Element.new('foo')
  e.add_attribute('bar', 'baz')
  e.delete_attribute('bar')
  e.delete_attribute('bar')
  e['bar'] # => nil

==== Task: Determine Whether the Element Has Attributes

Use method
{Element#has_attributes?}[../../../../REXML/Element.html#method-i-has_attributes-3F]
to determine whether the element has attributes:

  e = REXML::Element.new('foo')
  e.has_attributes? # => false
  e.add_attribute('bar', 'baz')
  e.has_attributes? # => true

=== Children

<em>Element Children</em>

==== Task: Create and Add an Element

Use method
{Element#add_element}[../../../../REXML/Element.html#method-i-add_element]
to create a new element and add it to this element:

  e0 = REXML::Element.new('foo')
  e0.add_element('bar')
  e0.children # => [<bar/>]

==== Task: Add an Existing Element

Use method
{Element#add_element}[../../../../REXML/Element.html#method-i-add_element]
to add an element to this element:

  e0 = REXML::Element.new('foo')
  e1 = REXML::Element.new('bar')
  e0.add_element(e1)
  e0.children # => [<bar/>]

==== Task: Create and Add an Element with Attributes

Use method
{Element#add_element}[../../../../REXML/Element.html#method-i-add_element]
to create a new element with attributes, and add it to this element:

  e0 = REXML::Element.new('foo')
  e0.add_element('bar', {'name' => 'value'})
  e0.children # => [<bar name='value'/>]

==== Task: Add an Existing Element with Added Attributes

Use method
{Element#add_element}[../../../../REXML/Element.html#method-i-add_element]
to add an element to this element:

  e0 = REXML::Element.new('foo')
  e1 = REXML::Element.new('bar')
  e0.add_element(e1, {'name' => 'value'})
  e0.children # => [<bar name='value'/>]

==== Task: Delete a Specified Element

Use method
{Element#delete_element}[../../../../REXML/Element.html#method-i-delete_element]
to remove a specified element from this element:

  e0 = REXML::Element.new('foo')
  e1 = REXML::Element.new('bar')
  e0.add_element(e1)
  e0.children # => [<bar/>]
  e0.delete_element(e1)
  e0.children # => []

==== Task: Delete an Element by Index

Use method
{Element#delete_element}[../../../../REXML/Element.html#method-i-delete_element]
to remove an element from this element by index:

  e0 = REXML::Element.new('foo')
  e1 = REXML::Element.new('bar')
  e0.add_element(e1)
  e0.children # => [<bar/>]
  e0.delete_element(1)
  e0.children # => []

==== Task: Delete an Element by XPath

Use method
{Element#delete_element}[../../../../REXML/Element.html#method-i-delete_element]
to remove an element from this element by XPath:

  e0 = REXML::Element.new('foo')
  e1 = REXML::Element.new('bar')
  e0.add_element(e1)
  e0.children # => [<bar/>]
  e0.delete_element('//bar/')
  e0.children # => []

==== Task: Determine Whether Element Children

Use method
{Element#has_elements?}[../../../../REXML/Element.html#method-i-has_elements-3F]
to determine whether the element has element children:

  e0 = REXML::Element.new('foo')
  e0.has_elements? # => false
  e0.add_element(REXML::Element.new('bar'))
  e0.has_elements? # => true

==== Task: Get Element Descendants by XPath

Use method
{Element#get_elements}[../../../../REXML/Element.html#method-i-get_elements]
to fetch all element descendant children by XPath:

  xml_string = <<-EOT
  <root>
    <a level='1'>
      <a level='2'/>
    </a>
  </root>
  EOT
  d = REXML::Document.new(xml_string)
  d.root.get_elements('//a') # => [<a level='1'> ... </>, <a level='2'/>]

==== Task: Get Next Element Sibling

Use method
{Element#next_element}[../../../../REXML/Element.html#method-i-next_element]
to retrieve the next element sibling:

  d = REXML::Document.new '<a><b/>text<c/></a>'
  d.root.elements['b'].next_element #-> <c/>
  d.root.elements['c'].next_element #-> nil

==== Task: Get Previous Element Sibling

Use method
{Element#previous_element}[../../../../REXML/Element.html#method-i-previous_element]
to retrieve the previous element sibling:

  d = REXML::Document.new '<a><b/>text<c/></a>'
  d.root.elements['c'].previous_element #-> <b/>
  d.root.elements['b'].previous_element #-> nil

<em>Text Children</em>

==== Task: Add a Text Node

Use method
{Element#add_text}[../../../../REXML/Element.html#method-i-add_text]
to add a text node to the element:

  d = REXML::Document.new('<a>foo<b/>bar</a>')
  e = d.root
  e.add_text(REXML::Text.new('baz'))
  e.to_a # => ["foo", <b/>, "bar", "baz"]
  e.add_text(REXML::Text.new('baz'))
  e.to_a # => ["foo", <b/>, "bar", "baz", "baz"]

==== Task: Replace the First Text Node

Use method
{Element#text=}[../../../../REXML/Element.html#method-i-text-3D]
to replace the first text node in the element:

  d = REXML::Document.new('<root><a/>text<b/>more<c/></root>')
  e = d.root
  e.to_a # => [<a/>, "text", <b/>, "more", <c/>]
  e.text = 'oops'
  e.to_a # => [<a/>, "oops", <b/>, "more", <c/>]

==== Task: Remove the First Text Node

Use method
{Element#text=}[../../../../REXML/Element.html#method-i-text-3D]
to remove the first text node in the element:

  d = REXML::Document.new('<root><a/>text<b/>more<c/></root>')
  e = d.root
  e.to_a # => [<a/>, "text", <b/>, "more", <c/>]
  e.text = nil
  e.to_a # => [<a/>, <b/>, "more", <c/>]

==== Task: Retrieve the First Text Node

Use method
{Element#get_text}[../../../../REXML/Element.html#method-i-get_text]
to retrieve the first text node in the element:

  d = REXML::Document.new('<root><a/>text<b/>more<c/></root>')
  e = d.root
  e.to_a # => [<a/>, "text", <b/>, "more", <c/>]
  e.get_text # => "text"

==== Task: Retrieve a Specific Text Node

Use method
{Element#get_text}[../../../../REXML/Element.html#method-i-get_text]
to retrieve the first text node in a specified element:

  d = REXML::Document.new "<root>some text <b>this is bold!</b> more text</root>"
  e = d.root
  e.get_text('//root') # => "some text "
  e.get_text('//b')    # => "this is bold!"

==== Task: Determine Whether the Element has Text Nodes

Use method
{Element#has_text?}[../../../../REXML/Element.html#method-i-has_text-3F]
to determine whether the element has text:

  e = REXML::Element.new('foo')
  e.has_text? # => false
  e.add_text('bar')
  e.has_text? # => true

<em>Other Children</em>

==== Task: Get the Child at a Given Index

Use method
{Element#[]}[../../../../REXML/Element.html#method-i-5B-5D]
to retrieve the child at a given index:

  d = REXML::Document.new '><root><a/>text<b/>more<c/></root>'
  e = d.root
  e[0] # => <a/>
  e[1] # => "text"
  e[2] # => <b/>

==== Task: Get All CDATA Children

Use method
{Element#cdatas}[../../../../REXML/Element.html#method-i-cdatas]
to retrieve all CDATA children:

  xml_string = <<-EOT
    <root>
      <![CDATA[foo]]>
      <![CDATA[bar]]>
    </root>
  EOT
  d = REXML::Document.new(xml_string)
  d.root.cdatas # => ["foo", "bar"]

==== Task: Get All Comment Children

Use method
{Element#comments}[../../../../REXML/Element.html#method-i-comments]
to retrieve all comment children:

  xml_string = <<-EOT
    <root>
      <!--foo-->
      <!--bar-->
    </root>
  EOT
  d = REXML::Document.new(xml_string)
  d.root.comments.map {|comment| comment.to_s } # => ["foo", "bar"]

==== Task: Get All Processing Instruction Children

Use method
{Element#instructions}[../../../../REXML/Element.html#method-i-instructions]
to retrieve all processing instruction children:

  xml_string = <<-EOT
    <root>
      <?target0 foo?>
      <?target1 bar?>
    </root>
  EOT
  d = REXML::Document.new(xml_string)
  instructions = d.root.instructions.map {|instruction| instruction.to_s }
  instructions # => ["<?target0 foo?>", "<?target1 bar?>"]

==== Task: Get All Text Children

Use method
{Element#texts}[../../../../REXML/Element.html#method-i-texts]
to retrieve all text children:

  xml_string = '<root><a/>text<b/>more<c/></root>'
  d = REXML::Document.new(xml_string)
  d.root.texts # => ["text", "more"]

=== Namespaces

==== Task: Add a Namespace

Use method
{Element#add_namespace}[../../../../REXML/Element.html#method-i-add_namespace]
to add a namespace to the element:

  e = REXML::Element.new('foo')
  e.add_namespace('bar')
  e.namespaces # => {"xmlns"=>"bar"}

==== Task: Delete the Default Namespace

Use method
{Element#delete_namespace}[../../../../REXML/Element.html#method-i-delete_namespace]
to remove the default namespace from the element:

  d = REXML::Document.new "<a xmlns:foo='bar' xmlns='twiddle'/>"
  d.to_s # => "<a xmlns:foo='bar' xmlns='twiddle'/>"
  d.root.delete_namespace # => <a xmlns:foo='bar'/>
  d.to_s # => "<a xmlns:foo='bar'/>"

==== Task: Delete a Specific Namespace

Use method
{Element#delete_namespace}[../../../../REXML/Element.html#method-i-delete_namespace]
to remove a specific namespace from the element:

  d = REXML::Document.new "<a xmlns:foo='bar' xmlns='twiddle'/>"
  d.to_s # => "<a xmlns:foo='bar' xmlns='twiddle'/>"
  d.root.delete_namespace # => <a xmlns:foo='bar'/>
  d.to_s # => "<a xmlns:foo='bar'/>"
  d.root.delete_namespace('foo')
  d.to_s # => "<a/>"

==== Task: Get a Namespace URI

Use method
{Element#namespace}[../../../../REXML/Element.html#method-i-namespace]
to retrieve a specific namespace URI for the element:

  xml_string = <<-EOT
    <root>
       <a xmlns='1' xmlns:y='2'>
         <b/>
         <c xmlns:z='3'/>
       </a>
    </root>
  EOT
  d = REXML::Document.new(xml_string)
  b = d.elements['//b']
  b.namespace      # => "1"
  b.namespace('y') # => "2"

==== Task: Retrieve Namespaces

Use method
{Element#namespaces}[../../../../REXML/Element.html#method-i-namespaces]
to retrieve all namespaces for the element:

  xml_string = '<a xmlns="foo" xmlns:x="bar" xmlns:y="twee" z="glorp"/>'
  d = REXML::Document.new(xml_string)
  d.root.attributes.namespaces # => {"xmlns"=>"foo", "x"=>"bar", "y"=>"twee"}

==== Task: Retrieve Namespace Prefixes

Use method
{Element#prefixes}[../../../../REXML/Element.html#method-i-prefixes]
to retrieve all prefixes (namespace names) for the element:

  xml_string = <<-EOT
    <root>
       <a xmlns:x='1' xmlns:y='2'>
         <b/>
         <c xmlns:z='3'/>
       </a>
    </root>
  EOT
  d = REXML::Document.new(xml_string, {compress_whitespace: :all})
  d.elements['//a'].prefixes # => ["x", "y"]
  d.elements['//b'].prefixes # => ["x", "y"]
  d.elements['//c'].prefixes # => ["x", "y", "z"]

=== Iteration

==== Task: Iterate Over Elements

Use method
{Element#each_element}[../../../../REXML/Element.html#method-i-each_element]
to iterate over element children:

  d = REXML::Document.new '<a><b>b</b><c>b</c><d>d</d><e/></a>'
  d.root.each_element {|e| p e }

Output:

  <b> ... </>
  <c> ... </>
  <d> ... </>
  <e/>

==== Task: Iterate Over Elements Having a Specified Attribute

Use method
{Element#each_element_with_attribute}[../../../../REXML/Element.html#method-i-each_element_with_attribute]
to iterate over element children that have a specified attribute:

  d = REXML::Document.new '<a><b id="1"/><c id="2"/><d id="1"/><e/></a>'
  a = d.root
  a.each_element_with_attribute('id') {|e| p e }

Output:

  <b id='1'/>
  <c id='2'/>
  <d id='1'/>

==== Task: Iterate Over Elements Having a Specified Attribute and Value

Use method
{Element#each_element_with_attribute}[../../../../REXML/Element.html#method-i-each_element_with_attribute]
to iterate over element children that have a specified attribute and value:

  d = REXML::Document.new '<a><b id="1"/><c id="2"/><d id="1"/><e/></a>'
  a = d.root
  a.each_element_with_attribute('id', '1') {|e| p e }

Output:

  <b id='1'/>
  <d id='1'/>

==== Task: Iterate Over Elements Having Specified Text

Use method
{Element#each_element_with_text}[../../../../REXML/Element.html#method-i-each_element_with_text]
to iterate over element children that have specified text:


=== Context

#whitespace
#ignore_whitespace_nodes
#raw

=== Other Getters

#document
#root
#root_node
#node_type
#xpath
#inspect