Nokogiri::XML::Node is your window to the fun filled world of dealing with XML and HTML tags. A Nokogiri::XML::Node may be treated similarly to a hash with regard to attributes. For example (from irb):

irb(main):004:0> node
=> <a href="#foo" id="link">link</a>
irb(main):005:0> node['href']
=> "#foo"
irb(main):006:0> node.keys
=> ["href", "id"]
irb(main):007:0> node.values
=> ["#foo", "link"]
irb(main):008:0> node['class'] = 'green'
=> "green"
irb(main):009:0> node
=> <a href="#foo" id="link" class="green">link</a>
irb(main):010:0>

See #[] and Nokogiri::XML#[]= for more information.

Nokogiri::XML::Node also has methods that let you move around your tree. For navigating your tree, see:

  • Nokogiri::XML::Node#parent

  • Nokogiri::XML::Node#children

  • Nokogiri::XML::Node#next

  • Nokogiri::XML::Node#previous

When printing or otherwise emitting a document or a node (and its subtree), there are a few methods you might want to use:

  • content, text, inner_text, to_str: emit plaintext

    These methods will all emit the plaintext version of your document, meaning that entities will be replaced (e.g., “&lt;” will be replaced with “<”), meaning that any sanitizing will likely be un-done in the output.

  • #to_s, #to_xml, #to_html, #inner_html: emit well-formed markup

    These methods will all emit properly-escaped markup, meaning that it's suitable for consumption by browsers, parsers, etc.

You may search this node's subtree using Nokogiri::XML::Searchable#xpath and Nokogiri::XML::Searchable#css

Namespace
Methods
#
A
B
C
D
E
F
G
H
I
K
M
N
P
R
S
T
V
W
X
Included Modules
Constants
ELEMENT_NODE = 1
 

Element node type, see #element?

ATTRIBUTE_NODE = 2
 

Attribute node type

TEXT_NODE = 3
 

Text node type, see #text?

CDATA_SECTION_NODE = 4
 

CDATA node type, see #cdata?

ENTITY_REF_NODE = 5
 

Entity reference node type

ENTITY_NODE = 6
 

Entity node type

PI_NODE = 7
 

PI node type

COMMENT_NODE = 8
 

Comment node type, see #comment?

DOCUMENT_NODE = 9
 

Document node type, see #xml?

DOCUMENT_TYPE_NODE = 10
 

Document type node type

DOCUMENT_FRAG_NODE = 11
 

Document fragment node type

NOTATION_NODE = 12
 

Notation node type

HTML_DOCUMENT_NODE = 13
 

HTML document node type, see #html?

DTD_NODE = 14
 

DTD node type

ELEMENT_DECL = 15
 

Element declaration type

ATTRIBUTE_DECL = 16
 

Attribute declaration type

ENTITY_DECL = 17
 

Entity declaration type

NAMESPACE_DECL = 18
 

Namespace declaration type

XINCLUDE_START = 19
 

XInclude start type

XINCLUDE_END = 20
 

XInclude end type

DOCB_DOCUMENT_NODE = 21
 

DOCB document node type

IMPLIED_XPATH_CONTEXTS = [ './/'.freeze ].freeze # :nodoc:
 

@private

Instance Public methods
<<(node_or_tags)

Add node_or_tags as a child of this Node. node_or_tags can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.

Returns self, to support chaining of calls (e.g., root << child1 << child2)

Also see related method add_child.

# File lib/nokogiri/xml/node.rb, line 170
def << node_or_tags
  add_child node_or_tags
  self
end
<=>(other)

Compare two Node objects with respect to their Document. Nodes from different documents cannot be compared.

# File lib/nokogiri/xml/node.rb, line 731
def <=> other
  return nil unless other.is_a?(Nokogiri::XML::Node)
  return nil unless document == other.document
  compare other
end
==(other)

Test to see if this Node is equal to other

# File lib/nokogiri/xml/node.rb, line 596
def == other
  return false unless other
  return false unless other.respond_to?(:pointer_id)
  pointer_id == other.pointer_id
end
>(selector)

Search this node's immediate children using CSS selector selector

