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

call-seq:

 new(document, name, content)

Create a new ProcessingInstruction element on the document with name and content

Methods

Classes and Modules

Class Nokogiri::XML::Node::SaveOptions

Constants

ELEMENT_NODE = 1
ATTRIBUTE_NODE = 2
TEXT_NODE = 3
CDATA_SECTION_NODE = 4
ENTITY_REF_NODE = 5
ENTITY_NODE = 6
PI_NODE = 7
COMMENT_NODE = 8
DOCUMENT_NODE = 9
DOCUMENT_TYPE_NODE = 10
DOCUMENT_FRAG_NODE = 11
NOTATION_NODE = 12
HTML_DOCUMENT_NODE = 13
DTD_NODE = 14
ELEMENT_DECL = 15
ATTRIBUTE_DECL = 16
ENTITY_DECL = 17
NAMESPACE_DECL = 18
XINCLUDE_START = 19
XINCLUDE_END = 20
DOCB_DOCUMENT_NODE = 21

External Aliases

next_sibling -> next
previous_sibling -> previous
unlink -> remove
set_attribute -> :
content -> text
content -> inner_text
key? -> has_attribute?
add_child -> <<
node_name -> name
node_name= -> name=
node_type -> type
text -> to_str

Attributes

document  [RW]  The Document associated with this Node.

Public Class methods

Create a new node with name

Public Instance methods

/(*paths)

Alias for search

Test to see if this Node is equal to other

Get the attribute value for the attribute name

Set the property to value

Add node as a child of this node. Returns the new child node.

Add a namespace with prefix using href

Insert node after this node (as a sibling).

Insert node before this node (as a sibling).

Create nodes from data and insert them after this node (as a sibling).

Get a list of ancestor Node for this Node

Search for the first occurrence of path. Returns nil if nothing is found, otherwise a Node.

Get the attribute node with name

returns a list containing the Node attributes.

Returns a hash containing the node‘s attributes. The key is the attribute name, the value is the string value of the attribute.

Create nodes from data and insert them before this node (as a sibling).

Is this node blank?

Returns the child node

Get the list of children for this node as a NodeSet

recursively get all namespaces from this node and its subtree

Returns the content for this Node

Set the content to string. If encode, encode any special characters first.

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')

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
  })

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

delete(name)

Alias for remove_attribute

Returns the Node as html.

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.

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

elem?()

Alias for element?

Encode any special characters in string

Get the value for attribute

get_attribute(name)

Alias for #[]

Get the internal subset

Returns true if attribute is set

Get the attribute names for this Node.

Returns the line for this Node

returns the namespace prefix for the node, if one exists.

returns a hash containing the node‘s namespaces.

Set the content for this Node

Write this Node to io with encoding and options

Returns the next sibling node

Returns the name for this Node

Set the name for this Node

Get the type for this Node

Get the parent Node for this Node

Returns the path associated with this Node

Get the internal pointer number

Returns the previous sibling node

Remove the attribute named name

replace node with the new node in the document.

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 Node using encoding and save_options. Save options can also be set using a block. See SaveOptions.

These two statements are equivalent:

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

or

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

Serialize this Node to HTML using encoding

Serialize this Node to XML using encoding

Serialize this Node to XML using encoding

Yields self and all children to block recursively.

Unlink this node from its current context.

Get the attribute values for this Node.

Write Node as HTML to io with encoding

Write Node as XHTML to io with encoding

Write Node as XML to io with encoding

Search this node for XPath paths. paths must be one or more XPath queries. A hash of namespaces may be appended. For example:

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

Custom XPath functions may also be defined. To define custom functions create a class and implement the # function you want to define. 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]