class REXML::DocType

[edit]

要約

XML の DTD(文書型定義、Document Type Definition)を表すクラスです。

rexml では DTD は積極的にはサポートされていません。デフォルトの実体定義(gt, lt, amp, quot apos)の解決のため DTD はある程度はサポートされますが、スキーマの定義や検証をしたい場合は XML schema や RELAX NG などを使ってください。

子ノード(REXML::Parent#children)として、

などを保持しています。

目次

特異メソッド
インスタンスメソッド
定数

継承しているメソッド

REXML::Parentから継承しているメソッド
Enumerableから継承しているメソッド
REXML::Childから継承しているメソッド
REXML::Nodeから継承しているメソッド

特異メソッド

new(source, parent = nil) -> REXML::DocType[permalink][rdoc][edit]

DocType オブジェクトを生成します。

REXML::Source オブジェクトの場合は、Source オブジェクトが保持しているDTDのテキストがパースされ、その内容によって DocType オブジェクトが初期化されます。

REXML::DocType.new(Source.new(<<EOS))
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
EOS

このインターフェースは deprecated です。

このメソッドは他のインターフェースもありますが、内部用なので使わないでください。

一般的にいって、XML 文書に含まれる DTD は REXML::Document.new などで適切に解析され、REXML::Document#doctype で取得できます。このメソッドを直接使う必要はありません。

インスタンスメソッド

add(child) -> ()[permalink][rdoc][edit]

child を子ノード列の最後に追加します。

REXML::Parent#add を内部で呼び出します。また、REXML::DocType#entities を更新します。

[PARAM] child:
追加するノード
attribute_of(element, attribute) -> String | nil[permalink][rdoc][edit]

DTD 内の属性リスト宣言で、 element という名前の要素の attribute という名前の属性のデフォルト値を返します。

elementという名前の要素の属性値は宣言されていない、 elementという名前の要素にはattributeという名前の属性が宣言されていない、もしくはデフォルト値が宣言されていない、のいずれかの場合は nil を返します。

[PARAM] element:
要素名(文字列)
[PARAM] attribute:
属性名(文字列)

require 'rexml/document'

doctype = REXML::Document.new(<<EOS).doctype
<!DOCTYPE books [
<!ELEMENT book (comment)>
<!ELEMENT comment (#PCDATA)>
<!ATTLIST book
          author CDATA #REQUIRED
          title CDATA #REQUIRED
          publisher CDATA "foobar publisher">
]>
EOS

p doctype.attribute_of("book", "publisher") # => "foobar publisher"
p doctype.attribute_of("bar", "foo") # => nil
p doctype.attribute_of("book", "baz") # => nil
p doctype.attribute_of("book", "title") # => nil
attributes_of(element) -> [REXML::Attribute][permalink][rdoc][edit]

DTD 内の属性リスト宣言で、 element という名前の要素に対し宣言されている属性の名前とデフォルト値を REXML::Attribute の配列で返します。

名前とデフォルト値のペアは、各 Attribute オブジェクトの REXML::Attribute#nameREXML::Attribute#value で表現されます。


require 'rexml/document'

doctype = REXML::Document.new(<<EOS).doctype
<!DOCTYPE books [
<!ELEMENT book (comment)>
<!ELEMENT comment (#PCDATA)>
<!ATTLIST book
          author CDATA #REQUIRED
          title CDATA #REQUIRED
          publisher CDATA "foobar publisher">
]>
EOS

p doctype.attributes_of("book")
# => [author='', title='', publisher='foobar publisher']
p doctype.attributes_of("book")[0].name # => "author"
p doctype.attributes_of("book")[0].value # => ""
clone -> REXML::DocType[permalink][rdoc][edit]

self の複製を返します。

external_id (REXML::DocType#external_id) と名前(REXML::DocType#name) のみ複製されるため、結果として得られるオブジェクトはあまり有用ではないでしょう。

context -> { Symbol => object }[permalink][rdoc][edit]

DTD が属する文書の「コンテキスト」を返します。

具体的には親ノードである REXML::Document オブジェクトの REXML::Element#context を返します。

コンテキストの具体的な内容については REXML::Element#context を参照してください。

entities -> { String => REXML::Entity }[permalink][rdoc][edit]

DTD で宣言されている実体の集合を Hash で返します。

返される Hash は実体参照名をキーとし、対応する REXML::Entity オブジェクトを値とするハッシュテーブルです。

これには、XML のデフォルトの実体(gt, lt, quot, apos)も含まれています。


doctype = REXML::Document.new(<<EOS).doctype
<!DOCTYPE foo [
  <!ENTITY bar "barbarbarbar">
]>
EOS

p doctype.entities # => { "gt" => #<REXML::Entity: ...>,
                    #      "lt" => #<REXML::Entity: ...>, ... }
p doctype.entities["bar"].to_s # => "<!ENTITY bar \"barbarbarbar\">"
p doctype.entities["gt"].to_s # => "<!ENTITY gt \">\">"
entity(name) -> String | nil[permalink][rdoc][edit]

name という実体参照名を持つ実体を文字列で返します。

返される文字列は非正規化(REXML::Entity#unnormalized 参照) された文字列が返されます。

name という名前を持つ実体が存在しない場合には nil を返します。

[PARAM] name:
実体参照名(文字列)

doctype = REXML::Document.new(<<EOS).doctype
<!DOCTYPE foo [
  <!ENTITY bar "barbarbarbar">
]>
EOS
p doctype.entity("bar") # => "barbarbar"
p doctype.entity("foo") # => nil
external_id -> String | nil[permalink][rdoc][edit]

DTD が外部サブセットを用いている場合は "SYSTEM", "PUBLIC" のいずれかの文字列を返します。

それ以外の場合は nil を返します。


require 'rexml/document'
doctype = REXML::Document.new(<<EOS).doctype
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
EOS
doctype.name # => "html"
doctype.external_id  # => "PUBLIC"

doctype = REXML::Document.new(<<EOS).doctype
<!DOCTYPE books [
  <!ELEMENT books (book+)>
  <!ELEMENT book (title,author)>
  <!ELEMENT title (#PCDATA)>
  <!ELEMENT author (#PCDATA)>
]>
EOS
doctype.name # => "books"
doctype.external_id # => nil
name -> String[permalink][rdoc][edit]

ルート要素名を返します。


document = REXML::Document.new(<<EOS)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
EOS
doctype = document.doctype
p doctype.name # => "html"
namespaces -> nil[permalink][rdoc][edit]

nil を返します。

node_type -> Symbol[permalink][rdoc][edit]

Symbol :doctype を返します。

notation(name) -> REXML::NotationDecl | nil[permalink][rdoc][edit]

DTD に含まれている記法宣言 (REXML::NotationDecl) で name という名前を持つものを返します。

name という名前を持つ記法宣言が存在しない場合は nil を返します。

[PARAM] name:
検索する記法名
notations -> [REXML::NotationDecl][permalink][rdoc][edit]

DTD に含まれている記法宣言 (REXML::NotationDecl) を配列で返します。

public -> String | nil[permalink][rdoc][edit]

DTD の公開識別子を返します。

DTD が公開識別子による外部サブセットを含んでいない場合は nil を返します。


require 'rexml/document'
doctype = REXML::Document.new(<<EOS).doctype
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
EOS
doctype.system # => "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
doctype.public  # => "-//W3C//DTD XHTML 1.0 Strict//EN"

doctype = REXML::Document.new(<<EOS).doctype
<!DOCTYPE root SYSTEM "foobar">
EOS
doctype.system # => "foobar"
doctype.public  # => nil
system -> String | nil[permalink][rdoc][edit]

DTD のシステム識別子を返します。

DTD が外部サブセットを含んでいない場合は nil を返します。


require 'rexml/document'
doctype = REXML::Document.new(<<EOS).doctype
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
EOS
doctype.system # => "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
doctype.public  # => "-//W3C//DTD XHTML 1.0 Strict//EN"

doctype = REXML::Document.new(<<EOS).doctype
<!DOCTYPE root SYSTEM "foobar">
EOS
doctype.system # => "foobar"
doctype.public  # => nil
write(output, indent = 0, transitive = false, ie_hack = false) -> ()[permalink][rdoc][edit]

output に DTD を出力します。

このメソッドは deprecated です。REXML::Formatter で出力してください。

[PARAM] output:
出力先の IO オブジェクト
[PARAM] indent:
インデントの深さ。指定しないでください。
[PARAM] transitive:
無視されます。指定しないでください。
[PARAM] ie_hack:
無視されます。指定しないでください。

require 'rexml/document'

doctype = REXML::Document.new(<<EOS).doctype
<!DOCTYPE books [
<!ELEMENT book (comment)>
<!ELEMENT comment (#PCDATA)>
<!ATTLIST book
          author CDATA #REQUIRED
          title CDATA #REQUIRED
          publisher CDATA "foobar publisher">
<!ENTITY p "foobar publisher">
<!ENTITY % q "quzz">
]>
EOS

doctype.write(STDOUT)
# =>
# <!DOCTYPE books [
# <!ELEMENT book (comment)>
# ....

定数

DEFAULT_ENTITIES -> { String => REXML::Entity }[permalink][rdoc][edit]

XML の仕様上デフォルトで定義されている実体の Hash table。

"amp" => REXML::EntityConst::AMP は含まれません。