# File lib/nokogiri/xml/node.rb, line 112
def > selector
  ns = document.root.namespaces
  xpath CSS.xpath_for(selector, :prefix => "./", :ns => ns).first
end
[](name)

Get the attribute value for the attribute name

Also aliased as: get_attribute, attr
# File lib/nokogiri/xml/node.rb, line 119
def [] name
  get(name.to_s)
end
[]=(name, value)

Set the attribute value for the attribute name to value

Also aliased as: set_attribute
# File lib/nokogiri/xml/node.rb, line 125
def []= name, value
  set name.to_s, value.to_s
end
accept(visitor)

Accept a visitor. This method calls “visit” on visitor with self.

# File lib/nokogiri/xml/node.rb, line 590
def accept visitor
  visitor.visit(self)
end
add_child(node_or_tags)

Add node_or_tags as a child of this Node. node_or_tags can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.

Returns the reparented node (if node_or_tags is a Node), or NodeSet (if node_or_tags is a DocumentFragment, NodeSet, or string).

Also see related method +<<+.

# File lib/nokogiri/xml/node.rb, line 136
def add_child node_or_tags
  node_or_tags = coerce(node_or_tags)
  if node_or_tags.is_a?(XML::NodeSet)
    node_or_tags.each { |n| add_child_node_and_reparent_attrs n }
  else
    add_child_node_and_reparent_attrs node_or_tags
  end
  node_or_tags
end
add_next_sibling(node_or_tags)

Insert node_or_tags after this Node (as a sibling). node_or_tags can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.

Returns the reparented node (if node_or_tags is a Node), or NodeSet (if node_or_tags is a DocumentFragment, NodeSet, or string).

Also see related method after.

# File lib/nokogiri/xml/node.rb, line 195
def add_next_sibling node_or_tags
  raise ArgumentError.new("A document may not have multiple root nodes.") if (parent && parent.document?) && !(node_or_tags.comment? || node_or_tags.processing_instruction?)

  add_sibling :next, node_or_tags
end
add_previous_sibling(node_or_tags)

Insert node_or_tags before this Node (as a sibling). node_or_tags can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.

Returns the reparented node (if node_or_tags is a Node), or NodeSet (if node_or_tags is a DocumentFragment, NodeSet, or string).

Also see related method before.

Also aliased as: previous=
# File lib/nokogiri/xml/node.rb, line 182
def add_previous_sibling node_or_tags
  raise ArgumentError.new("A document may not have multiple root nodes.") if (parent && parent.document?) && !(node_or_tags.comment? || node_or_tags.processing_instruction?)

  add_sibling :previous, node_or_tags
end
after(node_or_tags)

Insert node_or_tags after this node (as a sibling). node_or_tags can be a Nokogiri::XML::Node, a Nokogiri::XML::DocumentFragment, or a string containing markup.

Returns self, to support chaining of calls.

Also see related method add_next_sibling.

# File lib/nokogiri/xml/node.rb, line 220
def after node_or_tags
  add_next_sibling node_or_tags
  self
end
ancestors(selector = nil)

Get a list of ancestor Node for this Node. If selector is given, the ancestors must match selector

# File lib/nokogiri/xml/node.rb, line 530
def ancestors selector = nil
  return NodeSet.new(document) unless respond_to?(:parent)
  return NodeSet.new(document) unless parent

  parents = [parent]

  while parents.last.respond_to?(:parent)
    break unless ctx_parent = parents.last.parent
    parents << ctx_parent
  end

  return NodeSet.new(document, parents) unless selector

  root = parents.last
  search_results = root.search(selector)

  NodeSet.new(document, parents.find_all { |parent|
    search_results.include?(parent)
  })
end
attr(name)
Alias for: []
attributes()

Returns a hash containing the node's attributes. The key is the attribute name without any namespace, the value is a Nokogiri::XML::Attr representing the attribute. If you need to distinguish attributes with the same name, with different namespaces use attribute_nodes instead.

# File lib/nokogiri/xml/node.rb, line 326
def attributes
  Hash[attribute_nodes.map { |node|
    [node.node_name, node]
  }]
end
before(node_or_tags)

