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.

 Attribute.new( attribute_to_clone )
 Attribute.new( source )
 Attribute.new( "attr", "attr_value" )
 Attribute.new( "attr", "attr_value", parent_element )

[Source]

    # 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

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 74
74:                 def ==( other )
75:                         other.kind_of?(Attribute) and other.name==name and other.value==@value
76:                 end

Returns a copy of this attribute

[Source]

     # 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

[Source]

     # File lib/rexml/attribute.rb, line 129
129:                 def element=( element )
130:                         @element = element
131:                         self
132:                 end

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

[Source]

    # File lib/rexml/attribute.rb, line 79
79:                 def hash
80:                         name.hash + value.hash
81:                 end

[Source]

     # File lib/rexml/attribute.rb, line 150
150:     def inspect
151:       rv = ""
152:       write( rv )
153:       rv
154:     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 67
67:                 def namespace arg=nil
68:                         arg = prefix if arg.nil?
69:                         @element.namespace arg
70:                 end

[Source]

     # File lib/rexml/attribute.rb, line 146
146:     def node_type
147:       :attribute
148:     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 54
54:                 def prefix
55:                         pf = super
56:                         if pf == ""
57:                                 pf = @element.prefix if @element
58:                         end
59:                         pf
60:                 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 137
137:                 def remove
138:                         @element.attributes.delete self.name unless @element.nil?
139:                 end

Returns the attribute value, with entities replaced

[Source]

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

[Source]

    # File lib/rexml/attribute.rb, line 89
89:                 def to_string
90:                         "#@expanded_name='#{to_s().gsub(/'/, '&apos;')}'"
91:                 end

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

[Source]

     # 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

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

[Source]

     # File lib/rexml/attribute.rb, line 142
142:                 def write( output, indent=-1 )
143:                         output << to_string
144:                 end

[Source]

     # File lib/rexml/attribute.rb, line 156
156:     def xpath
157:       path = @element.xpath
158:       path += "/@#{self.expanded_name}"
159:       return path
160:     end

[Validate]