Class | Nokogiri::XML::Node |
In: |
lib/nokogiri/ffi/xml/node.rb
lib/nokogiri/xml/node.rb lib/nokogiri/xml/node/save_options.rb |
Parent: | Object |
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 Nokogiri::XML::Node#[] and Nokogiri::XML#[]= for more information.
Nokogiri::XML::Node also has methods that let you move around your tree. For navigating your tree, see:
You may search this node’s subtree using Node#xpath and Node#css
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 |
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 |
add_namespace_definition | -> | add_namespace |
Add node as a child of this Node. The new node must be a Nokogiri::XML::Node or a non-empty String. Returns the new child node.
Search for the first occurrence of path. Returns nil if nothing is found, otherwise a Node.
Search this node for the first occurrence of XPath paths. Equivalent to xpath(paths).first See Node#xpath for more information.
Returns a hash containing the node’s attributes. The key is the attribute name, the value is a Nokogiri::XML::Attr representing the attribute.
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 }.new)
Fetch the Nokogiri::HTML::ElementDescription for this node. Returns nil on XML documents and on unknown tags.
replace this Node with the node in the Document. The new node must be a Nokogiri::XML::Node or a non-empty String. Returns the new child node.
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 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
doc.to_html
See Node#write_to for a list of options. For formatted output, use Node#to_xhtml instead.
Serialize this Node to XHTML using options
doc.to_xhtml(:indent => 5, :encoding => 'UTF-8')
See Node#write_to for a list of 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
Write Node as HTML to io with options
See Node#write_to for a list of options
Write Node to io with options. options modify the output of this method. Valid options are:
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 Node as XHTML to io with options
See Node#write_to for a list of 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
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)