Insert node_or_tags before this node (as a sibling). node_or_tags can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.

Returns self, to support chaining of calls.

Also see related method add_previous_sibling.

# File lib/nokogiri/xml/node.rb, line 208
def before node_or_tags
  add_previous_sibling node_or_tags
  self
end
canonicalize(mode=XML::XML_C14N_1_0,inclusive_namespaces=nil,with_comments=false)
# File lib/nokogiri/xml/node.rb, line 751
def canonicalize(mode=XML::XML_C14N_1_0,inclusive_namespaces=nil,with_comments=false)
  c14n_root = self
  document.canonicalize(mode, inclusive_namespaces, with_comments) do |node, parent|
    tn = node.is_a?(XML::Node) ? node : parent
    tn == c14n_root || tn.ancestors.include?(c14n_root)
  end
end
cdata?()

Returns true if this is a CDATA

# File lib/nokogiri/xml/node.rb, line 453
def cdata?
  type == CDATA_SECTION_NODE
end
children=(node_or_tags)

Set the inner html for this Node node_or_tags node_or_tags can be a Nokogiri::XML::Node, a Nokogiri::XML::DocumentFragment, or a string containing markup.

Returns the reparented node (if node_or_tags is a Node), or NodeSet (if node_or_tags is a DocumentFragment, NodeSet, or string).

Also see related method inner_html=

# File lib/nokogiri/xml/node.rb, line 244
def children= node_or_tags
  node_or_tags = coerce(node_or_tags)
  children.unlink
  if node_or_tags.is_a?(XML::NodeSet)
    node_or_tags.each { |n| add_child_node_and_reparent_attrs n }
  else
    add_child_node_and_reparent_attrs node_or_tags
  end
  node_or_tags
end
comment?()

Returns true if this is a Comment

# File lib/nokogiri/xml/node.rb, line 448
def comment?
  type == COMMENT_NODE
end
content=(string)

Set the Node's content to a Text node containing string. The string gets XML escaped, not interpreted as markup.

# File lib/nokogiri/xml/node.rb, line 415
def content= string
  self.native_content = encode_special_chars(string.to_s)
end
css_path()

Get the path to this node as a CSS expression

# File lib/nokogiri/xml/node.rb, line 521
def css_path
  path.split(/\//).map { |part|
    part.length == 0 ? nil : part.gsub(/\[(\d+)\]/, ':nth-of-type(\1)')
  }.compact.join(' > ')
end
decorate!()

Decorate this node with the decorators set up in this node's Document

# File lib/nokogiri/xml/node.rb, line 106
def decorate!
  document.decorate(self)
end
default_namespace=(url)

Adds a default namespace supplied as a string url href, to self. The consequence is as an xmlns attribute with supplied argument were present in parsed XML. A default namespace set with this method will now show up in attributes, but when this node is serialized to XML an “xmlns” attribute will appear. See also namespace and namespace=

# File lib/nokogiri/xml/node.rb, line 557
def default_namespace= url
  add_namespace_definition(nil, url)
end
delete(name)
Alias for: remove_attribute
description()

Fetch the Nokogiri::HTML::ElementDescription for this node. Returns nil on XML documents and on unknown tags.

# File lib/nokogiri/xml/node.rb, line 490
def description
  return nil if document.xml?
  Nokogiri::HTML::ElementDescription[name]
end
do_xinclude(options = XML::ParseOptions::DEFAULT_XML, &block)

Do xinclude substitution on the subtree below node. If given a block, a Nokogiri::XML::ParseOptions object initialized from options, will be passed to it, allowing more convenient modification of the parser options.

# File lib/nokogiri/xml/node.rb, line 741
def do_xinclude options = XML::ParseOptions::DEFAULT_XML, &block
  options = Nokogiri::XML::ParseOptions.new(options) if Integer === options

  # give options to user
  yield options if block_given?

  # call c extension
  process_xincludes(options.to_i)
end
document?()

Returns true if this is a Document

# File lib/nokogiri/xml/node.rb, line 468
def document?
  is_a? XML::Document
end
each()

Iterate over each attribute name and value pair for this Node.

