Method: REXML::Attributes#get_attribute
- Defined in:
- lib/rexml/element.rb
#get_attribute(name) ⇒ Object
:call-seq:
get_attribute(name) -> attribute_object or nil
Returns the REXML::Attribute object for the given name:
xml_string = " <root xmlns:foo=\"http://foo\" xmlns:bar=\"http://bar\">\n <ele foo:att='1' bar:att='2' att='<'/>\n </root>\n"
d = REXML::Document.new(xml_string)
ele = d.root.elements['//ele'] # => <a foo:att='1' bar:att='2' att='<'/>
attrs = ele.attributes
attrs.get_attribute('foo:att') # => foo:att='1'
attrs.get_attribute('foo:att').class # => REXML::Attribute
attrs.get_attribute('bar:att') # => bar:att='2'
attrs.get_attribute('att') # => att='<'
attrs.get_attribute('nosuch') # => nil
2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 |
# File 'lib/rexml/element.rb', line 2302 def get_attribute( name ) attr = fetch( name, nil ) if attr.nil? return nil if name.nil? # Look for prefix name =~ Namespace::NAMESPLIT prefix, n = $1, $2 if prefix attr = fetch( n, nil ) # check prefix if attr == nil elsif attr.kind_of? Attribute return attr if prefix == attr.prefix else attr = attr[ prefix ] return attr end end doctype = @element.document&.doctype if doctype expn = @element. expn = doctype.name if expn.size == 0 attr_val = doctype.attribute_of(expn, name) return Attribute.new( name, attr_val ) if attr_val end return nil end if attr.kind_of? Hash attr = attr[ @element.prefix ] end attr end |