Class: REXML::Attributes

Class REXML::Attributes |
In: | rexml/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 length new prefixes

Public Class methods

new(element)

Constructor

element: | the Element of which this is an Attribute

Public Instance methods

[](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.

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

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

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
     }

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 }

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"

[]=( 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

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']

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'/>"

add( attribute )

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

attribute: | An Attribute

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