Class | Nokogiri::XML::Node
In: | lib/nokogiri/xml/node.rb lib/nokogiri/xml/node/save_options.rb ext/nokogiri/xml_sax_push_parser.c
Parent: | Object

call-seq:

     new(document, content)

Create a new EntityReference element on the `document` with `name`

### Methods

% / << <=> == > [] []= accept add_child add_namespace_definition add_next_sibling add_previous_sibling after ancestors at at_css at_xpath attribute attribute_nodes attribute_with_ns attributes before blank? cdata? child children children= comment? compare content content= create_external_subset create_internal_subset css css_path decorate! default_namespace= delete description document dump_html dup each elem? element? element_children encode_special_chars external_subset first_element_child fragment fragment? get get_attribute html? in_context inner_html inner_html= internal_subset key? keys last_element_child line matches? namespace namespace= namespace_definitions namespace_scopes namespaced_key? namespaces native_content= native_write_to new next_element next_sibling node_name node_name= node_type parent parent= parse path pointer_id previous= previous_element previous_sibling read_only? remove_attribute replace search serialize set_namespace swap text? to_html to_s to_xhtml to_xml traverse unlink values write_html_to write_to write_xhtml_to write_xml_to xml? xpath

### Included Modules

Nokogiri::XML::PP::Node Enumerable

### Classes and Modules

Class Nokogiri::XML::Node::SaveOptions

### Constants

ELEMENT_NODE | = | 1 |  | Element node type, see Nokogiri::XML::Node#element?
ATTRIBUTE_NODE | = | 2 |  | Attribute node type
TEXT_NODE | = | 3 |  | Text node type, see Nokogiri::XML::Node#text?
CDATA_SECTION_NODE | = | 4 |  | CDATA node type, see Nokogiri::XML::Node#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 Nokogiri::XML::Node#comment?
DOCUMENT_NODE | = | 9 |  | Document node type, see Nokogiri::XML::Node#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 Nokogiri::XML::Node#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

### External Aliases

next_sibling | -> | next
previous_sibling | -> | previous
unlink | -> | remove
attr | -> | :
[]= | -> | set_attribute
text | -> | :
content | -> | inner_text
key? | -> | has_attribute?
node_name | -> | name
node_name= | -> | name=
node_type | -> | type
text | -> | to_str
dup | -> | clone
element_children | -> | elements
add_namespace_definition | -> | add_namespace

### Public Class methods

new(name, document)

Create a new node with `name` sharing GC lifecycle with `document`

### Public Instance methods

%(path, ns = document.root ? document.root.namespaces : {})

Alias for at

/(*paths)

Alias for search

