Ruby 2.1.0 リファレンスマニュアル > ライブラリ一覧 > rexml/documentライブラリ > REXML::Attributeクラス

class REXML::Attribute

クラスの継承リスト: REXML::Attribute < REXML::Node < REXML::Namespace < REXML::XMLTokens < Object < Kernel < BasicObject

要約

要素(REXML::Element)の属性を表すクラスです。

つまり、 <element attribute="value"/> という 要素における attribute=value というペアのことです。

属性にはなんらかの名前空間(namespace, REXML::Namespace) に属することができます。

目次

特異メソッド
new
インスタンスメソッド
== clone element element= namespace node_type normalized= prefix remove to_s to_string value write xpath

特異メソッド

new(attribute_to_clone, parent = nil) -> REXML::Attribute[permalink][rdoc]
new(attribute, value, parent = nil) -> REXML::Attribute

新たな属性オブジェクトを生成します。

2種類の初期化が可能です。 REXML::Attribute オブジェクトを渡した場合は、 属性名とその値がそれから複製されます。 parent で新たに作られる属性オブジェクトが属する 要素が指定できます。 parent を省略した場合は複製元と同じ要素の属するように 設定されます。

また、属性名とその値を文字列で指定することもできます。 parent で新たに作られる属性オブジェクトが属する 要素が指定できます。 parent を省略した場合は nil が設定されます。

通常はこのメソッドは直接は使わず、REXML::Element#add_attribute などを 使うでしょう。

[PARAM] attribute_to_clone:
複製元の REXML::Attribute オブジェクト
[PARAM] attribute:
属性名
[PARAM] value:
属性の値
[PARAM] parent:
生成される属性が所属する要素(REXML::Element)

インスタンスメソッド

self == other -> bool[permalink][rdoc]

属性の名前と値が other と一致する場合に真を返します。

clone -> REXML::Element[permalink][rdoc]

self を複製し返します。

element -> REXML::Element[permalink][rdoc]

その属性が属する要素を返します。

element=(element)[permalink][rdoc]

self が属する要素を変更します。

[PARAM] element:
変更先の要素(REXML::Element)
namespace(arg = nil) -> String | nil[permalink][rdoc]

属性の名前空間の URI を返します。

URI が定義されていない場合は nil を返します。

[PARAM] arg:
この値を指定すると、その属性の名前空間でなく、arg という名前空間 の URI が返されます。 通常は省略します。

require 'rexml/document'
e = REXML::Element.new("el")
e.add_attribute("xmlns:ns", "http://www.example.com/ns")
e.add_attribute("ns:r", "rval")
p e.attributes.get_attribute("r").prefix  # => "ns"
p e.attributes.get_attribute("r").namespace # => "http://www.example.com/ns"
node_type -> Symbol[permalink][rdoc]

「:attribute」というシンボルを返します。

normalized=(value)[permalink][rdoc]

正規化された属性値を設定します。

通常はライブラリが自動的にこの値を設定するので ユーザはこれを使う必要はないでしょう。

[PARAM] value:
正規化された属性値
prefix -> String[permalink][rdoc]

属性の名前空間を返します。

require 'rexml/document'
e = REXML::Element.new( "elns:myelement" )
e.add_attribute( "nsa:a", "aval" )
e.add_attribute( "b", "bval" )
p e.attributes.get_attribute( "a" ).prefix   # -> "nsa"
p e.attributes.get_attribute( "b" ).prefix   # -> "elns"
a = REXML::Attribute.new( "x", "y" )
p a.prefix                                   # -> ""
remove -> ()[permalink][rdoc]

self を所属する要素から取り除きます。

to_s -> String[permalink][rdoc]

正規化された属性値を返します。

属性値の正規化については XML の仕様を参考にしてください。

to_string -> String[permalink][rdoc]

"name='value'" という形式の文字列を返します。

require 'rexml/document'
e = REXML::Element.new("el")
e.add_attribute("ns:r", "rval")
p e.attributes.get_attribute("r").to_string # => "ns:r='rval'"
value -> String[permalink][rdoc]

正規化されていない属性値を返します。

属性値の正規化については XML の仕様を参考にしてください。

write(output, indent = -1) -> object[permalink][rdoc]

output に self の情報を name='value' という形式で書き込みます。

output が返ります。

[PARAM] output:
書き込み先の IO オブジェクト
[PARAM] indent:
インデントレベル、ここでは無視される
xpath -> String[permalink][rdoc]

その属性を指定する xpath 文字列を返します。

例えば "/foo/bar/@ns:r" という文字列を返します。