Class: REXML::Attributes

Class | REXML::Attributes
In: | temp/element.rb
Parent: | Hash

A class that defines the set of Attributes of an Element and provides operations for accessing elements in that set.

### Methods

<< [] []= add delete delete_all each each_attribute get_attribute get_attribute_ns length namespaces new prefixes size to_a

### Public Class methods

new(element)

Constructor

element: | the Element of which this is an Attribute

### Public Instance methods

<<( attribute )

Alias for add

[](name)

Fetches an attribute value. If you want to get the Attribute itself, use get_attribute()

name: | an XPath attribute name. Namespaces are relevant here.
Returns: | the String value of the matching attribute, or `nil` if no matching attribute was found. This is the unnormalized value (with entities expanded).

     doc = Document.new "<a foo:att='1' bar:att='2' att='&lt;'/>"
     doc.root.attributes['att']         #-> '<'
     doc.root.attributes['bar:att']     #-> '2'

[]=( name, value )

Sets an attribute, overwriting any existing attribute value by the same name. Namespace is significant.

name: | the name of the attribute
value: | (optional) If supplied, the value of the attribute. If nil, any existing matching attribute is deleted.
Returns: | Owning element

     doc = Document.new "<a x:foo='1' foo='3'/>"
     doc.root.attributes['y:foo'] = '2'
     doc.root.attributes['foo'] = '4'
     doc.root.attributes['x:foo'] = nil

add( attribute )

Adds an attribute, overriding any existing attribute by the same name. Namespaces are significant.

attribute: | An Attribute

delete( attribute )

Removes an attribute

attribute: | either a String, which is the name of the attribute to remove — namespaces are significant here — or the attribute to remove.
Returns: | the owning element

     doc = Document.new "<a y:foo='0' x:foo='1' foo='3' z:foo='4'/>"
     doc.root.attributes.delete 'foo'   #-> <a y:foo='0' x:foo='1' z:foo='4'/>"
     doc.root.attributes.delete 'x:foo' #-> <a y:foo='0' z:foo='4'/>"
     attr = doc.root.attributes.get_attribute('y:foo')
     doc.root.attributes.delete attr    #-> <a z:foo='4'/>"

delete_all( name )

Deletes all attributes matching a name. Namespaces are significant.

name: | A String; all attributes that match this path will be removed
Returns: | an Array of the Attributes that were removed

each() {|[attr.expanded_name, attr.value]| ...}

Itterates over each attribute of an Element, yielding the expanded name and value as a pair of Strings.

     doc = Document.new '<a x="1" y="2"/>'
     doc.root.attributes.each {|name, value| p name+" => "+value }

each_attribute( {|attribute| ...}

Itterates over the attributes of an Element. Yields actual Attribute nodes, not String values.

     doc = Document.new '<a x="1" y="2"/>'
     doc.root.attributes.each_attribute {|attr|
       p attr.expanded_name+" => "+attr.value
     }

get_attribute( name )

Fetches an attribute

name: | the name by which to search for the attribute. Can be a `prefix:name` namespace name.
Returns: | The first matching attribute, or nil if there was none. This

value is an Attribute node, not the String value of the attribute.

     doc = Document.new '<a x:foo="1" foo="2" bar="3"/>'
     doc.root.attributes.get_attribute("foo").value    #-> "2"
     doc.root.attributes.get_attribute("x:foo").value  #-> "1"

get_attribute_ns(namespace, name)

The `get_attribute_ns` method retrieves a method by its namespace and name. Thus it is possible to reliably identify an attribute even if an XML processor has changed the prefix.

Method contributed by Henrik Martensson

length()

Returns the number of attributes the owning Element contains.

     doc = Document "<a x='1' y='2' foo:x='3'/>"
     doc.root.attributes.length        #-> 3

namespaces()
prefixes()

Returns an array of Strings containing all of the prefixes declared by this set of # attributes. The array does not include the default namespace declaration, if one exists.

     doc = Document.new("<a xmlns='foo' xmlns:x='bar' xmlns:y='twee' "+
           "z='glorp' p:k='gru'/>")
     prefixes = doc.root.attributes.prefixes    #-> ['x', 'y']

size()

Alias for length

to_a()

[Validate]