class Nokogiri::XML::Document


Nokogiri::XML::Searchable#xpath
For searching a Document, see Nokogiri::XML::Searchable#css and
See Nokogiri::XML::Document.parse() for more information on parsing.
XML documents. The Document is created by parsing an XML document.
Nokogiri::XML::Document is the main entry point for dealing with
#

def self.empty_doc? string_or_io

def self.empty_doc? string_or_io
  string_or_io.nil? ||
    (string_or_io.respond_to?(:empty?) && string_or_io.empty?) ||
    (string_or_io.respond_to?(:eof?) && string_or_io.eof?)
end

def self.parse string_or_io, url = nil, encoding = nil, options = ParseOptions::DEFAULT_XML


Nokogiri.XML() is a convenience method which will call this method.

set) by default.
and that module's DEFAULT_XML constant for what's set (and not
Nokogiri::XML::ParseOptions for a complete list of options;
does not attempt to load DTDs or access the network. See
By default, Nokogiri treats documents as untrusted, and so

parse options may be set.
+block+ (optional) is passed a configuration object on which

Nokogiri::XML::ParseOptions for more information.
parsing, such as Nokogiri::XML::ParseOptions::RECOVER. See the
+options+ (optional) is a configuration object that sets options during

the document.
+encoding+ (optional) is the encoding that should be used when processing

+url+ (optional) is the URI where this document is located.

_read_ and _close_ such as an IO, or StringIO.
+string_or_io+ may be a String, or any object that responds to

Parse an XML file.
#
def self.parse string_or_io, url = nil, encoding = nil, options = ParseOptions::DEFAULT_XML
  options = Nokogiri::XML::ParseOptions.new(options) if Integer === options
  yield options if block_given?
  url ||= string_or_io.respond_to?(:path) ? string_or_io.path : nil
  if empty_doc?(string_or_io)
    if options.strict?
      raise Nokogiri::XML::SyntaxError.new("Empty document")
    else
      return encoding ? new.tap { |i| i.encoding = encoding } : new
    end
  end
  doc = if string_or_io.respond_to?(:read)
          if string_or_io.is_a?(Pathname)
            # resolve the Pathname to the file and open it as an IO object, see #2110
            string_or_io = string_or_io.expand_path.open
            url ||= string_or_io.path
          end
          read_io(string_or_io, url, encoding, options.to_i)
        else
          # read_memory pukes on empty docs
          read_memory(string_or_io, url, encoding, options.to_i)
        end
  # do xinclude processing
  doc.do_xinclude(options) if options.xinclude?
  return doc
end

def add_child node_or_tags

def add_child node_or_tags
  raise "A document may not have multiple root nodes." if (root && root.name != 'nokogiri_text_wrapper') && !(node_or_tags.comment? || node_or_tags.processing_instruction?)
  node_or_tags = coerce(node_or_tags)
  if node_or_tags.is_a?(XML::NodeSet)
    raise "A document may not have multiple root nodes." if node_or_tags.size > 1
    super(node_or_tags.first)
  else
    super
  end
end

def collect_namespaces


implementation of the underlying XML library.
namespaces, and as a result the order may be dependent on the
Note that this method does an xpath lookup for nodes with

in the hash.
Non-prefixed default namespaces (as in "xmlns=") are not included

The hash returned will look like this: { 'xmlns:foo' => 'bar' }





For example, given this document:
WARNING: this method will clobber duplicate names in the keys.

{ 'xmlns:foo' => 'bar', 'xmlns:hello' => 'world' }

This method will return:





For example, given this document:

return them as a hash.
Recursively get all namespaces from this node and its subtree and
#
def collect_namespaces
  xpath("//namespace::*").inject({}) do |hash, ns|
    hash[["xmlns",ns.prefix].compact.join(":")] = ns.href if ns.prefix != "xml"
    hash
  end
end

def create_cdata string, &block

Create a CDATA Node containing +string+
def create_cdata string, &block
  Nokogiri::XML::CDATA.new self, string.to_s, &block
end

def create_comment string, &block

Create a Comment Node containing +string+
def create_comment string, &block
  Nokogiri::XML::Comment.new self, string.to_s, &block
end

def create_element(name, *contents_or_attrs, &block)

Other tags:
    Example: Passing a block to mutate the element -
    Example: An element with contents and attributes -
    Example: An element with attributes -
    Example: An element with contents -
    Example: An empty element without attributes -

Returns:
  • (Nokogiri::XML::Element) -

Other tags:
    Yieldparam: node -

Parameters:
  • contents_or_attrs (#to_s, Hash) --
  • name (String) --
def create_element(name, *contents_or_attrs, &block)
  elm = Nokogiri::XML::Element.new(name, self, &block)
  contents_or_attrs.each do |arg|
    case arg
    when Hash
      arg.each do |k, v|
        key = k.to_s
        if key =~ NCNAME_RE
          ns_name = Regexp.last_match(1)
          elm.add_namespace_definition(ns_name, v)
        else
          elm[k.to_s] = v.to_s
        end
      end
    else
      elm.content = arg
    end
  end
  if ns = elm.namespace_definitions.find { |n| n.prefix.nil? || (n.prefix == '') }
    elm.namespace = ns
  end
  elm
end

def create_text_node string, &block

Create a Text Node with +string+
def create_text_node string, &block
  Nokogiri::XML::Text.new string.to_s, self, &block
end

def decorate node

Apply any decorators to +node+
#
def decorate node
  return unless @decorators
  @decorators.each { |klass,list|
    next unless node.is_a?(klass)
    list.each { |moodule| node.extend(moodule) }
  }
end

def decorators key

Get the list of decorators given +key+
def decorators key
  @decorators ||= Hash.new
  @decorators[key] ||= []
end

def document

A reference to +self+
def document
  self
end

def fragment tags = nil

Returns an empty fragment if +tags+ is nil.
Create a Nokogiri::XML::DocumentFragment from +tags+
#
def fragment tags = nil
  DocumentFragment.new(self, tags, self.root)
end

def initialize *args # :nodoc:

:nodoc:
def initialize *args # :nodoc:
  @errors     = []
  @decorators = nil
end

def inspect_attributes

def inspect_attributes
  [:name, :children]
end

def name

The name of this document. Always returns "document"
def name
  'document'
end

def namespaces

Get the hash of namespaces on the root Nokogiri::XML::Node
def namespaces
  root ? root.namespaces : {}
end

def slop!


... which does absolutely nothing.
irb> doc.slop!
... followed by irb's implicit inspect (and therefore instantiation of every node) ...
irb> doc = Nokogiri::HTML my_markup

and not

irb> doc = Nokogiri::Slop my_markup

irb, the preferred idiom is:
is called will not be decorated with sloppy behavior. So, if you're in
Note that any nodes that have been instantiated before #slop!

Explore a document with shortcut methods. See Nokogiri::Slop for details.
#
def slop!
  unless decorators(XML::Node).include? Nokogiri::Decorators::Slop
    decorators(XML::Node) << Nokogiri::Decorators::Slop
    decorate!
  end
  self
end

def validate

the document or +nil+ when there is no DTD.
Validate this Document against it's DTD. Returns a list of errors on
#
def validate
  return nil unless internal_subset
  internal_subset.validate self
end