| 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. FIXME: The parser doesn‘t catch illegal characters in attributes
| first: | Either: an Attribute, which this new attribute will become a clone of; or a String, which is the name of this attribute |
| second: | If first is an Attribute, then this may be an Element, or nil. If nil, then the Element parent of this attribute is the parent of the first Attribute. If the first argument is a String, then this must also be a String, and is the content of the attribute. If this is the content, it must be fully normalized (contain no illegal characters). |
| parent: | Ignored unless first is a String; otherwise, may be the Element parent of this attribute, or nil. |
Attribute.new( attribute_to_clone ) Attribute.new( attribute_to_clone, parent_element ) Attribute.new( "attr", "attr_value" ) Attribute.new( "attr", "attr_value", parent_element )
# File lib/rexml/attribute.rb, line 42
42: def initialize( first, second=nil, parent=nil )
43: @normalized = @unnormalized = @element = nil
44: if first.kind_of? Attribute
45: self.name = first.expanded_name
46: @unnormalized = first.value
47: if second.kind_of? Element
48: @element = second
49: else
50: @element = first.element
51: end
52: elsif first.kind_of? String
53: @element = parent
54: self.name = first
55: @normalized = second.to_s
56: else
57: raise "illegal argument #{first.class.name} to Attribute constructor"
58: end
59: end
Returns a copy of this attribute
# File lib/rexml/attribute.rb, line 143
143: def clone
144: Attribute.new self
145: 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 151
151: def element=( element )
152: @element = element
153: self
154: end
# File lib/rexml/attribute.rb, line 172
172: def inspect
173: rv = ""
174: write( rv )
175: rv
176: 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 70
70: def prefix
71: pf = super
72: if pf == ""
73: pf = @element.prefix if @element
74: end
75: pf
76: end
Returns the attribute value, with entities replaced
# File lib/rexml/attribute.rb, line 114
114: def to_s
115: return @normalized if @normalized
116:
117: doctype = nil
118: if @element
119: doc = @element.document
120: doctype = doc.doctype if doc
121: end
122:
123: @normalized = Text::normalize( @unnormalized, doctype )
124: @unnormalized = nil
125: @normalized
126: 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 105
105: def to_string
106: if @element and @element.context and @element.context[:attribute_quote] == :quote
107: %Q^#@expanded_name="#{to_s().gsub(/"/, '"e;')}"^
108: else
109: "#@expanded_name='#{to_s().gsub(/'/, ''')}'"
110: end
111: end
Returns the UNNORMALIZED value of this attribute. That is, entities have been expanded to their values
# File lib/rexml/attribute.rb, line 130
130: def value
131: return @unnormalized if @unnormalized
132: doctype = nil
133: if @element
134: doc = @element.document
135: doctype = doc.doctype if doc
136: end
137: @unnormalized = Text::unnormalize( @normalized, doctype )
138: @normalized = nil
139: @unnormalized
140: end