# File lib/nokogiri/xml/node.rb, line 346
def each
  attribute_nodes.each { |node|
    yield [node.node_name, node.value]
  }
end
elem?()
Alias for: element?
element?()

Returns true if this is an Element node

Also aliased as: elem?
# File lib/nokogiri/xml/node.rb, line 503
def element?
  type == ELEMENT_NODE
end
fragment(tags)

Create a DocumentFragment containing tags that is relative to this context node.

# File lib/nokogiri/xml/node.rb, line 370
def fragment tags
  type = document.html? ? Nokogiri::HTML : Nokogiri::XML
  type::DocumentFragment.new(document, tags, self)
end
fragment?()

Returns true if this is a DocumentFragment

# File lib/nokogiri/xml/node.rb, line 483
def fragment?
  type == DOCUMENT_FRAG_NODE
end
get_attribute(name)
Alias for: []
html?()

Returns true if this is an HTML::Document node

# File lib/nokogiri/xml/node.rb, line 463
def html?
  type == HTML_DOCUMENT_NODE
end
inner_html(*args)

Get the #inner_html for this node's Node#children

# File lib/nokogiri/xml/node.rb, line 516
def inner_html *args
  children.map { |x| x.to_html(*args) }.join
end
inner_html=(node_or_tags)

Set the inner html for this Node to node_or_tags node_or_tags can be a Nokogiri::XML::Node, a Nokogiri::XML::DocumentFragment, or a string containing markup.

Returns self.

Also see related method children=

# File lib/nokogiri/xml/node.rb, line 232
def inner_html= node_or_tags
  self.children = node_or_tags
  self
end
keys()

Get the attribute names for this Node.

# File lib/nokogiri/xml/node.rb, line 340
def keys
  attribute_nodes.map(&:node_name)
end
matches?(selector)

Returns true if this Node matches selector

# File lib/nokogiri/xml/node.rb, line 363
def matches? selector
  ancestors.last.search(selector).include?(self)
end
namespace=(ns)

Set the default namespace on this node (as would be defined with an “xmlns=” attribute in XML source), as a Namespace object ns. Note that a Namespace added this way will NOT be serialized as an xmlns attribute for this node. You probably want default_namespace= instead, or perhaps add_namespace_definition with a nil prefix argument.

# File lib/nokogiri/xml/node.rb, line 568
def namespace= ns
  return set_namespace(ns) unless ns

  unless Nokogiri::XML::Namespace === ns
    raise TypeError, "#{ns.class} can't be coerced into Nokogiri::XML::Namespace"
  end
  if ns.document != document
    raise ArgumentError, 'namespace must be declared on the same document'
  end

  set_namespace ns
end
namespaces()

Returns a Hash of {prefix => value} for all namespaces on this node and its ancestors.

This method returns the same namespaces as namespace_scopes.

Returns namespaces in scope for self – those defined on self element directly or any ancestor node – as a Hash of attribute-name/value pairs. Note that the keys in this hash XML attributes that would be used to define this namespace, such as “xmlns:prefix”, not just the prefix. Default namespace set on self will be included with key “xmlns”. However, default namespaces set on ancestor will NOT be, even if self has no explicit default namespace.

# File lib/nokogiri/xml/node.rb, line 440
def namespaces
  Hash[namespace_scopes.map { |nd|
    key = ['xmlns', nd.prefix].compact.join(':')
    [key, nd.href]
  }]
end
parent=(parent_node)

Set the parent Node for this Node

# File lib/nokogiri/xml/node.rb, line 421
def parent= parent_node
  parent_node.add_child(self)
  parent_node
end
parse(string_or_io, options = nil)

Parse string_or_io as a document fragment within the context of this node. Returns a XML::NodeSet containing the nodes parsed from string_or_io.

