Class: XmlUtils::Element
Instance Attribute Summary collapse
Attributes inherited from Node
#parent
Instance Method Summary
collapse
Methods inherited from ChildNode
#write
Methods inherited from Node
#deep_clone, #document, #each, #next_sibling, #previous_sibling, #remove, #root, #to_s
Constructor Details
#initialize(name, parent = nil) ⇒ Element
Returns a new instance of Element.
354
355
356
357
358
359
360
361
362
|
# File 'lib/xmlutils/node.rb', line 354
def initialize(name, parent = nil)
super()
@parent = parent
@name = name.to_s
@attributes = {}
@children = []
@prefixes = {}
@parent.add(self) if @parent && @parent.respond_to?(:add)
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
352
353
354
|
# File 'lib/xmlutils/node.rb', line 352
def attributes
@attributes
end
|
#children ⇒ Object
Returns the value of attribute children.
352
353
354
|
# File 'lib/xmlutils/node.rb', line 352
def children
@children
end
|
#name ⇒ Object
Returns the value of attribute name.
352
353
354
|
# File 'lib/xmlutils/node.rb', line 352
def name
@name
end
|
#prefixes ⇒ Object
Returns the value of attribute prefixes.
352
353
354
|
# File 'lib/xmlutils/node.rb', line 352
def prefixes
@prefixes
end
|
Instance Method Details
#[](name_or_index) ⇒ Object
391
392
393
394
395
396
397
398
|
# File 'lib/xmlutils/node.rb', line 391
def [](name_or_index)
if name_or_index.is_a?(Integer)
@children[name_or_index]
else
attr = attribute(name_or_index.to_s)
attr ? attr.value : nil
end
end
|
#[]=(name_or_index, value) ⇒ Object
400
401
402
403
404
405
406
|
# File 'lib/xmlutils/node.rb', line 400
def []=(name_or_index, value)
if name_or_index.is_a?(Integer)
@children[name_or_index] = value
else
add_attribute(name_or_index.to_s, value.to_s)
end
end
|
#add(element) ⇒ Object
Also known as:
<<
375
376
377
378
379
|
# File 'lib/xmlutils/node.rb', line 375
def add(element)
element.parent = self
@children << element
element
end
|
#add_attribute(name, value) ⇒ Object
Also known as:
set_attribute
408
409
410
411
412
413
|
# File 'lib/xmlutils/node.rb', line 408
def add_attribute(name, value)
attr = value.is_a?(Attribute) ? value : Attribute.new(name, value)
attr.element = self
@attributes[name.to_s] = attr
attr
end
|
#add_text(text) ⇒ Object
448
449
450
451
452
|
# File 'lib/xmlutils/node.rb', line 448
def add_text(text)
t = text.is_a?(Text) ? text : Text.new(text)
add(t)
t
end
|
#attribute(name) ⇒ Object
420
421
422
|
# File 'lib/xmlutils/node.rb', line 420
def attribute(name)
@attributes[name.to_s]
end
|
#clone ⇒ Object
368
369
370
371
372
373
|
# File 'lib/xmlutils/node.rb', line 368
def clone
cloned = Element.new(@name)
@attributes.each { |k, v| cloned.add_attribute(k, v.clone) }
@children.each { |c| cloned.add(c.clone) }
cloned
end
|
#delete(element) ⇒ Object
382
383
384
385
|
# File 'lib/xmlutils/node.rb', line 382
def delete(element)
@children.delete(element)
element.parent = nil if element.respond_to?(:parent=)
end
|
#delete_at(index) ⇒ Object
387
388
389
|
# File 'lib/xmlutils/node.rb', line 387
def delete_at(index)
@children.delete_at(index)
end
|
#delete_attribute(name) ⇒ Object
416
417
418
|
# File 'lib/xmlutils/node.rb', line 416
def delete_attribute(name)
@attributes.delete(name.to_s)
end
|
#each_element(&block) ⇒ Object
428
429
430
431
|
# File 'lib/xmlutils/node.rb', line 428
def each_element(&block)
return to_enum(:each_element) unless block_given?
@children.select { |c| c.is_a?(Element) }.each(&block)
end
|
#each_recursive(&block) ⇒ Object
458
459
460
461
462
463
464
|
# File 'lib/xmlutils/node.rb', line 458
def each_recursive(&block)
return to_enum(:each_recursive) unless block_given?
@children.each do |child|
block.call(child)
child.each_recursive(&block) if child.is_a?(Element)
end
end
|
#elements ⇒ Object
433
434
435
|
# File 'lib/xmlutils/node.rb', line 433
def elements
@children.select { |c| c.is_a?(Element) }
end
|
#expand(name) ⇒ Object
489
490
491
492
493
494
|
# File 'lib/xmlutils/node.rb', line 489
def expand(name)
return name unless name.include?(':')
p, local = name.split(':')
ns = namespace(p)
ns ? "{#{ns}}#{local}" : name
end
|
#get_elements(name) ⇒ Object
454
455
456
|
# File 'lib/xmlutils/node.rb', line 454
def get_elements(name)
@children.select { |c| c.is_a?(Element) && c.name == name }
end
|
#get_text(path = nil) ⇒ Object
443
444
445
446
|
# File 'lib/xmlutils/node.rb', line 443
def get_text(path = nil)
return XPath.match(self, path).first if path
@children.find { |c| c.is_a?(Text) || c.is_a?(CData) }
end
|
#has_attribute?(name) ⇒ Boolean
424
425
426
|
# File 'lib/xmlutils/node.rb', line 424
def has_attribute?(name)
@attributes.key?(name.to_s)
end
|
#inspect ⇒ Object
515
516
517
518
519
|
# File 'lib/xmlutils/node.rb', line 515
def inspect
attrs = @attributes.map { |k, v| "#{k}=#{v.to_s.inspect}" }.join(' ')
attrs = " #{attrs}" unless attrs.empty?
"<#{@name}#{attrs}>"
end
|
#namespace(prefix = nil) ⇒ Object
478
479
480
481
482
483
|
# File 'lib/xmlutils/node.rb', line 478
def namespace(prefix = nil)
prefix ||= self.prefix
ns = namespaces
return ns[prefix] if ns.key?(prefix)
@parent.respond_to?(:namespace) ? @parent.namespace(prefix) : nil
end
|
#namespaces ⇒ Object
466
467
468
469
470
471
472
473
474
475
476
|
# File 'lib/xmlutils/node.rb', line 466
def namespaces
ns = @prefixes.dup
@attributes.each do |k, v|
if k == 'xmlns'
ns[''] = v.value
elsif k.start_with?('xmlns:')
ns[k.sub('xmlns:', '')] = v.value
end
end
ns
end
|
#node_type ⇒ Object
364
365
366
|
# File 'lib/xmlutils/node.rb', line 364
def node_type
:element
end
|
#prefix ⇒ Object
485
486
487
|
# File 'lib/xmlutils/node.rb', line 485
def prefix
@name.include?(':') ? @name.split(':').first : ''
end
|
#text(path = nil) ⇒ Object
437
438
439
440
441
|
# File 'lib/xmlutils/node.rb', line 437
def text(path = nil)
return XPath.match(self, path).first.text if path
txt = @children.select { |c| c.is_a?(Text) || c.is_a?(CData) }
txt.map(&:to_s).join('')
end
|
#write_content(output, indent = 0) ⇒ Object
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
|
# File 'lib/xmlutils/node.rb', line 496
def write_content(output, indent = 0)
output << "<#{@name}"
@attributes.each_value { |attr| output << " #{attr.to_string}" }
if @children.empty?
output << " />"
else
output << ">"
has_only_text = @children.all? { |c| c.is_a?(Text) || c.is_a?(CData) }
if has_only_text
@children.each { |c| c.write(output, 0) }
else
output << "\n"
@children.each { |c| c.write(output, indent + 1); output << "\n" }
output << ' ' * indent
end
output << "</#{@name}>"
end
end
|