Class Nokogiri::XML::Document
In: lib/nokogiri/xml/document.rb
ext/nokogiri/xml_namespace.c
Parent: Node

Methods

External Aliases

serialize -> to_xml
dup -> clone

Attributes

errors  [RW]  A list of Nokogiri::XML::SyntaxError found when parsing a document

Public Class methods

Create a new document with version (defaults to "1.0")

Parse an XML file. thing may be a String, or any object that responds to read and close such as an IO, or StringIO. url is resource where this document is located. encoding is the encoding that should be used when processing the document. options is a number that sets options in the parser, such as Nokogiri::XML::ParseOptions::RECOVER. See the constants in Nokogiri::XML::ParseOptions.

Public Instance methods

<<(child)

Alias for add_child

Recursively get all namespaces from this node and its subtree and return them as a hash.

For example, given this document:

  <root xmlns:foo="bar">
    <bar xmlns:hello="world" />
  </root>

This method will return:

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

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

  <root xmlns:foo="bar">
    <bar xmlns:foo="baz" />
  </root>

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

Create an element with name

Create a text node with text

Apply any decorators to node

Get the list of decorators given key

A reference to self

Copy this Document. An optional depth may be passed in, but it defaults to a deep copy. 0 is a shallow copy, 1 is a deep copy.

Get the encoding for this Document

Set the encoding string for this Document

Create a Nokogiri::XML::DocumentFragment from tags Returns an empty fragment if tags is nil.

The name of this document. Always returns "document"

Remove all namespaces from all nodes in the document.

This could be useful for developers who either don‘t understand namespaces or don‘t care about them.

The following example shows a use case, and you can decide for yourself whether this is a good thing or not:

  doc = Nokogiri::XML <<-EOXML
     <root>
       <car xmlns:part="http://general-motors.com/">
         <part:tire>Michelin Model XGV</part:tire>
       </car>
       <bicycle xmlns:part="http://schwinn.com/">
         <part:tire>I'm a bicycle tire!</part:tire>
       </bicycle>
     </root>
     EOXML

  doc.xpath("//tire").to_s # => ""
  doc.xpath("//part:tire", "part" => "http://general-motors.com/").to_s # => "<part:tire>Michelin Model XGV</part:tire>"
  doc.xpath("//part:tire", "part" => "http://schwinn.com/").to_s # => "<part:tire>I'm a bicycle tire!</part:tire>"

  doc.remove_namespaces!

  doc.xpath("//tire").to_s # => "<tire>Michelin Model XGV</tire><tire>I'm a bicycle tire!</tire>"
  doc.xpath("//part:tire", "part" => "http://general-motors.com/").to_s # => ""
  doc.xpath("//part:tire", "part" => "http://schwinn.com/").to_s # => ""

For more information on why this probably is not a good thing in general, please direct your browser to tenderlovemaking.com/2009/04/23/namespaces-in-xml/

Get the root node for this document.

Set the root element on this document

Explore a document with shortcut methods.

Get the url name for this document.

Validate this Document against it‘s DTD. Returns a list of errors on the document or nil when there is no DTD.

Get the XML version for this Document

[Validate]