# File lib/nokogiri/xml/node.rb, line 379
def parse string_or_io, options = nil
  ##
  # When the current node is unparented and not an element node, use the
  # document as the parsing context instead. Otherwise, the in-context
  # parser cannot find an element or a document node.
  # Document Fragments are also not usable by the in-context parser.
  if !element? && !document? && (!parent || parent.fragment?)
    return document.parse(string_or_io, options)
  end

  options ||= (document.html? ? ParseOptions::DEFAULT_HTML : ParseOptions::DEFAULT_XML)
  if Integer === options
    options = Nokogiri::XML::ParseOptions.new(options)
  end
  # Give the options to the user
  yield options if block_given?

  contents = string_or_io.respond_to?(:read) ?
    string_or_io.read :
    string_or_io

  return Nokogiri::XML::NodeSet.new(document) if contents.empty?

  ##
  # This is a horrible hack, but I don't care. See #313 for background.
  error_count = document.errors.length
  node_set = in_context(contents, options.to_i)
  if node_set.empty? and document.errors.length > error_count and options.recover?
    fragment = Nokogiri::HTML::DocumentFragment.parse contents
    node_set = fragment.children
  end
  node_set
end
prepend_child(node_or_tags)

Add node_or_tags as the first child of this Node. node_or_tags can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.

Returns the reparented node (if node_or_tags is a Node), or NodeSet (if node_or_tags is a DocumentFragment, NodeSet, or string).

Also see related method add_child.

# File lib/nokogiri/xml/node.rb, line 153
def prepend_child node_or_tags
  if first = children.first
    # Mimic the error add_child would raise.
    raise RuntimeError, "Document already has a root node" if document? && !(node_or_tags.comment? || node_or_tags.processing_instruction?)
    first.__send__(:add_sibling, :previous, node_or_tags)
  else
    add_child(node_or_tags)
  end
end
previous=(node_or_tags)
processing_instruction?()

Returns true if this is a ProcessingInstruction node

# File lib/nokogiri/xml/node.rb, line 473
def processing_instruction?
  type == PI_NODE
end
read_only?()

Is this a read only node?

# File lib/nokogiri/xml/node.rb, line 497
def read_only?
  # According to gdome2, these are read-only node types
  [NOTATION_NODE, ENTITY_NODE, ENTITY_DECL].include?(type)
end
remove_attribute(name)

Remove the attribute named name

Also aliased as: delete
# File lib/nokogiri/xml/node.rb, line 354
def remove_attribute name
  attr = attributes[name].remove if key? name
  clear_xpath_context if Nokogiri.jruby?
  attr
end
replace(node_or_tags)

Replace this Node with node_or_tags. node_or_tags can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.

Returns the reparented node (if node_or_tags is a Node), or NodeSet (if node_or_tags is a DocumentFragment, NodeSet, or string).

Also see related method swap.

# File lib/nokogiri/xml/node.rb, line 262
def replace node_or_tags
  # We cannot replace a text node directly, otherwise libxml will return
  # an internal error at parser.c:13031, I don't know exactly why
  # libxml is trying to find a parent node that is an element or document
  # so I can't tell if this is bug in libxml or not. issue #775.
  if text?
    replacee = Nokogiri::XML::Node.new 'dummy', document
    add_previous_sibling_node replacee
    unlink
    return replacee.replace node_or_tags
  end

  node_or_tags = coerce(node_or_tags)

  if node_or_tags.is_a?(XML::NodeSet)
    node_or_tags.each { |n| add_previous_sibling n }
    unlink
  else
    replace_node node_or_tags
  end
  node_or_tags
end
serialize(*args, &block)

Serialize Node using options. Save options can also be set using a block. See SaveOptions.

These two statements are equivalent:

node.serialize(:encoding => 'UTF-8', :save_with => FORMAT | AS_XML)

or

node.serialize(:encoding => 'UTF-8') do |config|
  config.format.as_xml
end
# File lib/nokogiri/xml/node.rb, line 616
def serialize *args, &block
  options = args.first.is_a?(Hash) ? args.shift : {
    :encoding   => args[0],
    :save_with  => args[1]
  }

  encoding = options[:encoding] || document.encoding
  options[:encoding] = encoding

  outstring = String.new
  if encoding && outstring.respond_to?(:force_encoding)
    outstring.force_encoding(Encoding.find(encoding))
  end
  io = StringIO.new(outstring)
  write_to io, options, &block
  io.string
end
set_attribute(name, value)
Alias for: []=
swap(node_or_tags)

