class LibXML::XML::Document

Public Class Methods

XML::Document.document(document) → XML::Document click to toggle source

Creates a new document based on the specified document.

Parameters:

document - A preparsed document.
# File lib/libxml/document.rb, line 13
def self.document(value)
  Parser.document(value).parse
end
XML::Document.file(path) → XML::Document click to toggle source
XML::Document.file(path, :encoding → XML::Encoding::UTF_8,
:options => XML::Parser::Options::NOENT) → XML::Document

Creates a new document from the specified file or uri.

You may provide an optional hash table to control how the parsing is performed. Valid options are:

encoding - The document encoding, defaults to nil. Valid values
           are the encoding constants defined on XML::Encoding.
options - Parser options.  Valid values are the constants defined on
          XML::Parser::Options.  Mutliple options can be combined
          by using Bitwise OR (|).
# File lib/libxml/document.rb, line 32
def self.file(value, options = {})
  Parser.file(value, options).parse
end
XML::Document.io(io) → XML::Document click to toggle source
XML::Document.io(io, :encoding → XML::Encoding::UTF_8,
:options → XML::Parser::Options::NOENT
:base_uri="http://libxml.org") → XML::Document

Creates a new document from the specified io object.

Parameters:

io - io object that contains the xml to parser
base_uri - The base url for the parsed document.
encoding - The document encoding, defaults to nil. Valid values
           are the encoding constants defined on XML::Encoding.
options - Parser options.  Valid values are the constants defined on
          XML::Parser::Options.  Mutliple options can be combined
          by using Bitwise OR (|).
# File lib/libxml/document.rb, line 53
def self.io(value, options = {})
  Parser.io(value, options).parse
end
XML::Document.string(string) → XML::Document click to toggle source
XML::Document.string(string, :encoding → XML::Encoding::UTF_8,
:options → XML::Parser::Options::NOENT
:base_uri="http://libxml.org") → XML::Document

Creates a new document from the specified string.

You may provide an optional hash table to control how the parsing is performed. Valid options are:

base_uri - The base url for the parsed document.
encoding - The document encoding, defaults to nil. Valid values
           are the encoding constants defined on XML::Encoding.
options  - Parser options.  Valid values are the constants defined on
           XML::Parser::Options.  Mutliple options can be combined
           by using Bitwise OR (|).
# File lib/libxml/document.rb, line 74
def self.string(value, options = {})
  Parser.string(value, options).parse
end

Public Instance Methods

context(namespaces=nil) → XPath::Context click to toggle source

Returns a new XML::XPathContext for the document.

Namespaces is an optional array of XML::NS objects

# File lib/libxml/document.rb, line 84
def context(nslist = nil)
  context = XPath::Context.new(self)
  context.node = self.root
  context.register_namespaces_from_node(self.root)
  context.register_namespaces(nslist) if nslist
  context
end
find(xpath, nslist=nil) → XML::XPath::Object click to toggle source

Return the nodes matching the specified xpath expression, optionally using the specified namespace. For more information about working with namespaces, please refer to the XML::XPath documentation.

Parameters:

  • xpath - The xpath expression as a string

  • namespaces - An optional list of namespaces (see XML::XPath for information).

document.find('/foo', 'xlink:http://www.w3.org/1999/xlink')

IMPORTANT - The returned XML::Node::Set must be freed before its associated document. In a running Ruby program this will happen automatically via Ruby's mark and sweep garbage collector. However, if the program exits, Ruby does not guarantee the order in which objects are freed (see blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/17700). As a result, the associated document may be freed before the node list, which will cause a segmentation fault. To avoid this, use the following (non-ruby like) coding style:

nodes = doc.find('/header')
nodes.each do |node|
  ... do stuff ...
end

# nodes = nil # GC.start

# File lib/libxml/document.rb, line 121
def find(xpath, nslist = nil)
  self.context(nslist).find(xpath)
end
find_first(xpath, nslist = nil) click to toggle source

Return the first node matching the specified xpath expression. For more information, please refer to the documentation for #find.

# File lib/libxml/document.rb, line 128
def find_first(xpath, nslist = nil)
  find(xpath, nslist).first
end
node_type_name() click to toggle source

Returns this node's type name

# File lib/libxml/document.rb, line 133
def node_type_name
  case node_type
  when XML::Node::DOCUMENT_NODE
    'document_xml'
  when XML::Node::DOCB_DOCUMENT_NODE
    'document_docbook'
  when XML::Node::HTML_DOCUMENT_NODE
    'document_html'
  else
    raise(UnknownType, "Unknown node type: %n", node.node_type);
  end
end