<<(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`.

<=>(other)

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

==(other)

Test to see if this Node is equal to `other`

>(selector)

Search this node‘s immediate children using CSS selector `selector`

[](name)

Get the attribute value for the attribute `name`

[]=(property, value)

Set the `property` to `value`

accept(visitor)

Accept a visitor. This method calls "visit" on `visitor` with self.

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 +<<+.

add_namespace_definition(prefix, href)

Adds a namespace definition with `prefix` using `href` value. The result is as if parsed XML for this node had included an attribute ‘xmlns:prefix=value’. A default namespace for this node ("xmlns=") can be added by passing ‘nil’ for prefix. Namespaces added this way will not show up in attributes, but they will be included as an xmlns attribute when the node is serialized to XML.

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`.

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`.

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`.

ancestors(selector = nil)

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

at(path, ns = document.root ? document.root.namespaces : {})

Search for the first occurrence of `path`.

Returns nil if nothing is found, otherwise a Node.

at_css(*rules)

Search this node for the first occurrence of CSS `rules`. Equivalent to `css(rules).first` See Node#css for more information.

at_xpath(*paths)

Search this node for the first occurrence of XPath `paths`. Equivalent to `xpath(paths).first` See Node#xpath for more information.

attribute(name)

Get the attribute node with `name`

attribute_nodes()

returns a list containing the Node attributes.

attribute_with_ns(name, namespace)

Get the attribute node with `name` and `namespace`

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.

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`.

blank?

Is this node blank?

cdata?()

Returns true if this is a CDATA

child

Returns the child node

children

Get the list of children for this node as a NodeSet

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=+

comment?()

Returns true if this is a Comment

compare(other)

Compare this Node to `other` with respect to their Document

content

Returns the content for this Node

content=(string)

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

create_external_subset(name, external_id, system_id)

Create an external subset

create_internal_subset(name, external_id, system_id)

Create the internal subset of a document.

      doc.create_internal_subset("chapter", "-//OASIS//DTD DocBook XML//EN", "chapter.dtd")
      # => <!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML//EN" "chapter.dtd">

      doc.create_internal_subset("chapter", nil, "chapter.dtd")
      # => <!DOCTYPE chapter SYSTEM "chapter.dtd">

css *rules, [namespace-bindings, custom-pseudo-class]

Search this node for CSS `rules`. `rules` must be one or more CSS selectors. For example:

      node.css('title')
      node.css('body h1.bold')
      node.css('div + p.green', 'div#one')

A hash of namespace bindings may be appended. For example:

      node.css('bike|tire', {'bike' => 'http://schwinn.com/'})

Custom CSS pseudo classes may also be defined. To define custom pseudo classes, create a class and implement the custom pseudo class you want defined. The first argument to the method will be the current matching NodeSet. Any other arguments are ones that you pass in. For example:

      node.css('title:regex("\w+")', Class.new {
        def regex node_set, regex
          node_set.find_all { |node| node['some_attribute'] =~ /#{regex}/ }
        end
      }.new)

Note that the CSS query string is case-sensitive with regards to your document type. That is, if you‘re looking for "H1" in an HTML document, you‘ll never find anything, since HTML tags will match only lowercase CSS queries. However, "H1" might be found in an XML document, where tags names are case-sensitive (e.g., "H1" is distinct from "h1").

css_path()

Get the path to this node as a CSS expression

decorate!()

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

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=

delete(name)

Alias for remove_attribute

description()

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

document

Get the document for this Node

dump_html

Returns the Node as html.

dup

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

each(&block)

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

elem?()

Alias for element?

element?()

Returns true if this is an Element node

element_children

Get the list of children for this node as a NodeSet. All nodes will be element nodes.

Example:

      @doc.root.element_children.all? { |x| x.element? } # => true

encode_special_chars(string)

Encode any special characters in `string`

external_subset

Get the external subset

first_element_child

Returns the first child node of this node that is an element.

Example:

      @doc.root.first_element_child.element? # => true

fragment(tags)

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

fragment?()

Returns true if this is a DocumentFragment

get(attribute)

Get the value for `attribute`

get_attribute(name)

Alias for #[]

html?()

Returns true if this is an HTML::Document node

in_context(p1, p2)

TODO: DOCUMENT ME

inner_html(*args)

Get the inner_html for this node‘s Node#children

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=+

internal_subset

Get the internal subset

key?(attribute)

Returns true if `attribute` is set

keys()

Get the attribute names for this Node.

last_element_child

Returns the last child node of this node that is an element.

Example:

      @doc.root.last_element_child.element? # => true

line

Returns the line for this Node

matches?(selector)

Returns true if this Node matches `selector`

namespace()

returns the default namespace set on this node (as with an "xmlns=" attribute), as a Namespace object.

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.

namespace_definitions()

returns namespaces defined on self element directly, as an array of Namespace objects. Includes both a default namespace (as in"xmlns="), and prefixed namespaces (as in "xmlns:prefix=").

namespace_scopes()

returns namespaces in scope for self — those defined on self element directly or any ancestor node — as an array of Namespace objects. Default namespaces ("xmlns=" style) for self are included in this array; Default namespaces for ancestors, however, are not. See also namespaces

namespaced_key?(attribute, namespace)

Returns true if `attribute` is set with `namespace`

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.

content=

Set the content for this Node

native_write_to(io, encoding, options)

Write this Node to `io` with `encoding` and `options`

next_element

Returns the next Nokogiri::XML::Element type sibling node.

next_sibling

Returns the next sibling node

name

Returns the name for this Node

name=(new_name)

Set the name for this Node

node_type

Get the type for this Node

parent

Get the parent Node for this Node

parent=(parent_node)

Set the parent Node for this Node

parse(string_or_io, options = nil) {|options if block_given?| ...}

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`.

path

Returns the path associated with this Node

pointer_id

Get the internal pointer number

previous=(node_or_tags)

Alias for add_previous_sibling

previous_element

Returns the previous Nokogiri::XML::Element type sibling node.

previous_sibling

Returns the previous sibling node

read_only?()

Is this a read only node?

remove_attribute(name)

Remove the attribute named `name`

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`.

search(*paths)

Search this node for `paths`. `paths` can be XPath or CSS, and an optional hash of namespaces may be appended. See Node#xpath and Node#css.

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

set_namespace(namespace)

Set the namespace to `namespace`

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`.

text?()

Returns true if this is a Text node

to_html(options = {})

Serialize this Node to HTML

      doc.to_html

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

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.

to_xhtml(options = {})

Serialize this Node to XHTML using `options`

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

See Node#write_to for a list of `options`

to_xml(options = {})

Serialize this Node to XML using `options`

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

See Node#write_to for a list of `options`

traverse(&block)

Yields self and all children to `block` recursively.

unlink

Unlink this node from its current context.

values()

Get the attribute values for this Node.

write_html_to(io, options = {})

Write Node as HTML to `io` with `options`

See Node#write_to for a list of `options`

write_to(io, *options) {|config if block_given?| ...}

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

write_xhtml_to(io, options = {})

Write Node as XHTML to `io` with `options`

See Node#write_to for a list of `options`

write_xml_to(io, options = {})

Write Node as XML to `io` with `options`

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

See Node#write_to for a list of options

xml?()

Returns true if this is an XML::Document node

xpath *paths, [namespace-bindings, variable-bindings, custom-handler-class]

Search this node for XPath `paths`. `paths` must be one or more XPath queries.

      node.xpath('.//title')

A hash of namespace bindings may be appended. For example:

      node.xpath('.//foo:name', {'foo' => 'http://example.org/'})
      node.xpath('.//xmlns:name', node.root.namespaces)

A hash of variable bindings may also be appended to the namespace bindings. For example:

      node.xpath('.//address[@domestic=$value]', nil, {:value => 'Yes'})

Custom XPath functions may also be defined. To define custom functions create a class and implement the function you want to define. The first argument to the method will be the current matching NodeSet. Any other arguments are ones that you pass in. Note that this class may appear anywhere in the argument list. For example:

      node.xpath('.//title[regex(., "\w+")]', Class.new {
        def regex node_set, regex
          node_set.find_all { |node| node['some_attribute'] =~ /#{regex}/ }
        end
      }.new)

[Validate]