Swap this Node for node_or_tags node_or_tags can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a string containing markup.

Returns self, to support chaining of calls.

Also see related method replace.

# File lib/nokogiri/xml/node.rb, line 292
def swap node_or_tags
  replace node_or_tags
  self
end
text?()

Returns true if this is a Text node

# File lib/nokogiri/xml/node.rb, line 478
def text?
  type == TEXT_NODE
end
to_html(options = {})

Serialize this Node to HTML

doc.to_html

See #write_to for a list of options. For formatted output, use #to_xhtml instead.

# File lib/nokogiri/xml/node.rb, line 641
def to_html options = {}
  to_format SaveOptions::DEFAULT_HTML, options
end
to_s()

Turn this node in to a string. If the document is HTML, this method returns html. If the document is XML, this method returns XML.

# File lib/nokogiri/xml/node.rb, line 511
def to_s
  document.xml? ? to_xml : to_html
end
to_xhtml(options = {})

Serialize this Node to XHTML using options

doc.to_xhtml(:indent => 5, :encoding => 'UTF-8')

See #write_to for a list of options

# File lib/nokogiri/xml/node.rb, line 662
def to_xhtml options = {}
  to_format SaveOptions::DEFAULT_XHTML, options
end
to_xml(options = {})

Serialize this Node to XML using options

doc.to_xml(:indent => 5, :encoding => 'UTF-8')

See #write_to for a list of options

# File lib/nokogiri/xml/node.rb, line 651
def to_xml options = {}
  options[:save_with] ||= SaveOptions::DEFAULT_XML
  serialize(options)
end
traverse(&block)

Yields self and all children to block recursively.

# File lib/nokogiri/xml/node.rb, line 583
def traverse &block
  children.each{|j| j.traverse(&block) }
  block.call(self)
end
values()

Get the attribute values for this Node.

# File lib/nokogiri/xml/node.rb, line 334
def values
  attribute_nodes.map(&:value)
end
write_html_to(io, options = {})

Write Node as HTML to io with options

See #write_to for a list of options

# File lib/nokogiri/xml/node.rb, line 705
def write_html_to io, options = {}
  write_format_to SaveOptions::DEFAULT_HTML, io, options
end
write_to(io, *options)

Write Node to io with options. options modify the output of this method. Valid options are:

  • :encoding for changing the encoding

  • :indent_text the indentation text, defaults to one space

  • :indent the number of :indent_text to use, defaults to 2

  • :save_with a combination of SaveOptions constants.

To save with UTF-8 indented twice:

node.write_to(io, :encoding => 'UTF-8', :indent => 2)

To save indented with two dashes:

node.write_to(io, :indent_text => '-', :indent => 2
# File lib/nokogiri/xml/node.rb, line 683
def write_to io, *options
  options       = options.first.is_a?(Hash) ? options.shift : {}
  encoding      = options[:encoding] || options[0]
  if Nokogiri.jruby?
    save_options  = options[:save_with] || options[1]
    indent_times  = options[:indent] || 0
  else
    save_options  = options[:save_with] || options[1] || SaveOptions::FORMAT
    indent_times  = options[:indent] || 2
  end
  indent_text   = options[:indent_text] || ' '

  config = SaveOptions.new(save_options.to_i)
  yield config if block_given?

  native_write_to(io, encoding, indent_text * indent_times, config.options)
end
write_xhtml_to(io, options = {})

Write Node as XHTML to io with options

See #write_to for a list of options

# File lib/nokogiri/xml/node.rb, line 713
def write_xhtml_to io, options = {}
  write_format_to SaveOptions::DEFAULT_XHTML, io, options
end
write_xml_to(io, options = {})

Write Node as XML to io with options

doc.write_xml_to io, :encoding => 'UTF-8'

See #write_to for a list of options

# File lib/nokogiri/xml/node.rb, line 723
def write_xml_to io, options = {}
  options[:save_with] ||= SaveOptions::DEFAULT_XML
  write_to io, options
end
xml?()

Returns true if this is an XML::Document node

# File lib/nokogiri/xml/node.rb, line 458
def xml?
  type == DOCUMENT_NODE
end