| Class | REXML::Attribute |
| In: |
lib/rexml/attribute.rb
|
| Parent: | Object |
| PATTERN | = | /\s*(#{NAME_STR})\s*=\s*(["'])(.*?)\2/um |
| element | [R] | The element to which this attribute belongs |
| normalized | [W] | The normalized value of this attribute. That is, the attribute with entities intact. |
Constructor.
Attribute.new( attribute_to_clone ) Attribute.new( source ) Attribute.new( "attr", "attr_value" ) Attribute.new( "attr", "attr_value", parent_element )
# File lib/rexml/attribute.rb, line 26
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
Returns a copy of this attribute
# File lib/rexml/attribute.rb, line 121
121: def clone
122: Attribute.new self
123: end
Sets the element of which this object is an attribute. Normally, this is not directly called.
Returns this attribute
# File lib/rexml/attribute.rb, line 129
129: def element=( element )
130: @element = element
131: self
132: end
# File lib/rexml/attribute.rb, line 150
150: def inspect
151: rv = ""
152: write( rv )
153: rv
154: 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 # -> ""
# File lib/rexml/attribute.rb, line 54
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
# File lib/rexml/attribute.rb, line 94
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 this attribute out as XML source, expanding the name
a = Attribute.new( "x", "y" ) a.to_string # -> "x='y'" b = Attribute.new( "ns:x", "y" ) b.to_string # -> "ns:x='y'"
# File lib/rexml/attribute.rb, line 89
89: def to_string
90: "#@expanded_name='#{to_s().gsub(/'/, ''')}'"
91: end
Returns the UNNORMALIZED value of this attribute. That is, entities have been expanded to their values
# File lib/rexml/attribute.rb, line 109
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