Class REXML::Attribute
In: lib/rexml/attribute.rb
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.

Methods

==   clone   element=   hash   inspect   namespace   new   node_type   prefix   remove   to_s   to_string   value   write   xpath  

Included Modules

Node Namespace

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. 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 )

[Source]

    # 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

Public Instance methods

Returns true if other is an Attribute and has the same name and value, false otherwise.

[Source]

    # File lib/rexml/attribute.rb, line 90
90:                 def ==( other )
91:                         other.kind_of?(Attribute) and other.name==name and other.value==value
92:                 end

Returns a copy of this attribute

[Source]

     # 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

[Source]

     # File lib/rexml/attribute.rb, line 151
151:                 def element=( element )
152:                         @element = element
153:                         self
154:                 end

Creates (and returns) a hash from both the name and value

[Source]

    # File lib/rexml/attribute.rb, line 95
95:                 def hash
96:                         name.hash + value.hash
97:                 end

[Source]

     # File lib/rexml/attribute.rb, line 172
172:     def inspect
173:       rv = ""
174:       write( rv )
175:       rv
176:     end

Returns the namespace URL, if defined, or nil otherwise

 e = Element.new("el")
 e.add_attributes({"xmlns:ns", "http://url"})
 e.namespace( "ns" )              # -> "http://url"

[Source]

    # File lib/rexml/attribute.rb, line 83
83:                 def namespace arg=nil
84:                         arg = prefix if arg.nil?
85:                         @element.namespace arg
86:                 end

[Source]

     # File lib/rexml/attribute.rb, line 168
168:     def node_type
169:       :attribute
170:     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                                   # -> ""

[Source]

    # 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

Removes this Attribute from the tree, and returns true if successfull

This method is usually not called directly.

[Source]

     # File lib/rexml/attribute.rb, line 159
159:                 def remove
160:                         @element.attributes.delete self.name unless @element.nil?
161:                 end

Returns the attribute value, with entities replaced

[Source]

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

[Source]

     # 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(/"/, '&quote;')}"^
108:                         else
109:                                 "#@expanded_name='#{to_s().gsub(/'/, '&apos;')}'"
110:                         end
111:                 end

Returns the UNNORMALIZED value of this attribute. That is, entities have been expanded to their values

[Source]

     # 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

Writes this attribute (EG, puts ‘key="value"’ to the output)

[Source]

     # File lib/rexml/attribute.rb, line 164
164:                 def write( output, indent=-1 )
165:                         output << to_string
166:                 end

[Source]

     # File lib/rexml/attribute.rb, line 178
178:     def xpath
179:       path = @element.xpath
180:       path += "/@#{self.expanded_name}"
181:       return path
182:     end

[Validate]