| Class | REXML::Attribute |
| In: |
|
| Parent: | Object |
Defines an Element Attribute; IE, a attribute=value pair, as in: <element attribute="value"/>. Attributes can be in their own namespaces. General users of REXML will not interact with the Attribute class much.
Constants
| PATTERN | = | /\s*(#{NAME_STR})\s*=\s*(["'])(.*?)\2/um |
Attributes
| element | [R] | The element to which this attribute belongs |
| normalized | [W] | The normalized value of this attribute. That is, the attribute with entities intact. |
Public Class methods
Constructor.
Attribute.new( attribute_to_clone ) Attribute.new( source ) Attribute.new( "attr", "attr_value" ) Attribute.new( "attr", "attr_value", parent_element )
26: def initialize( first, second=nil, parent=nil ) 27: @normalized = @unnormalized = @element = nil 28: if first.kind_of? Attribute 29: self.name = first.expanded_name 30: @value = first.value 31: if second.kind_of? Element 32: @element = second 33: else 34: @element = first.element 35: end 36: elsif first.kind_of? String 37: @element = parent if parent.kind_of? Element 38: self.name = first 39: @value = second.to_s 40: else 41: raise "illegal argument #{first.class.name} to Attribute constructor" 42: end 43: end
Public Instance methods
Sets the element of which this object is an attribute. Normally, this is not directly called.
Returns this attribute
129: def element=( element ) 130: @element = element 131: self 132: end
Creates (and returns) a hash from both the name and value
79: def hash 80: name.hash + value.hash 81: end
Returns the namespace URL, if defined, or nil otherwise
e = Element.new("el")
e.namespace( "ns" ) # -> "http://url"
67: def namespace arg=nil 68: arg = prefix if arg.nil? 69: @element.namespace arg 70: end
Returns the namespace of the attribute.
e = Element.new( "elns:myelement" ) e.add_attribute( "nsa:a", "aval" ) e.add_attribute( "b", "bval" ) e.attributes.get_attribute( "a" ).prefix # -> "nsa" e.attributes.get_attribute( "b" ).prefix # -> "elns" a = Attribute.new( "x", "y" ) a.prefix # -> ""
54: def prefix 55: pf = super 56: if pf == "" 57: pf = @element.prefix if @element 58: end 59: pf 60: end
Returns the attribute value, with entities replaced
94: def to_s 95: return @normalized if @normalized 96: 97: doctype = nil 98: if @element 99: doc = @element.document 100: doctype = doc.doctype if doc 101: end 102: 103: @unnormalized = nil 104: @normalized = Text::normalize( @value, doctype ) 105: end
Returns the UNNORMALIZED value of this attribute. That is, entities have been expanded to their values
109: def value 110: return @unnormalized if @unnormalized 111: doctype = nil 112: if @element 113: doc = @element.document 114: doctype = doc.doctype if doc 115: end 116: @normalized = nil 117: @unnormalized = Text::unnormalize( @value, doctype ) 118: end