class Nokogiri::XML::Node


See the method group entitled Node@Searching+via+XPath+or+CSS+Queries for the full set of methods.
You may search this node’s subtree using methods like #xpath and #css.
== Searching
See the method group entitled Node@Serialization+and+Generating+Output for the full set of methods.
consumption by browsers, parsers, etc.
These methods will all **emit properly-escaped markup**, meaning that it’s suitable for
[#to_s, #to_xml, #to_html, #inner_html]
that any sanitizing will likely be un-done in the output.
meaning that entities will be replaced (e.g., +&lt;+ will be replaced with +<+), meaning
These methods will all **emit plaintext**,
[#content, #text, #inner_text, #to_str]
methods you might want to use:
When printing or otherwise emitting a document or a node (and its subtree), there are a few
== Serialization
See the method group entitled Node@Traversing+Document+Structure for the full set of methods.
Navigate up, down, or through siblings.
[#parent, #children, #next, #previous]
Nokogiri::XML::Node also has methods that let you move around your tree:
== Navigation
See the method group entitled Node@Working+With+Node+Attributes for the full set of methods.
node.to_html # => “<a href="#foo" id="link" class="green">link</a>”<br>node = ‘green’ # => “green”
node.values # => [“#foo”, “link”]
node.keys # => [“href”, “id”]<br>node # => “#foo”
node.to_html # => “<a href="#foo" id="link">link</a>”
node = Nokogiri::XML::DocumentFragment.parse(“<a href=‘#foo’ id=‘link’>link</a>”).at_css(“a”)
example:
A Nokogiri::XML::Node may be treated similarly to a hash with regard to attributes. For
== Attributes
Nokogiri::XML::Node is the primary API you’ll use to interact with your Document.

def <<(node_or_tags)

Also see related method +add_child+.

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

containing markup.
+node_or_tags+ can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a String

Add +node_or_tags+ as a child of this Node.
##
def <<(node_or_tags)
  add_child(node_or_tags)
  self
end

def <=>(other)

different documents cannot be compared.
Compare two Node objects with respect to their Document. Nodes from
##
def <=>(other)
  return unless other.is_a?(Nokogiri::XML::Node)
  return unless document == other.document
  compare(other)
end

def ==(other)

Test to see if this Node is equal to +other+
##
def ==(other)
  return false unless other
  return false unless other.respond_to?(:pointer_id)
  pointer_id == other.pointer_id
end

def [](name)


# => "broad"
doc.at_css("child").attribute_with_ns("size", "http://example.com/widths").value
doc.at_css("child").attribute("size").value # => "broad"
doc.at_css("child")["size"] # => nil
EOF



doc = Nokogiri::XML(<<~EOF)

⚠ Note namespaced attributes may be accessed with #attribute or #attribute_with_ns

*Example:* Namespaced attributes will not be returned.

child["class"] # => "big wide tall"
child["size"] # => "large"
child = doc.at_css("child")
doc = Nokogiri::XML("")

*Example*

[Returns] (String, nil) value of the attribute +name+, or +nil+ if no matching attribute exists

namespaced attributes, use #attribute_with_ns.
⚠ Note that attributes with namespaces cannot be accessed with this method. To access

Fetch an attribute from this node.

:call-seq: [](name) → (String, nil)
def [](name)
  get(name.to_s)
end

def []=(name, value)


# "\n"
# " \n" +
# => "\n" +
doc.to_html
child.attribute("size").namespace = ns
ns = doc.root.namespace_definitions.find { |ns| ns.prefix == "width" }
child["size"] = "broad"
child = doc.at_css("child")
EOF



doc = Nokogiri::XML(<<~EOF)

*Example:* Add a namespaced attribute.

# => ""
child.to_html
child["size"] = "broad"
child = doc.at_css("child")
doc = Nokogiri::XML("")

*Example*

[Returns] +value+

see the example below.
namespaced attributes for update, use #attribute_with_ns. To add a namespaced attribute,
⚠ Note that attributes with namespaces cannot be accessed with this method. To access

Update the attribute +name+ to +value+, or create the attribute if it does not exist.

:call-seq: []=(name, value) → value
def []=(name, value)
  set(name.to_s, value.to_s)
end

def accept(visitor)

Accept a visitor. This method calls "visit" on +visitor+ with self.
##
def accept(visitor)
  visitor.visit(self)
end

def add_child(node_or_tags)

Also see related method +<<+.

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

containing markup.
+node_or_tags+ can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a String

Add +node_or_tags+ as a child of this Node.
##
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

def add_child_node_and_reparent_attrs(node)

def add_child_node_and_reparent_attrs(node)
  add_child_node(node)
  node.attribute_nodes.find_all { |a| a.name.include?(":") }.each do |attr_node|
    attr_node.remove
    node[attr_node.name] = attr_node.value
  end
end

def add_class(names)


node.add_class(["section", "header"]) # =>

node # =>


*Example:* Ensure that the node has CSS classes "section" and "header", via an Array argument

node.add_class("section header") # =>

node # =>


Note also that the pre-existing duplicate CSS class "section" is not removed.
Note that the CSS class "section" is not added because it is already present.

*Example:* Ensure that the node has CSS classes "section" and "header", via a String argument

node.add_class("section") # =>
# duplicate not added
node.add_class("section") # =>

node # =>


*Example:* Ensure that the node has CSS class "section"

[Returns] +self+ (Node) for ease of chaining method calls.

exists, one is created.
will not be added. Any class names not present will be added. If no "class" attribute
whitespace-delimited names, or an Array of String names. Any class names already present
CSS class names to be added to the Node's "class" attribute. May be a string containing

- +names+ (String, Array)
[Parameters]

See related: #kwattr_add, #classes, #append_class, #remove_class

node.kwattr_add("class", names)

This is a convenience function and is equivalent to:

"class" attribute are not removed. Compare with #append_class.
in the "class" attribute are _not_ added. Note that any existing duplicates in the
Ensure HTML CSS classes are present on +self+. Any CSS classes in +names+ that already exist

:call-seq: add_class(names) → self
def add_class(names)
  kwattr_add("class", names)
end

def add_next_sibling(node_or_tags)

Also see related method +after+.

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

containing markup.
+node_or_tags+ can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a String

Insert +node_or_tags+ after this Node (as a sibling).
##
def add_next_sibling(node_or_tags)
  raise ArgumentError,
    "A document may not have multiple root nodes." if parent&.document? && !(node_or_tags.comment? || node_or_tags.processing_instruction?)
  add_sibling(:next, node_or_tags)
end

def add_previous_sibling(node_or_tags)

Also see related method +before+.

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

containing markup.
+node_or_tags+ can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a String

Insert +node_or_tags+ before this Node (as a sibling).
##
def add_previous_sibling(node_or_tags)
  raise ArgumentError,
    "A document may not have multiple root nodes." if parent&.document? && !(node_or_tags.comment? || node_or_tags.processing_instruction?)
  add_sibling(:previous, node_or_tags)
end

def add_sibling(next_or_previous, node_or_tags)

def add_sibling(next_or_previous, node_or_tags)
  raise("Cannot add sibling to a node with no parent") unless parent
  impl = next_or_previous == :next ? :add_next_sibling_node : :add_previous_sibling_node
  iter = next_or_previous == :next ? :reverse_each : :each
  node_or_tags = parent.coerce(node_or_tags)
  if node_or_tags.is_a?(XML::NodeSet)
    if text?
      pivot = Nokogiri::XML::Node.new("dummy", document)
      send(impl, pivot)
    else
      pivot = self
    end
    node_or_tags.send(iter) { |n| pivot.send(impl, n) }
    pivot.unlink if text?
  else
    send(impl, node_or_tags)
  end
  node_or_tags
end

def after(node_or_tags)

Also see related method +add_next_sibling+.

Returns +self+, to support chaining of calls.

containing markup.
+node_or_tags+ can be a Nokogiri::XML::Node, a Nokogiri::XML::DocumentFragment, or a String

Insert +node_or_tags+ after this node (as a sibling).
###
def after(node_or_tags)
  add_next_sibling(node_or_tags)
  self
end

def ancestors(selector = nil)

the ancestors must match +selector+
Get a list of ancestor Node for this Node. If +selector+ is given,
##
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 do |parent|
    search_results.include?(parent)
  end)
end

def append_class(names)


node.append_class(["section", "header"]) # =>

node.append_class(["section", "header"]) # =>

node # =>


*Example:* Append "section" and "header" to the node's CSS "class" attribute, via an Array argument

node.append_class("section header") # =>

node # =>


Note that the CSS class "section" is appended even though it is already present

*Example:* Append "section" and "header" to the noded's CSS "class" attribute, via a String argument

node.append_class("section") # =>
# duplicate added!
node.append_class("section") # =>

node # =>


*Example:* Append "section" to the node's CSS "class" attribute

[Returns] +self+ (Node) for ease of chaining method calls.

value. If no "class" attribute exists, one is created.
appended to the "class" attribute even if they are already present in the attribute
whitespace-delimited names, or an Array of String names. All class names passed in will be
CSS class names to be appended to the Node's "class" attribute. May be a string containing

- +names+ (String, Array)
[Parameters]

See related: #kwattr_append, #classes, #add_class, #remove_class

node.kwattr_append("class", names)

This is a convenience function and is equivalent to:

Add HTML CSS classes to +self+, regardless of duplication. Compare with #add_class.

:call-seq: append_class(names) → self
def append_class(names)
  kwattr_append("class", names)
end

def attributes


# })}
# value = "tall"
# }),
# href = "http://example.com/heights"
# prefix = "height",
# namespace = #(Namespace:0x564 {
# name = "size",
# #(Attr:0x550 {
# => {"size"=>
doc.at_css("child").attributes
EOF


xmlns:height='http://example.com/heights'>
doc = Nokogiri::XML(<<~EOF)

⚠ Note that only one of the attributes is returned in the Hash.

*Example* with an attribute name collision:

# })}
# value = "large"
# }),
# href = "http://example.com/sizes"
# prefix = "desc",
# namespace = #(Namespace:0x564 {
# name = "size",
# #(Attr:0x550 {
# => {"size"=>
doc.at_css("child").attributes
doc = Nokogiri::XML("")

*Example* with a namespace:

# "class"=>#(Attr:0x564 { name = "class", value = "big wide tall" })}
# => {"size"=>#(Attr:0x550 { name = "size", value = "large" }),
doc.at_css("child").attributes
doc = Nokogiri::XML("")

*Example* with no namespaces:

names (without the namespace), and the hash values are Nokogiri::XML::Attr.
Hash containing attributes belonging to +self+. The hash keys are String attribute
[Returns]

use #attribute_nodes.
simple name collision, not all attributes will be returned. In this case, you will need to
⚠ Because the keys do not include any namespace information for the attribute, in case of a

Fetch this node's attributes.

:call-seq: attributes() → Hash
def attributes
  attribute_nodes.each_with_object({}) do |node, hash|
    hash[node.node_name] = node
  end
end

def before(node_or_tags)

Also see related method +add_previous_sibling+.

Returns +self+, to support chaining of calls.

containing markup.
+node_or_tags+ can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a String

Insert +node_or_tags+ before this node (as a sibling).
###
def before(node_or_tags)
  add_previous_sibling(node_or_tags)
  self
end

def canonicalize(mode = XML::XML_C14N_1_0, inclusive_namespaces = nil, with_comments = false)

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

def cdata?

Returns true if this is a CDATA
def cdata?
  type == CDATA_SECTION_NODE
end

def children=(node_or_tags)

Also see related method +inner_html=+

containing markup.
+node_or_tags+ can be a Nokogiri::XML::Node, a Nokogiri::XML::DocumentFragment, or a String

Set the content for this Node +node_or_tags+
###
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
end

def classes


node.classes # => ["section", "title", "header"]
node # =>


*Example*

attribute is empty or non-existent, the return value is an empty array.
The CSS classes (Array of String) present in the Node's "class" attribute. If the
[Returns]

See related: #kwattr_values, #add_class, #append_class, #remove_class

node.kwattr_values("class")

This is a convenience function and is equivalent to:

Fetch CSS class names of a Node.

:call-seq: classes() → Array
def classes
  kwattr_values("class")
end

def coerce(data)

def coerce(data)
  case data
  when XML::NodeSet
    return data
  when XML::DocumentFragment
    return data.children
  when String
    return fragment(data).children
  when Document, XML::Attr
    # unacceptable
  when XML::Node
    return data
  end
  raise ArgumentError, <<~EOERR
    Requires a Node, NodeSet or String argument, and cannot accept a #{data.class}.
    (You probably want to select a node from the Document with at() or search(), or create a new Node via Node.new().)
  EOERR
end

def comment?

Returns true if this is a Comment
def comment?
  type == COMMENT_NODE
end

def content=(string)

interpreted as markup.
Set the Node's content to a Text node containing +string+. The string gets XML escaped, not
###
def content=(string)
  self.native_content = encode_special_chars(string.to_s)
end

def css_path

Get the path to this node as a CSS expression
def css_path
  path.split(%r{/}).filter_map do |part|
    part.empty? ? nil : part.gsub(/\[(\d+)\]/, ':nth-of-type(\1)')
  end.join(" > ")
end

def deconstruct_keys(keys)


Since v1.14.0

# })]}
# value = "def"
# }),
# href = "http://nokogiri.org/ns/noko"
# prefix = "noko",
# namespace = #(Namespace:0x398 {
# name = "bar",
# #(Attr:0x384 {
# [#(Attr:0x370 { name = "foo", value = "abc" }),
# => {:attributes=>
doc.root.elements.first.deconstruct_keys([:attributes])

# " Second\n"}
# " First\n" +
# "\n" +
# :inner_html=>
# => {:content=>"\n" + " First\n" + " Second\n",
doc.root.deconstruct_keys([:inner_html, :content])

# #(Namespace:0x35c { href = "http://nokogiri.org/ns/default" })}
# :namespace=>
# => {:name=>"parent",
doc.root.deconstruct_keys([:name, :namespace])

XML

Second
First


doc = Nokogiri::XML.parse(<<~XML)

*Example*

- +inner_html+ → (String) The inner markup for the children of this node. See #inner_html.
- +content+ → (String) The contents of all the text nodes in this node's subtree. See #content.
- +elements+ → (Array) The child elements of this node. 💡 Note this does not include text nodes.
- +children+ → (Array) The children of this node. 💡 Note this includes text nodes.
- +attributes+ → (Array) The attributes of this node.
- +namespace+ → (Namespace, nil) The namespace of this node, or nil if there is no namespace.
- +name+ → (String) The name of this node, or "text" if it is a Text node.
Valid keys and their values:

Returns a hash describing the Node, to use in pattern matching.

:call-seq: deconstruct_keys(array_of_names) → Hash
def deconstruct_keys(keys)
  requested_keys = DECONSTRUCT_KEYS & keys
  {}.tap do |values|
    requested_keys.each do |key|
      method = DECONSTRUCT_METHODS[key] || key
      values[key] = send(method)
    end
  end
end

def decorate!

Decorate this node with the decorators set up in this node's Document
##
def decorate!
  document.decorate(self)
end

def default_namespace=(url)

"xmlns" attribute will appear. See also #namespace and #namespace=
now show up in #attributes, but when this node is serialized to XML an
present in parsed XML. A default namespace set with this method will
The consequence is as an xmlns attribute with supplied argument were
Adds a default namespace supplied as a string +url+ href, to self.
##
def default_namespace=(url)
  add_namespace_definition(nil, url)
end

def description

nil on XML documents and on unknown tags.
Fetch the Nokogiri::HTML4::ElementDescription for this node. Returns
##
def description
  return if document.xml?
  Nokogiri::HTML4::ElementDescription[name]
end

def do_xinclude(options = XML::ParseOptions::DEFAULT_XML)

passed to it, allowing more convenient modification of the parser options.
Nokogiri::XML::ParseOptions object initialized from +options+, will be
Do xinclude substitution on the subtree below node. If given a block, a
##
def do_xinclude(options = XML::ParseOptions::DEFAULT_XML)
  options = Nokogiri::XML::ParseOptions.new(options) if Integer === options
  yield options if block_given?
  # call c extension
  process_xincludes(options.to_i)
end

def document?

Returns true if this is a Document
def document?
  is_a?(XML::Document)
end

def each

Iterate over each attribute name and value pair for this Node.
##
def each
  attribute_nodes.each do |node|
    yield [node.node_name, node.value]
  end
end

def element?

Returns true if this is an Element node
def element?
  type == ELEMENT_NODE
end

def fragment(tags)

context node.
Create a DocumentFragment containing +tags+ that is relative to _this_
##
def fragment(tags)
  document.related_class("DocumentFragment").new(document, tags, self)
end

def fragment?

Returns true if this is a DocumentFragment
def fragment?
  type == DOCUMENT_FRAG_NODE
end

def html?

Returns true if this is an HTML4::Document or HTML5::Document node
def html?
  type == HTML_DOCUMENT_NODE
end

def initialize(name, document)


[Returns] Nokogiri::XML::Node
[Yields] Nokogiri::XML::Node
- +document+ (Nokogiri::XML::Document) The document to which the the returned node will belong.
- +name+ (String)
[Parameters]

attributes but (like this method) avoids parsing markup.
Nokogiri::XML::Document#create_element which accepts additional arguments for contents or
Another alternative, if you are concerned about performance, is

both create an element (or subtree) and place it in the document tree.
Nokogiri::XML::Node methods like #add_child, #add_next_sibling, #replace, etc. which will
If you intend to add a node to a document tree, it's likely that you will prefer one of the

Create a new node with +name+ that belongs to +document+.

new(name, document) { |node| ... } -> Nokogiri::XML::Node
new(name, document) -> Nokogiri::XML::Node
:call-seq:
def initialize(name, document)
  # This is intentionally empty, and sets the method signature for subclasses.
end

def inner_html(*args)

Get the inner_html for this node's Node#children
def inner_html(*args)
  children.map { |x| x.to_html(*args) }.join
end

def inner_html=(node_or_tags)

Also see related method +children=+

parse the string as XML using XML::DocumentFragment.
using HTML4::DocumentFragment; but if the document is an XML::Document then it will
For example, if the document is an HTML4::Document then the string will be parsed as HTML4

node's document.
as HTML. A String argument will be parsed with the +DocumentFragment+ parser related to this
⚠ Please note that despite the name, this method will *not* always parse a String argument

containing markup.
+node_or_tags+ can be a Nokogiri::XML::Node, a Nokogiri::XML::DocumentFragment, or a String

Set the content for this Node to +node_or_tags+.
###
def inner_html=(node_or_tags)
  self.children = node_or_tags
end

def inspect_attributes

def inspect_attributes
  [:name, :namespace, :attribute_nodes, :children]
end

def keys

Get the attribute names for this Node.
##
def keys
  attribute_nodes.map(&:node_name)
end

def keywordify(keywords)

def keywordify(keywords)
  case keywords
  when Enumerable
    keywords
  when String
    keywords.scan(/\S+/)
  else
    raise ArgumentError,
      "Keyword attributes must be passed as either a String or an Enumerable, but received #{keywords.class}"
  end
end

def kwattr_add(attribute_name, keywords)

Since v1.11.0

node.kwattr_add("rel", ["nofollow", "noreferrer"]) # =>
node # =>

an Array argument.
*Example:* Ensure that a +Node+ has "nofollow" and "noreferrer" in its +rel+ attribute, via

node.kwattr_add("rel", "nofollow noreferrer") # =>
node # =>

pre-existing duplicate "nofollow" is not removed.
Note that "nofollow" is not added because it is already present. Note also that the

String argument.
*Example:* Ensure that a +Node+ has "nofollow" and "noreferrer" in its +rel+ attribute, via a

node.kwattr_add("rel", "nofollow") # =>
node.kwattr_add("rel", "nofollow") # =>
node # =>

Note that duplicates are not added.

*Example:* Ensure that a +Node+ has "nofollow" in its +rel+ attribute.

[Returns] +self+ (Nokogiri::XML::Node) for ease of chaining method calls.

it is created.
not be added. Any values not present will be added. If the named attribute does not exist,
whitespace-delimited values, or an Array of String values. Any values already present will
Keywords to be added to the attribute named +attribute_name+. May be a string containing
- +keywords+ (String, Array)
- +attribute_name+ (String) The name of the keyword attribute to be modified.
[Parameters]

See also #add_class, #kwattr_values, #kwattr_append, #kwattr_remove

{the "rel" attribute}[https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel].
contain CSS classes. But other keyword attributes exist, for instance
values. Perhaps the most familiar example of this is the HTML "class" attribute used to
A "keyword attribute" is a node attribute that contains a set of space-delimited

with #kwattr_append.
added. Note that any existing duplicates in the attribute values are not removed. Compare
Any values in +keywords+ that already exist in the Node's attribute values are _not_

Ensure that values are present in a keyword attribute.

kwattr_add(attribute_name, keywords) → self
:call-seq:
def kwattr_add(attribute_name, keywords)
  keywords = keywordify(keywords)
  current_kws = kwattr_values(attribute_name)
  new_kws = (current_kws + (keywords - current_kws)).join(" ")
  set_attribute(attribute_name, new_kws)
  self
end

def kwattr_append(attribute_name, keywords)

Since v1.11.0

node.kwattr_append("rel", ["nofollow", "noreferrer"]) # =>
node # =>

*Example:* Append "nofollow" and "noreferrer" to the +rel+ attribute, via an Array argument.


node.kwattr_append("rel", "nofollow noreferrer") # =>
node # =>

Note that "nofollow" is appended even though it is already present.

*Example:* Append "nofollow" and "noreferrer" to the +rel+ attribute, via a String argument.

node.kwattr_append("rel", "nofollow") # =>
node.kwattr_append("rel", "nofollow") # =>
node # =>

Note that duplicates are added.

*Example:* Append "nofollow" to the +rel+ attribute.

[Returns] +self+ (Node) for ease of chaining method calls.

named attribute does not exist, it is created.
appended to the named attribute even if they are already present in the attribute. If the
whitespace-delimited values, or an Array of String values. All values passed in will be
Keywords to be added to the attribute named +attribute_name+. May be a string containing
- +keywords+ (String, Array)
- +attribute_name+ (String) The name of the keyword attribute to be modified.
[Parameters]

See also #append_class, #kwattr_values, #kwattr_add, #kwattr_remove

{the "rel" attribute}[https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel].
contain CSS classes. But other keyword attributes exist, for instance
values. Perhaps the most familiar example of this is the HTML "class" attribute used to
A "keyword attribute" is a node attribute that contains a set of space-delimited

#kwattr_add.
Add keywords to a Node's keyword attribute, regardless of duplication. Compare with

kwattr_append(attribute_name, keywords) → self
:call-seq:
def kwattr_append(attribute_name, keywords)
  keywords = keywordify(keywords)
  current_kws = kwattr_values(attribute_name)
  new_kws = (current_kws + keywords).join(" ")
  set_attribute(attribute_name, new_kws)
  self
end

def kwattr_remove(attribute_name, keywords)

Since v1.11.0

node.kwattr_remove("rel", "noreferrer") # => link
node.kwattr_remove("rel", "nofollow") # => link
node # => link

Note that the +rel+ attribute is deleted when empty.

*Example:*

[Returns] +self+ (Node) for ease of chaining method calls.

the attribute is deleted.
in the named attribute will be removed. If no keywords remain, or if +keywords+ is nil,
containing whitespace-delimited values, or an Array of String values. Any keywords present
Keywords to be removed from the attribute named +attribute_name+. May be a string
- +keywords+ (String, Array)
- +attribute_name+ (String) The name of the keyword attribute to be modified.
[Parameters]

See also #remove_class, #kwattr_values, #kwattr_add, #kwattr_append

{the "rel" attribute}[https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel].
contain CSS classes. But other keyword attributes exist, for instance
values. Perhaps the most familiar example of this is the HTML "class" attribute used to
A "keyword attribute" is a node attribute that contains a set of space-delimited

deleted from the node.
If no keywords remain after this operation, or if +keywords+ is +nil+, the attribute is

attribute are removed, including any multiple entries.
Remove keywords from a keyword attribute. Any matching keywords that exist in the named

kwattr_remove(attribute_name, keywords) → self
:call-seq:
def kwattr_remove(attribute_name, keywords)
  if keywords.nil?
    remove_attribute(attribute_name)
    return self
  end
  keywords = keywordify(keywords)
  current_kws = kwattr_values(attribute_name)
  new_kws = current_kws - keywords
  if new_kws.empty?
    remove_attribute(attribute_name)
  else
    set_attribute(attribute_name, new_kws.join(" "))
  end
  self
end

def kwattr_values(attribute_name)

Since v1.11.0

node.kwattr_values("rel") # => ["nofollow", "noopener", "external"]
node # => link

*Example:*

attribute is empty or non-existent, the return value is an empty array.
(Array) The values present in the Node's +attribute_name+ attribute. If the
[Returns]

- +attribute_name+ (String) The name of the keyword attribute to be inspected.
[Parameters]

See also #classes, #kwattr_add, #kwattr_append, #kwattr_remove

{the "rel" attribute}[https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel].
contain CSS classes. But other keyword attributes exist, for instance
values. Perhaps the most familiar example of this is the HTML "class" attribute used to
A "keyword attribute" is a node attribute that contains a set of space-delimited

Fetch values from a keyword attribute of a Node.

kwattr_values(attribute_name) → Array
:call-seq:
def kwattr_values(attribute_name)
  keywordify(get_attribute(attribute_name) || [])
end

def matches?(selector)

Returns true if this Node matches +selector+
##
def matches?(selector)
  ancestors.last.search(selector).include?(self)
end

def namespace=(ns)

#add_namespace_definition with a nil prefix argument.
for this node. You probably want #default_namespace= instead, or perhaps
a Namespace added this way will NOT be serialized as an xmlns attribute
"xmlns=" attribute in XML source), as a Namespace object +ns+. Note that
Set the default namespace on this node (as would be defined with an
##
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

def namespaces


# "xmlns:in_scope"=>"http://example.com/in_scope"}
# "xmlns"=>"http://example.com/root",
# => {"xmlns:foo"=>"http://example.com/foo",
doc.at_xpath("//root:third", "root" => "http://example.com/root").namespaces
# "xmlns:in_scope"=>"http://example.com/in_scope"}
# => {"xmlns"=>"http://example.com/child",
doc.at_xpath("//child:second", "child" => "http://example.com/child").namespaces
# "xmlns:in_scope"=>"http://example.com/in_scope"}
# => {"xmlns"=>"http://example.com/root",
doc.at_xpath("//root:first", "root" => "http://example.com/root").namespaces
EOF





doc = Nokogiri::XML(<<~EOF)

*Example:*

namespace prefix, and the hash value for each key is the namespace URI.
Hash containing all the namespaces on this node and its ancestors. The hash keys are the
[Returns]

See also #namespace_scopes

The default namespace for this node will be included with key "xmlns".

such as "xmlns:prefix", not just the prefix.
Note that the keys in this hash XML attributes that would be used to define this namespace,

Fetch all the namespaces on this node and its ancestors.

namespaces() → Hash
:call-seq:
def namespaces
  namespace_scopes.each_with_object({}) do |ns, hash|
    prefix = ns.prefix
    key = prefix ? "xmlns:#{prefix}" : "xmlns"
    hash[key] = ns.href
  end
end

def parent=(parent_node)

Set the parent Node for this Node
##
def parent=(parent_node)
  parent_node.add_child(self)
end

def parse(string_or_io, options = nil)

+string_or_io+.
*this* node. Returns a XML::NodeSet containing the nodes parsed from
Parse +string_or_io+ as a document fragment within the context of
##
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)
  options = Nokogiri::XML::ParseOptions.new(options) if Integer === options
  yield options if block_given?
  contents = if string_or_io.respond_to?(:read)
    string_or_io.read
  else
    string_or_io
  end
  return Nokogiri::XML::NodeSet.new(document) if contents.empty?
  error_count = document.errors.length
  node_set = in_context(contents, options.to_i)
  if document.errors.length > error_count
    raise document.errors[error_count] unless options.recover?
    if node_set.empty?
      # libxml2 < 2.13 does not obey the +recover+ option after encountering errors during
      # +in_context+ parsing, and so this horrible hack is here to try to emulate recovery
      # behavior.
      #
      # (Note that HTML4 fragment parsing seems to have been fixed in abd74186, and XML
      # fragment parsing is fixed in 1c106edf. Both are in 2.13.)
      #
      # Unfortunately, this means we're no longer parsing "in context" and so namespaces that
      # would have been inherited from the context node won't be handled correctly. This hack
      # was written in 2010, and I regret it, because it's silently degrading functionality in
      # a way that's not easily prevented (or even detected).
      #
      # I think preferable behavior would be to either:
      #
      # a. add an error noting that we "fell back" and pointing the user to turning off the
      #    +recover+ option
      # b. don't recover, but raise a sensible exception
      #
      # For context and background:
      # - https://github.com/sparklemotion/nokogiri/issues/313
      # - https://github.com/sparklemotion/nokogiri/issues/2092
      fragment = document.related_class("DocumentFragment").parse(contents)
      node_set = fragment.children
    end
  end
  node_set
end

def prepend_child(node_or_tags)

Also see related method +add_child+.

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

containing markup.
+node_or_tags+ can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a String

Add +node_or_tags+ as the first child of this Node.
##
def prepend_child(node_or_tags)
  if (first = children.first)
    # Mimic the error add_child would raise.
    raise "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

def processing_instruction?

Returns true if this is a ProcessingInstruction node
def processing_instruction?
  type == PI_NODE
end

def read_only?

Is this a read only node?
##
def read_only?
  # According to gdome2, these are read-only node types
  [NOTATION_NODE, ENTITY_NODE, ENTITY_DECL].include?(type)
end

def remove_attribute(name)

Remove the attribute named +name+
##
def remove_attribute(name)
  attr = attributes[name].remove if key?(name)
  clear_xpath_context if Nokogiri.jruby?
  attr
end

def remove_class(names = nil)


node.remove_class(["section", "float"]) # =>

node # =>


Note that the "class" attribute is deleted once it's empty.

*Example*: Deleting multiple CSS classes

node.remove_class("section") # =>

node # =>


Note that the attribute is removed once there are no remaining classes.

*Example*: Deleting the only remaining CSS class

node.remove_class("section") # =>

node # =>


Note that all instances of the class "section" are removed from the "class" attribute.

*Example*: Deleting a CSS class

[Returns] +self+ (Nokogiri::XML::Node) for ease of chaining method calls.

the "class" attribute is deleted.
String names. Any class names already present will be removed. If no CSS classes remain,
"class" attribute. May be a string containing whitespace-delimited names, or an Array of
CSS class names to be removed from the Node's

- +css_classes+ (String, Array)
[Parameters]

Also see #kwattr_remove, #classes, #add_class, #append_class

node.kwattr_remove("class", css_classes)

This is a convenience function and is equivalent to:

attribute is deleted from the node.
If no CSS classes remain after this operation, or if +css_classes+ is +nil+, the "class"

this node's "class" attribute are removed, including any multiple entries.
Remove HTML CSS classes from this node. Any CSS class names in +css_classes+ that exist in

remove_class(css_classes) → self
:call-seq:
def remove_class(names = nil)
  kwattr_remove("class", names)
end

def replace(node_or_tags)

Also see related method +swap+.

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

containing markup.
+node_or_tags+ can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a String

Replace this Node with +node_or_tags+.
###
def replace(node_or_tags)
  raise("Cannot replace a node with no parent") unless parent
  # 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 = parent.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

def serialize(*args, &block)


end
config.format.as_xml
node.serialize(encoding: 'UTF-8') do |config|

or

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

These two statements are equivalent:

See also Nokogiri::XML::Node::SaveOptions and Node@Serialization+and+Generating+Output.

Serialize Node using +options+. Save options can also be set using a block.
##
def serialize(*args, &block)
  # TODO: deprecate non-hash options, see 46c68ed 2009-06-20 for context
  options = if args.first.is_a?(Hash)
    args.shift
  else
    {
      encoding: args[0],
      save_with: args[1],
    }
  end
  options[:encoding] ||= document.encoding
  encoding = Encoding.find(options[:encoding] || "UTF-8")
  io = StringIO.new(String.new(encoding: encoding))
  write_to(io, options, &block)
  io.string
end

def swap(node_or_tags)

Also see related method +replace+.

Returns self, to support chaining of calls.

Containing markup.
+node_or_tags+ can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a String

Swap this Node for +node_or_tags+
###
def swap(node_or_tags)
  replace(node_or_tags)
  self
end

def text?

Returns true if this is a Text node
def text?
  type == TEXT_NODE
end

def to_format(save_option, options)

def to_format(save_option, options)
  return dump_html if USING_LIBXML_WITH_BROKEN_SERIALIZATION
  options[:save_with] = save_option unless options[:save_with]
  serialize(options)
end

def to_html(options = {})

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

doc.to_html

Serialize this Node to HTML
##
def to_html(options = {})
  to_format(SaveOptions::DEFAULT_HTML, options)
end

def to_s

returns html. If the document is XML, this method returns XML.
Turn this node in to a string. If the document is HTML, this method
##
def to_s
  document.xml? ? to_xml : to_html
end

def to_xhtml(options = {})

See Node#write_to for a list of +options+

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

Serialize this Node to XHTML using +options+
##
def to_xhtml(options = {})
  to_format(SaveOptions::DEFAULT_XHTML, options)
end

def to_xml(options = {})

See Node#write_to for a list of +options+

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

Serialize this Node to XML using +options+
##
def to_xml(options = {})
  options[:save_with] ||= SaveOptions::DEFAULT_XML
  serialize(options)
end

def traverse(&block)

Yields self and all children to +block+ recursively.
###
def traverse(&block)
  children.each { |j| j.traverse(&block) }
  yield(self)
end

def value?(value)

Does this Node's attributes include
##
def value?(value)
  values.include?(value)
end

def values

Get the attribute values for this Node.
##
def values
  attribute_nodes.map(&:value)
end

def wrap(node_or_tags)


#
#
#
doc.to_html
doc.at_css("a").wrap(doc.create_element("div"))
HTML

asdf

doc = Nokogiri::HTML5(<<~HTML)

*Example* with a +Node+ argument:

#
#
# =>
doc.to_html
doc.at_css("a").wrap("
")
HTML

asdf

doc = Nokogiri::HTML5(<<~HTML)

*Example* with a +String+ argument:

Also see NodeSet#wrap

[Returns] +self+, to support chaining.

An element that is `#dup`ed and used as the wrapper.
- *node* (Nokogiri::XML::Node)
fragment has multiple roots, the first root node is used as the wrapper.
as the context node for parsing; otherwise the associated document is used. If the parsed
Markup that is parsed and used as the wrapper. This node's parent, if it exists, is used
- *markup* (String)
[Parameters]

Wrap this Node with the node parsed from +markup+ or a dup of the +node+.

wrap(node) -> self
wrap(markup) -> self
:call-seq:
def wrap(node_or_tags)
  case node_or_tags
  when String
    context_node = parent || document
    new_parent = context_node.coerce(node_or_tags).first
    if new_parent.nil?
      raise "Failed to parse '#{node_or_tags}' in the context of a '#{context_node.name}' element"
    end
  when XML::Node
    new_parent = node_or_tags.dup
  else
    raise ArgumentError, "Requires a String or Node argument, and cannot accept a #{node_or_tags.class}"
  end
  if parent
    add_next_sibling(new_parent)
  else
    new_parent.unlink
  end
  new_parent.add_child(self)
  self
end

def write_format_to(save_option, io, options)

def write_format_to(save_option, io, options)
  return (io << dump_html) if USING_LIBXML_WITH_BROKEN_SERIALIZATION
  options[:save_with] ||= save_option
  write_to(io, options)
end

def write_html_to(io, options = {})

See Node#write_to for a list of +options+

Write Node as HTML to +io+ with +options+
##
def write_html_to(io, options = {})
  write_format_to(SaveOptions::DEFAULT_HTML, io, options)
end

def write_to(io, *options)


node.write_to(io, indent_text: '-', indent: 2)

To save indented with two dashes:

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

To save with UTF-8 indented twice:

* +:save_with+ (Integer) a combination of SaveOptions constants
* +:indent+ (Integer) the number of +:indent_text+ to use (defaults to +2+)
* +:indent_text+ (String) the indentation text (defaults to " ")
* +:encoding+ (String or Encoding) specify the encoding of the output (defaults to document encoding)
[Options]

- +options+ (Hash) See below
- +io+ (IO) An IO-like object to which the serialized content will be written.
[Parameters]

Serialize this node or document to +io+.

write_to(io, *options)
:call-seq:
##
def write_to(io, *options)
  options = options.first.is_a?(Hash) ? options.shift : {}
  encoding = options[:encoding] || options[0] || document.encoding
  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] || " "
  # Any string times 0 returns an empty string. Therefore, use the same
  # string instead of generating a new empty string for every node with
  # zero indentation.
  indentation = indent_times.zero? ? "" : (indent_text * indent_times)
  config = SaveOptions.new(save_options.to_i)
  yield config if block_given?
  encoding = encoding.is_a?(Encoding) ? encoding.name : encoding
  native_write_to(io, encoding, indentation, config.options)
end

def write_xhtml_to(io, options = {})

See Node#write_to for a list of +options+

Write Node as XHTML to +io+ with +options+
##
def write_xhtml_to(io, options = {})
  write_format_to(SaveOptions::DEFAULT_XHTML, io, options)
end

def write_xml_to(io, options = {})

See Node#write_to for a list of options

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

Write Node as XML to +io+ with +options+
##
def write_xml_to(io, options = {})
  options[:save_with] ||= SaveOptions::DEFAULT_XML
  write_to(io, options)
end

def xml?

Returns true if this is an XML::Document node
def xml?
  type == DOCUMENT_NODE
end