Class: REXML::Attributes
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Hash
|
|
Instance Chain:
self,
Hash
|
|
Inherits: |
Hash
|
Defined in: | lib/rexml/element.rb |
Overview
A class that defines the set of Attributes
of an Element
and provides operations for accessing elements in that set.
Class Method Summary
-
.new(element) ⇒ Attributes
constructor
Creates and returns a new REXML::Attributes object.
Instance Method Summary
-
#<<(attribute)
Alias for #add.
-
#[](name) ⇒ attribute_value?
Returns the value for the attribute given by
name
, if it exists; otherwisenil
. -
#[]=(name, value) ⇒ value
When
value
is non-nil
, assigns that to the attribute for the givenname
, overwriting the previous value if it exists: -
#add(attribute) ⇒ attribute
(also: #<<)
Adds attribute
attribute
, replacing the previous attribute of the same name if it exists; returnsattribute
: -
#delete(name) ⇒ element
Removes a specified attribute if it exists; returns the attributes’ element.
-
#delete_all(name) ⇒ Attributes
Removes all attributes matching the given
name
; returns an array of the removed attributes: -
#each {|expanded_name, value| ... }
Calls the given block with each expanded-name/value pair:
-
#each_attribute {|attr| ... }
Calls the given block with each REXML::Attribute object:
-
#get_attribute(name) ⇒ attribute_object?
Returns the REXML::Attribute object for the given
name
: -
#get_attribute_ns(namespace, name)
Returns the REXML::Attribute object among the attributes that matches the given
namespace
andname
: -
#length
(also: #size)
Returns the count of attributes:
-
#namespaces
Returns a hash of name/value pairs for the namespaces:
-
#prefixes ⇒ array_of_prefix_strings
Returns an array of prefix strings in the attributes.
-
#size
Alias for #length.
-
#to_a ⇒ array_of_attribute_objects
Returns an array of REXML::Attribute objects representing the attributes:
Constructor Details
.new(element) ⇒ Attributes
Creates and returns a new REXML::Attributes object. The element given by argument element
is stored, but its own attributes are not modified:
ele = REXML::Element.new('foo')
attrs = REXML::Attributes.new(ele)
attrs.object_id == ele.attributes.object_id # => false
Other instance methods in class REXML::Attributes may refer to:
-
element.document
. -
element.prefix
. -
element.expanded_name
.
# File 'lib/rexml/element.rb', line 2160
def initialize element @element = element end
Instance Method Details
#<<(attribute)
Alias for #add.
# File 'lib/rexml/element.rb', line 2541
alias :<< :add
#[](name) ⇒ attribute_value
?
Returns the value for the attribute given by name
, if it exists; otherwise nil
. The value returned is the unnormalized attribute value, with entities expanded:
xml_string = <<-EOT
<root xmlns:foo="http://foo" xmlns:bar="http://bar">
<ele foo:att='1' bar:att='2' att='<'/>
</root>
EOT
d = REXML::Document.new(xml_string)
ele = d.elements['//ele'] # => <a foo:att='1' bar:att='2' att='<'/>
ele.attributes['att'] # => "<"
ele.attributes['bar:att'] # => "2"
ele.attributes['nosuch'] # => nil
Related: get_attribute (returns an Attribute object).
# File 'lib/rexml/element.rb', line 2185
def [](name) attr = get_attribute(name) return attr.value unless attr.nil? return nil end
#[]=(name, value) ⇒ value
When value
is non-nil
, assigns that to the attribute for the given name
, overwriting the previous value if it exists:
xml_string = <<-EOT
<root xmlns:foo="http://foo" xmlns:bar="http://bar">
<ele foo:att='1' bar:att='2' att='<'/>
</root>
EOT
d = REXML::Document.new(xml_string)
ele = d.root.elements['//ele'] # => <a foo:att='1' bar:att='2' att='<'/>
attrs = ele.attributes
attrs['foo:att'] = '2' # => "2"
attrs['baz:att'] = '3' # => "3"
When value
is nil
, deletes the attribute if it exists:
attrs['baz:att'] = nil
attrs.include?('baz:att') # => false
# File 'lib/rexml/element.rb', line 2369
def []=( name, value ) if value.nil? # Delete the named attribute attr = get_attribute(name) delete attr return end unless value.kind_of? Attribute if @element.document and @element.document.doctype value = Text::normalize( value, @element.document.doctype ) else value = Text::normalize( value, nil ) end value = Attribute.new(name, value) end value.element = @element old_attr = fetch(value.name, nil) if old_attr.nil? store(value.name, value) elsif old_attr.kind_of? Hash old_attr[value.prefix] = value elsif old_attr.prefix != value.prefix # Check for conflicting namespaces if value.prefix != "xmlns" and old_attr.prefix != "xmlns" old_namespace = old_attr.namespace new_namespace = value.namespace if old_namespace == new_namespace raise ParseException.new( "Namespace conflict in adding attribute \"#{value.name}\": "+ "Prefix \"#{old_attr.prefix}\" = \"#{old_namespace}\" and "+ "prefix \"#{value.prefix}\" = \"#{new_namespace}\"") end end store value.name, {old_attr.prefix => old_attr, value.prefix => value} else store value.name, value end return @element end
#add(attribute) ⇒ attribute
Also known as: #<<
Adds attribute attribute
, replacing the previous attribute of the same name if it exists; returns attribute
:
xml_string = <<-EOT
<root xmlns:foo="http://foo" xmlns:bar="http://bar">
<ele foo:att='1' bar:att='2' att='<'/>
</root>
EOT
d = REXML::Document.new(xml_string)
ele = d.root.elements['//ele'] # => <a foo:att='1' bar:att='2' att='<'/>
attrs = ele.attributes
attrs # => {"att"=>{"foo"=>foo:att='1', "bar"=>bar:att='2', ""=>att='<'}}
attrs.add(REXML::Attribute.new('foo:att', '2')) # => foo:att='2'
attrs.add(REXML::Attribute.new('baz', '3')) # => baz='3'
attrs.include?('baz') # => true
# File 'lib/rexml/element.rb', line 2537
def add( attribute ) self[attribute.name] = attribute end
#delete(name) ⇒ element
#delete(attribute) ⇒ element
element
#delete(attribute) ⇒ element
Removes a specified attribute if it exists; returns the attributes’ element.
When string argument name
is given, removes the attribute of that name if it exists:
xml_string = <<-EOT
<root xmlns:foo="http://foo" xmlns:bar="http://bar">
<ele foo:att='1' bar:att='2' att='<'/>
</root>
EOT
d = REXML::Document.new(xml_string)
ele = d.root.elements['//ele'] # => <a foo:att='1' bar:att='2' att='<'/>
attrs = ele.attributes
attrs.delete('foo:att') # => <ele bar:att='2' att='<'/>
attrs.delete('foo:att') # => <ele bar:att='2' att='<'/>
When attribute argument attribute
is given, removes that attribute if it exists:
attr = REXML::Attribute.new('bar:att', '2')
attrs.delete(attr) # => <ele att='<'/> # => <ele att='<'/>
attrs.delete(attr) # => <ele att='<'/> # => <ele/>
# File 'lib/rexml/element.rb', line 2490
def delete( attribute ) name = nil prefix = nil if attribute.kind_of? Attribute name = attribute.name prefix = attribute.prefix else attribute =~ Namespace::NAMESPLIT prefix, name = $1, $2 prefix = '' unless prefix end old = fetch(name, nil) if old.kind_of? Hash # the supplied attribute is one of many old.delete(prefix) if old.size == 1 repl = nil old.each_value{|v| repl = v} store name, repl end elsif old.nil? return @element else # the supplied attribute is a top-level one super(name) end @element end
#delete_all(name) ⇒ Attributes
Removes all attributes matching the given name
; returns an array of the removed attributes:
xml_string = <<-EOT
<root xmlns:foo="http://foo" xmlns:bar="http://bar">
<ele foo:att='1' bar:att='2' att='<'/>
</root>
EOT
d = REXML::Document.new(xml_string)
ele = d.root.elements['//ele'] # => <a foo:att='1' bar:att='2' att='<'/>
attrs = ele.attributes
attrs.delete_all('att') # => [att='<']
# File 'lib/rexml/element.rb', line 2559
def delete_all( name ) rv = [] each_attribute { |attribute| rv << attribute if attribute. == name } rv.each{ |attr| attr.remove } return rv end
#each {|expanded_name, value| ... }
Calls the given block with each expanded-name/value pair:
xml_string = <<-EOT
<root xmlns:foo="http://foo" xmlns:bar="http://bar">
<ele foo:att='1' bar:att='2' att='<'/>
</root>
EOT
d = REXML::Document.new(xml_string)
ele = d.root.elements['//ele'] # => <a foo:att='1' bar:att='2' att='<'/>
ele.attributes.each do |, value|
p [, value]
end
["foo:att", "1"]
["bar:att", "2"]
["att", "<"]
# File 'lib/rexml/element.rb', line 2287
def each return to_enum(__method__) unless block_given? each_attribute do |attr| yield [attr., attr.value] end end
#each_attribute {|attr| ... }
Calls the given block with each REXML::Attribute object:
xml_string = <<-EOT
<root xmlns:foo="http://foo" xmlns:bar="http://bar">
<ele foo:att='1' bar:att='2' att='<'/>
</root>
EOT
d = REXML::Document.new(xml_string)
ele = d.root.elements['//ele'] # => <a foo:att='1' bar:att='2' att='<'/>
ele.attributes.each_attribute do |attr|
p [attr.class, attr]
end
[REXML::Attribute, foo:att='1']
[REXML::Attribute, bar:att='2']
[REXML::Attribute, att='<']
# File 'lib/rexml/element.rb', line 2254
def each_attribute # :yields: attribute return to_enum(__method__) unless block_given? each_value do |val| if val.kind_of? Attribute yield val else val.each_value { |atr| yield atr } end end end
#get_attribute(name) ⇒ attribute_object
?
Returns the REXML::Attribute object for the given name
:
xml_string = <<-EOT
<root xmlns:foo="http://foo" xmlns:bar="http://bar">
<ele foo:att='1' bar:att='2' att='<'/>
</root>
EOT
d = REXML::Document.new(xml_string)
ele = d.root.elements['//ele'] # => <a foo:att='1' bar:att='2' att='<'/>
attrs = ele.attributes
attrs.get_attribute('foo:att') # => foo:att='1'
attrs.get_attribute('foo:att').class # => REXML::Attribute
attrs.get_attribute('bar:att') # => bar:att='2'
attrs.get_attribute('att') # => att='<'
attrs.get_attribute('nosuch') # => nil
# File 'lib/rexml/element.rb', line 2313
def get_attribute( name ) attr = fetch( name, nil ) if attr.nil? return nil if name.nil? # Look for prefix name =~ Namespace::NAMESPLIT prefix, n = $1, $2 if prefix attr = fetch( n, nil ) # check prefix if attr == nil elsif attr.kind_of? Attribute return attr if prefix == attr.prefix else attr = attr[ prefix ] return attr end end element_document = @element.document if element_document and element_document.doctype expn = @element. expn = element_document.doctype.name if expn.size == 0 attr_val = element_document.doctype.attribute_of(expn, name) return Attribute.new( name, attr_val ) if attr_val end return nil end if attr.kind_of? Hash attr = attr[ @element.prefix ] end return attr end
#get_attribute_ns(namespace, name)
Returns the REXML::Attribute object among the attributes that matches the given namespace
and name
:
xml_string = <<-EOT
<root xmlns:foo="http://foo" xmlns:bar="http://bar">
<ele foo:att='1' bar:att='2' att='<'/>
</root>
EOT
d = REXML::Document.new(xml_string)
ele = d.root.elements['//ele'] # => <a foo:att='1' bar:att='2' att='<'/>
attrs = ele.attributes
attrs.get_attribute_ns('http://foo', 'att') # => foo:att='1'
attrs.get_attribute_ns('http://foo', 'nosuch') # => nil
# File 'lib/rexml/element.rb', line 2585
def get_attribute_ns(namespace, name) result = nil each_attribute() { |attribute| if name == attribute.name && namespace == attribute.namespace() && ( !namespace.empty? || !attribute. .index(':') ) # foo will match xmlns:foo, but only if foo isn't also an attribute result = attribute if !result or !namespace.empty? or !attribute. .index(':') end } result end
#length Also known as: #size
Returns the count of attributes:
xml_string = <<-EOT
<root xmlns:foo="http://foo" xmlns:bar="http://bar">
<ele foo:att='1' bar:att='2' att='<'/>
</root>
EOT
d = REXML::Document.new(xml_string)
ele = d.root.elements['//ele'] # => <a foo:att='1' bar:att='2' att='<'/>
ele.attributes.length # => 3
# File 'lib/rexml/element.rb', line 2225
def length c = 0 each_attribute { c+=1 } c end
#namespaces
# File 'lib/rexml/element.rb', line 2446
def namespaces namespaces = {} each_attribute do |attribute| namespaces[attribute.name] = attribute.value if attribute.prefix == 'xmlns' or attribute.name == 'xmlns' end if @element.document and @element.document.doctype expn = @element. expn = @element.document.doctype.name if expn.size == 0 @element.document.doctype.attributes_of(expn).each { |attribute| namespaces[attribute.name] = attribute.value if attribute.prefix == 'xmlns' or attribute.name == 'xmlns' } end namespaces end
#prefixes ⇒ array_of_prefix_strings
# File 'lib/rexml/element.rb', line 2421
def prefixes ns = [] each_attribute do |attribute| ns << attribute.name if attribute.prefix == 'xmlns' end if @element.document and @element.document.doctype expn = @element. expn = @element.document.doctype.name if expn.size == 0 @element.document.doctype.attributes_of(expn).each { |attribute| ns << attribute.name if attribute.prefix == 'xmlns' } end ns end
#size
Alias for #length.
# File 'lib/rexml/element.rb', line 2230
alias :size :length
#to_a ⇒ array_of_attribute_objects
Returns an array of REXML::Attribute objects representing the attributes:
xml_string = <<-EOT
<root xmlns:foo="http://foo" xmlns:bar="http://bar">
<ele foo:att='1' bar:att='2' att='<'/>
</root>
EOT
d = REXML::Document.new(xml_string)
ele = d.root.elements['//ele'] # => <a foo:att='1' bar:att='2' att='<'/>
attrs = ele.attributes.to_a # => [foo:att='1', bar:att='2', att='<']
attrs.first.class # => REXML::Attribute
# File 'lib/rexml/element.rb', line 2207
def to_a enum_for(:each_attribute).to_a end