Class: Nokogiri::XML::Node
- Inherits:
-
Object
- Object
- Nokogiri::XML::Node
- Includes:
- Enumerable, ClassResolver, HTML5::Node, PP::Node, Searchable
- Defined in:
- lib/nokogiri/xml/node.rb,
lib/nokogiri/xml/node/save_options.rb,
ext/nokogiri/xml_node.c
Overview
Nokogiri::XML::Node is the primary API you’ll use to interact with your Document.
Attributes
A Nokogiri::XML::Node may be treated similarly to a hash with regard to attributes. For example:
node = Nokogiri::XML::DocumentFragment.parse("<a href='#foo' id='link'>link</a>").at_css("a")
node.to_html # => "<a href=\"#foo\" id=\"link\">link</a>"
node['href'] # => "#foo"
node.keys # => ["href", "id"]
node.values # => ["#foo", "link"]
node['class'] = 'green' # => "green"
node.to_html # => "<a href=\"#foo\" id=\"link\" class=\"green\">link</a>"
See the method group entitled Node@Working+With+Node+Attributes for the full set of methods.
Navigation
Nokogiri::XML::Node also has methods that let you move around your tree:
- #parent, #children, #next, #previous
-
Navigate up, down, or through siblings.
See the method group entitled Node@Traversing+Document+Structure for the full set of methods.
Serialization
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
-
These methods will all **emit plaintext**, meaning that entities will be replaced (e.g., < will be replaced with <), meaning that any sanitizing will likely be un-done in the output.
- #to_s, #to_xml, #to_html, #inner_html
-
These methods will all **emit properly-escaped markup**, meaning that it’s suitable for consumption by browsers, parsers, etc.
See the method group entitled Node@Serialization+and+Generating+Output for the full set of methods.
Searching
You may search this node’s subtree using methods like #xpath and #css.
See the method group entitled Node@Searching+via+XPath+or+CSS+Queries for the full set of methods.
Direct Known Subclasses
AttributeDecl, DTD, Document, DocumentFragment, ElementDecl, EntityDecl, EntityReference, ProcessingInstruction
Defined Under Namespace
Classes: SaveOptions
Constant Summary collapse
- ELEMENT_NODE =
Element node type, see Nokogiri::XML::Node#element?
1
- ATTRIBUTE_NODE =
Attribute node type
2
- TEXT_NODE =
Text node type, see Nokogiri::XML::Node#text?
3
- CDATA_SECTION_NODE =
CDATA node type, see Nokogiri::XML::Node#cdata?
4
- ENTITY_REF_NODE =
Entity reference node type
5
- ENTITY_NODE =
Entity node type
6
- PI_NODE =
PI node type
7
- COMMENT_NODE =
Comment node type, see Nokogiri::XML::Node#comment?
8
- DOCUMENT_NODE =
Document node type, see Nokogiri::XML::Node#xml?
9
- DOCUMENT_TYPE_NODE =
Document type node type
10
- DOCUMENT_FRAG_NODE =
Document fragment node type
11
- NOTATION_NODE =
Notation node type
12
- HTML_DOCUMENT_NODE =
HTML document node type, see Nokogiri::XML::Node#html?
13
- DTD_NODE =
DTD node type
14
- ELEMENT_DECL =
Element declaration type
15
- ATTRIBUTE_DECL =
Attribute declaration type
16
- ENTITY_DECL =
Entity declaration type
17
- NAMESPACE_DECL =
Namespace declaration type
18
- XINCLUDE_START =
XInclude start type
19
- XINCLUDE_END =
XInclude end type
20
- DOCB_DOCUMENT_NODE =
DOCB document node type
21
Constants included from ClassResolver
ClassResolver::VALID_NAMESPACES
Constants included from Searchable
Class Method Summary collapse
-
.new(*args) ⇒ Object
:nodoc: documented in lib/nokogiri/xml/node.rb.
Instance Method Summary collapse
-
#<<(node_or_tags) ⇒ Object
Add
node_or_tags
as a child of this Node. -
#<=>(other) ⇒ Object
Compare two Node objects with respect to their Document.
-
#==(other) ⇒ Object
Test to see if this Node is equal to
other
. -
#[](name) ⇒ Object
(also: #get_attribute, #attr)
:call-seq: [](name) → (String, nil).
-
#[]=(name, value) ⇒ Object
(also: #set_attribute)
:call-seq: []=(name, value) → value.
-
#accept(visitor) ⇒ Object
Accept a visitor.
-
#add_child(node_or_tags) ⇒ Object
Add
node_or_tags
as a child of this Node. -
#add_class(names) ⇒ Object
:call-seq: add_class(names) → self.
-
#add_namespace_definition(rb_prefix, rb_href) ⇒ Object
(also: #add_namespace)
:call-seq: add_namespace_definition(prefix, href) → Nokogiri::XML::Namespace add_namespace(prefix, href) → Nokogiri::XML::Namespace.
-
#add_next_sibling(node_or_tags) ⇒ Object
(also: #next=)
Insert
node_or_tags
after this Node (as a sibling). -
#add_previous_sibling(node_or_tags) ⇒ Object
(also: #previous=)
Insert
node_or_tags
before this Node (as a sibling). -
#after(node_or_tags) ⇒ Object
Insert
node_or_tags
after this node (as a sibling). -
#ancestors(selector = nil) ⇒ Object
Get a list of ancestor Node for this Node.
-
#append_class(names) ⇒ Object
:call-seq: append_class(names) → self.
-
#attribute(name) ⇒ Object
:call-seq: attribute(name) → Nokogiri::XML::Attr.
-
#attribute_nodes ⇒ Object
:call-seq: attribute_nodes() → Array<Nokogiri::XML::Attr>.
-
#attribute_with_ns(name, namespace) ⇒ Object
:call-seq: attribute_with_ns(name, namespace) → Nokogiri::XML::Attr.
-
#attributes ⇒ Object
:call-seq: attributes() → Hash<String ⇒ Nokogiri::XML::Attr>.
-
#before(node_or_tags) ⇒ Object
Insert
node_or_tags
before this node (as a sibling). -
#blank? ⇒ Boolean
- Returns
-
true
if the node is an empty or whitespace-only text or cdata node, elsefalse
.
- #canonicalize(mode = XML::XML_C14N_1_0, inclusive_namespaces = nil, with_comments = false) ⇒ Object
-
#cdata? ⇒ Boolean
Returns true if this is a CDATA.
-
#child ⇒ Object
:call-seq: child() → Nokogiri::XML::Node.
-
#children ⇒ Object
:call-seq: children() → Nokogiri::XML::NodeSet.
-
#children=(node_or_tags) ⇒ Object
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. -
#classes ⇒ Object
:call-seq: classes() → Array<String>.
-
#comment? ⇒ Boolean
Returns true if this is a Comment.
-
#content ⇒ Object
(also: #inner_text, #text, #to_str)
:call-seq: content() → String inner_text() → String text() → String to_str() → String.
-
#content=(string) ⇒ Object
Set the Node’s content to a Text node containing
string
. -
#create_external_subset(name, external_id, system_id) ⇒ Object
:call-seq: create_external_subset(name, external_id, system_id).
-
#create_internal_subset(name, external_id, system_id) ⇒ Object
:call-seq: create_internal_subset(name, external_id, system_id).
-
#css_path ⇒ Object
Get the path to this node as a CSS expression.
-
#decorate! ⇒ Object
Decorate this node with the decorators set up in this node’s Document.
-
#default_namespace=(url) ⇒ Object
Adds a default namespace supplied as a string
url
href, to self. -
#description ⇒ Object
Fetch the Nokogiri::HTML4::ElementDescription for this node.
-
#do_xinclude(options = XML::ParseOptions::DEFAULT_XML) {|options| ... } ⇒ Object
Do xinclude substitution on the subtree below node.
-
#document ⇒ Object
:call-seq: document() → Nokogiri::XML::Document.
-
#document? ⇒ Boolean
Returns true if this is a Document.
-
#dup(*args) ⇒ Object
(also: #clone)
:call-seq: dup → Nokogiri::XML::Node dup(depth) → Nokogiri::XML::Node dup(depth, new_parent_doc) → Nokogiri::XML::Node.
-
#each ⇒ Object
Iterate over each attribute name and value pair for this Node.
-
#element? ⇒ Boolean
(also: #elem?)
Returns true if this is an Element node.
-
#element_children ⇒ Object
(also: #elements)
:call-seq: element_children() → NodeSet elements() → NodeSet.
-
#encode_special_chars(string) ⇒ Object
:call-seq: encode_special_chars(string) → String.
-
#external_subset ⇒ Object
:call-seq: external_subset().
-
#first_element_child ⇒ Object
:call-seq: first_element_child() → Node.
-
#fragment(tags) ⇒ Object
Create a DocumentFragment containing
tags
that is relative to this context node. -
#fragment? ⇒ Boolean
Returns true if this is a DocumentFragment.
-
#html? ⇒ Boolean
Returns true if this is an HTML4::Document or HTML5::Document node.
-
#initialize(name, document) ⇒ Node
constructor
:call-seq: new(name, document) -> Nokogiri::XML::Node new(name, document) { |node| … } -> Nokogiri::XML::Node.
-
#inner_html(*args) ⇒ Object
Get the inner_html for this node’s Node#children.
-
#inner_html=(node_or_tags) ⇒ Object
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. -
#internal_subset ⇒ Object
:call-seq: internal_subset().
-
#key?(attribute) ⇒ Boolean
(also: #has_attribute?)
Returns true if
attribute
is set. -
#keys ⇒ Object
Get the attribute names for this Node.
-
#kwattr_add(attribute_name, keywords) ⇒ Object
:call-seq: kwattr_add(attribute_name, keywords) → self.
-
#kwattr_append(attribute_name, keywords) ⇒ Object
:call-seq: kwattr_append(attribute_name, keywords) → self.
-
#kwattr_remove(attribute_name, keywords) ⇒ Object
:call-seq: kwattr_remove(attribute_name, keywords) → self.
-
#kwattr_values(attribute_name) ⇒ Object
:call-seq: kwattr_values(attribute_name) → Array<String>.
-
#lang ⇒ Object
Searches the language of a node, i.e.
-
#lang= ⇒ Object
Set the language of a node, i.e.
-
#last_element_child ⇒ Object
:call-seq: last_element_child() → Node.
-
#line ⇒ Object
:call-seq: line() → Integer.
-
#line=(num) ⇒ Object
Sets the line for this Node.
-
#matches?(selector) ⇒ Boolean
Returns true if this Node matches
selector
. -
#namespace ⇒ Object
:call-seq: namespace() → Namespace.
-
#namespace=(ns) ⇒ Object
Set the default namespace on this node (as would be defined with an “xmlns=” attribute in XML source), as a Namespace object
ns
. -
#namespace_definitions ⇒ Object
:call-seq: namespace_definitions() → Array<Nokogiri::XML::Namespace>.
-
#namespace_scopes ⇒ Object
:call-seq: namespace_scopes() → Array<Nokogiri::XML::Namespace>.
-
#namespaced_key?(attribute, namespace) ⇒ Boolean
Returns true if
attribute
is set withnamespace
. -
#namespaces ⇒ Object
:call-seq: namespaces() → Hash<String(Namespace#prefix) ⇒ String(Namespace#href)>.
-
#content= ⇒ Object
Set the content for this Node.
-
#next_element ⇒ Object
Returns the next Nokogiri::XML::Element type sibling node.
-
#next_sibling ⇒ Object
(also: #next)
Returns the next sibling node.
-
#name ⇒ Object
(also: #name)
Returns the name for this Node.
-
#name=(new_name) ⇒ Object
(also: #name=)
Set the name for this Node.
-
#node_type ⇒ Object
(also: #type)
Get the type for this Node.
-
#parent ⇒ Object
Get the parent Node for this Node.
-
#parent=(parent_node) ⇒ Object
Set the parent Node for this Node.
-
#parse(string_or_io, options = nil) {|options| ... } ⇒ Object
Parse
string_or_io
as a document fragment within the context of this node. -
#path ⇒ Object
Returns the path associated with this Node.
-
#pointer_id ⇒ Object
:call-seq: pointer_id() → Integer.
-
#prepend_child(node_or_tags) ⇒ Object
Add
node_or_tags
as the first child of this Node. -
#previous_element ⇒ Object
Returns the previous Nokogiri::XML::Element type sibling node.
-
#previous_sibling ⇒ Object
(also: #previous)
Returns the previous sibling node.
-
#processing_instruction? ⇒ Boolean
Returns true if this is a ProcessingInstruction node.
-
#read_only? ⇒ Boolean
Is this a read only node?.
-
#remove_attribute(name) ⇒ Object
(also: #delete)
Remove the attribute named
name
. -
#remove_class(names = nil) ⇒ Object
:call-seq: remove_class(css_classes) → self.
-
#replace(node_or_tags) ⇒ Object
Replace this Node with
node_or_tags
. -
#serialize(*args, &block) ⇒ Object
Serialize Node using
options
. -
#swap(node_or_tags) ⇒ Object
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. -
#text? ⇒ Boolean
Returns true if this is a Text node.
-
#to_html(options = {}) ⇒ Object
Serialize this Node to HTML.
-
#to_s ⇒ Object
Turn this node in to a string.
-
#to_xhtml(options = {}) ⇒ Object
Serialize this Node to XHTML using
options
. -
#to_xml(options = {}) ⇒ Object
Serialize this Node to XML using
options
. -
#traverse {|_self| ... } ⇒ Object
Yields self and all children to
block
recursively. -
#unlink ⇒ Object
(also: #remove)
:call-seq: unlink() → self.
-
#value?(value) ⇒ Boolean
Does this Node’s attributes include <value>.
-
#values ⇒ Object
Get the attribute values for this Node.
-
#wrap(html) ⇒ Object
Add html around this node.
-
#write_html_to(io, options = {}) ⇒ Object
Write Node as HTML to
io
withoptions
. -
#write_to(io, *options) {|config| ... } ⇒ Object
Write Node to
io
withoptions
. -
#write_xhtml_to(io, options = {}) ⇒ Object
Write Node as XHTML to
io
withoptions
. -
#write_xml_to(io, options = {}) ⇒ Object
Write Node as XML to
io
withoptions
. -
#xml? ⇒ Boolean
Returns true if this is an XML::Document node.
Methods included from ClassResolver
Methods included from Searchable
#>, #at, #at_css, #at_xpath, #css, #search, #xpath
Methods included from PP::Node
Constructor Details
#initialize(name, document) ⇒ Node
:call-seq:
new(name, document) -> Nokogiri::XML::Node
new(name, document) { |node| ... } -> Nokogiri::XML::Node
Create a new node with name
that belongs to document
.
If you intend to add a node to a document tree, it’s likely that you will prefer one of the Nokogiri::XML::Node methods like #add_child, #add_next_sibling, #replace, etc. which will both create an element (or subtree) and place it in the document tree.
Another alternative, if you are concerned about performance, is Nokogiri::XML::Document#create_element which accepts additional arguments for contents or attributes but (like this method) avoids parsing markup.
- Parameters
-
name
(String) -
document
(Nokogiri::XML::Document) The document to which the the returned node will belong.
- Yields
-
Nokogiri::XML::Node
- Returns
-
Nokogiri::XML::Node
126 127 128 |
# File 'lib/nokogiri/xml/node.rb', line 126 def initialize(name, document) # rubocop:disable Style/RedundantInitialize # This is intentionally empty. end |
Class Method Details
.new(*args) ⇒ Object
:nodoc: documented in lib/nokogiri/xml/node.rb
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 |
# File 'ext/nokogiri/xml_node.c', line 1765
static VALUE
rb_xml_node_new(int argc, VALUE *argv, VALUE klass)
{
xmlNodePtr c_document_node;
xmlNodePtr c_node;
VALUE rb_name;
VALUE rb_document_node;
VALUE rest;
VALUE rb_node;
rb_scan_args(argc, argv, "2*", &rb_name, &rb_document_node, &rest);
if (!rb_obj_is_kind_of(rb_document_node, cNokogiriXmlNode)) {
rb_raise(rb_eArgError, "document must be a Nokogiri::XML::Node");
}
if (!rb_obj_is_kind_of(rb_document_node, cNokogiriXmlDocument)) {
// TODO: deprecate allowing Node
rb_warn("Passing a Node as the second parameter to Node.new is deprecated. Please pass a Document instead, or prefer an alternative constructor like Node#add_child. This will become an error in a future release of Nokogiri.");
}
Data_Get_Struct(rb_document_node, xmlNode, c_document_node);
c_node = xmlNewNode(NULL, (xmlChar *)StringValueCStr(rb_name));
c_node->doc = c_document_node->doc;
noko_xml_document_pin_node(c_node);
rb_node = noko_xml_node_wrap(
klass == cNokogiriXmlNode ? (VALUE)NULL : klass,
c_node
);
rb_obj_call_init(rb_node, argc, argv);
if (rb_block_given_p()) { rb_yield(rb_node); }
return rb_node;
}
|
Instance Method Details
#<<(node_or_tags) ⇒ Object
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
.
191 192 193 194 |
# File 'lib/nokogiri/xml/node.rb', line 191 def <<() add_child() self end |
#<=>(other) ⇒ Object
Compare two Node objects with respect to their Document. Nodes from different documents cannot be compared.
1163 1164 1165 1166 1167 1168 |
# File 'lib/nokogiri/xml/node.rb', line 1163 def <=>(other) return nil unless other.is_a?(Nokogiri::XML::Node) return nil unless document == other.document compare(other) end |
#==(other) ⇒ Object
Test to see if this Node is equal to other
1153 1154 1155 1156 1157 1158 |
# File 'lib/nokogiri/xml/node.rb', line 1153 def ==(other) return false unless other return false unless other.respond_to?(:pointer_id) pointer_id == other.pointer_id end |
#[](name) ⇒ Object Also known as: get_attribute, attr
:call-seq: [](name) → (String, nil)
Fetch an attribute from this node.
⚠ Note that attributes with namespaces cannot be accessed with this method. To access namespaced attributes, use #attribute_with_ns.
- Returns
-
(String, nil) value of the attribute
name
, ornil
if no matching attribute exists
Example
doc = Nokogiri::XML("<root><child size='large' class='big wide tall'/></root>")
child = doc.at_css("child")
child["size"] # => "large"
child["class"] # => "big wide tall"
Example: Namespaced attributes will not be returned.
⚠ Note namespaced attributes may be accessed with #attribute or #attribute_with_ns
doc = Nokogiri::XML(<<~EOF)
<root xmlns:width='http://example.com/widths'>
<child width:size='broad'/>
</root>
EOF
doc.at_css("child")["size"] # => nil
doc.at_css("child").attribute("size").value # => "broad"
doc.at_css("child").attribute_with_ns("size", "http://example.com/widths").value
# => "broad"
419 420 421 |
# File 'lib/nokogiri/xml/node.rb', line 419 def [](name) get(name.to_s) end |
#[]=(name, value) ⇒ Object Also known as: set_attribute
:call-seq: []=(name, value) → value
Update the attribute name
to value
, or create the attribute if it does not exist.
⚠ Note that attributes with namespaces cannot be accessed with this method. To access namespaced attributes for update, use #attribute_with_ns. To add a namespaced attribute, see the example below.
- Returns
-
value
Example
doc = Nokogiri::XML("<root><child/></root>")
child = doc.at_css("child")
child["size"] = "broad"
child.to_html
# => "<child size=\"broad\"></child>"
Example: Add a namespaced attribute.
doc = Nokogiri::XML(<<~EOF)
<root xmlns:width='http://example.com/widths'>
<child/>
</root>
EOF
child = doc.at_css("child")
child["size"] = "broad"
ns = doc.root.namespace_definitions.find { |ns| ns.prefix == "width" }
child.attribute("size").namespace = ns
doc.to_html
# => "<root xmlns:width=\"http://example.com/widths\">\n" +
# " <child width:size=\"broad\"></child>\n" +
# "</root>\n"
457 458 459 |
# File 'lib/nokogiri/xml/node.rb', line 457 def []=(name, value) set(name.to_s, value.to_s) end |
#accept(visitor) ⇒ Object
Accept a visitor. This method calls “visit” on visitor
with self.
1147 1148 1149 |
# File 'lib/nokogiri/xml/node.rb', line 1147 def accept(visitor) visitor.visit(self) end |
#add_child(node_or_tags) ⇒ Object
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 <<.
145 146 147 148 149 150 151 152 153 |
# File 'lib/nokogiri/xml/node.rb', line 145 def add_child() = coerce() if .is_a?(XML::NodeSet) .each { |n| add_child_node_and_reparent_attrs(n) } else add_child_node_and_reparent_attrs() end end |
#add_class(names) ⇒ Object
:call-seq: add_class(names) → self
Ensure HTML CSS classes are present on self
. Any CSS classes in names
that already exist in the “class” attribute are not added. Note that any existing duplicates in the “class” attribute are not removed. Compare with #append_class.
This is a convenience function and is equivalent to:
node.kwattr_add("class", names)
See related: #kwattr_add, #classes, #append_class, #remove_class
- Parameters
-
names
(String, Array<String>)CSS class names to be added to the Node’s “class” attribute. May be a string containing whitespace-delimited names, or an Array of String names. Any class names already present will not be added. Any class names not present will be added. If no “class” attribute exists, one is created.
- Returns
-
self
(Node) for ease of chaining method calls.
Example: Ensure that the node has CSS class “section”
node # => <div></div>
node.add_class("section") # => <div class="section"></div>
node.add_class("section") # => <div class="section"></div> # duplicate not added
Example: Ensure that the node has CSS classes “section” and “header”, via a String argument
Note that the CSS class “section” is not added because it is already present. Note also that the pre-existing duplicate CSS class “section” is not removed.
node # => <div class="section section"></div>
node.add_class("section header") # => <div class="section section header"></div>
Example: Ensure that the node has CSS classes “section” and “header”, via an Array argument
node # => <div></div>
node.add_class(["section", "header"]) # => <div class="section header"></div>
622 623 624 |
# File 'lib/nokogiri/xml/node.rb', line 622 def add_class(names) kwattr_add("class", names) end |
#add_namespace_definition(rb_prefix, rb_href) ⇒ Object Also known as: add_namespace
:call-seq:
add_namespace_definition(prefix, href) → Nokogiri::XML::Namespace
add_namespace(prefix, href) → Nokogiri::XML::Namespace
:category: Manipulating Document Structure
Adds a namespace definition to this node with prefix
using href
value, as if this node had included an attribute “xmlns:prefix=href”.
A default namespace definition for this node can be added by passing nil
for prefix
.
- Parameters
-
prefix
(String,nil
) An XML Name -
href
(String) The URI reference
- Returns
-
The new Nokogiri::XML::Namespace
Example: adding a non-default namespace definition
doc = Nokogiri::XML("<store><inventory></inventory></store>")
inventory = doc.at_css("inventory")
inventory.add_namespace_definition("automobile", "http://alices-autos.com/")
inventory.add_namespace_definition("bicycle", "http://bobs-bikes.com/")
inventory.add_child("<automobile:tire>Michelin model XGV, size 75R</automobile:tire>")
doc.to_xml
# => "<?xml version=\"1.0\"?>\n" +
# "<store>\n" +
# " <inventory xmlns:automobile=\"http://alices-autos.com/\" xmlns:bicycle=\"http://bobs-bikes.com/\">\n" +
# " <automobile:tire>Michelin model XGV, size 75R</automobile:tire>\n" +
# " </inventory>\n" +
# "</store>\n"
Example: adding a default namespace definition
doc = Nokogiri::XML("<store><inventory><tire>Michelin model XGV, size 75R</tire></inventory></store>")
doc.at_css("tire").add_namespace_definition(nil, "http://bobs-bikes.com/")
doc.to_xml
# => "<?xml version=\"1.0\"?>\n" +
# "<store>\n" +
# " <inventory>\n" +
# " <tire xmlns=\"http://bobs-bikes.com/\">Michelin model XGV, size 75R</tire>\n" +
# " </inventory>\n" +
# "</store>\n"
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
# File 'ext/nokogiri/xml_node.c', line 435
static VALUE
rb_xml_node_add_namespace_definition(VALUE rb_node, VALUE rb_prefix, VALUE rb_href)
{
xmlNodePtr c_node, element;
xmlNsPtr c_namespace;
const xmlChar *c_prefix = (const xmlChar *)(NIL_P(rb_prefix) ? NULL : StringValueCStr(rb_prefix));
Data_Get_Struct(rb_node, xmlNode, c_node);
element = c_node ;
c_namespace = xmlSearchNs(c_node->doc, c_node, c_prefix);
if (!c_namespace) {
if (c_node->type != XML_ELEMENT_NODE) {
element = c_node->parent;
}
c_namespace = xmlNewNs(element, (const xmlChar *)StringValueCStr(rb_href), c_prefix);
}
if (!c_namespace) {
return Qnil ;
}
if (NIL_P(rb_prefix) || c_node != element) {
xmlSetNs(c_node, c_namespace);
}
return noko_xml_namespace_wrap(c_namespace, c_node->doc);
}
|
#add_next_sibling(node_or_tags) ⇒ Object Also known as: next=
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
.
217 218 219 220 221 222 |
# File 'lib/nokogiri/xml/node.rb', line 217 def add_next_sibling() raise ArgumentError, "A document may not have multiple root nodes." if parent&.document? && !(.comment? || .processing_instruction?) add_sibling(:next, ) end |
#add_previous_sibling(node_or_tags) ⇒ Object Also known as: previous=
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
.
203 204 205 206 207 208 |
# File 'lib/nokogiri/xml/node.rb', line 203 def add_previous_sibling() raise ArgumentError, "A document may not have multiple root nodes." if parent&.document? && !(.comment? || .processing_instruction?) add_sibling(:previous, ) end |
#after(node_or_tags) ⇒ Object
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
.
243 244 245 246 |
# File 'lib/nokogiri/xml/node.rb', line 243 def after() add_next_sibling() self end |
#ancestors(selector = nil) ⇒ Object
Get a list of ancestor Node for this Node. If selector
is given, the ancestors must match selector
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 |
# File 'lib/nokogiri/xml/node.rb', line 1116 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 |
#append_class(names) ⇒ Object
:call-seq: append_class(names) → self
Add HTML CSS classes to self
, regardless of duplication. Compare with #add_class.
This is a convenience function and is equivalent to:
node.kwattr_append("class", names)
See related: #kwattr_append, #classes, #add_class, #remove_class
- Parameters
-
names
(String, Array<String>)CSS class names to be appended to the Node’s “class” attribute. May be a string containing whitespace-delimited names, or an Array of String names. All class names passed in will be appended to the “class” attribute even if they are already present in the attribute value. If no “class” attribute exists, one is created.
- Returns
-
self
(Node) for ease of chaining method calls.
Example: Append “section” to the node’s CSS “class” attribute
node # => <div></div>
node.append_class("section") # => <div class="section"></div>
node.append_class("section") # => <div class="section section"></div> # duplicate added!
Example: Append “section” and “header” to the noded’s CSS “class” attribute, via a String argument
Note that the CSS class “section” is appended even though it is already present
node # => <div class="section section"></div>
node.append_class("section header") # => <div class="section section section header"></div>
Example: Append “section” and “header” to the node’s CSS “class” attribute, via an Array argument
node # => <div></div>
node.append_class(["section", "header"]) # => <div class="section header"></div>
node.append_class(["section", "header"]) # => <div class="section header section header"></div>
666 667 668 |
# File 'lib/nokogiri/xml/node.rb', line 666 def append_class(names) kwattr_append("class", names) end |
#attribute(name) ⇒ Object
:call-seq: attribute(name) → Nokogiri::XML::Attr
:category: Working With Node Attributes
- Returns
-
Attribute (Nokogiri::XML::Attr) belonging to this node with name
name
.
⚠ Note that attribute namespaces are ignored and only the simple (non-namespace-prefixed) name is used to find a matching attribute. In case of a simple name collision, only one of the matching attributes will be returned. In this case, you will need to use #attribute_with_ns.
Example:
doc = Nokogiri::XML("<root><child size='large' class='big wide tall'/></root>")
child = doc.at_css("child")
child.attribute("size") # => #<Nokogiri::XML::Attr:0x550 name="size" value="large">
child.attribute("class") # => #<Nokogiri::XML::Attr:0x564 name="class" value="big wide tall">
Example showing that namespaced attributes will not be returned:
⚠ Note that only one of the two matching attributes is returned.
doc = Nokogiri::XML(<<~EOF)
<root xmlns:width='http://example.com/widths'
xmlns:height='http://example.com/heights'>
<child width:size='broad' height:size='tall'/>
</root>
EOF
doc.at_css("child").attribute("size")
# => #(Attr:0x550 {
# name = "size",
# namespace = #(Namespace:0x564 {
# prefix = "width",
# href = "http://example.com/widths"
# }),
# value = "broad"
# })
504 505 506 507 508 509 510 511 512 513 514 |
# File 'ext/nokogiri/xml_node.c', line 504
static VALUE
rb_xml_node_attribute(VALUE self, VALUE name)
{
xmlNodePtr node;
xmlAttrPtr prop;
Data_Get_Struct(self, xmlNode, node);
prop = xmlHasProp(node, (xmlChar *)StringValueCStr(name));
if (! prop) { return Qnil; }
return noko_xml_node_wrap(Qnil, (xmlNodePtr)prop);
}
|
#attribute_nodes ⇒ Object
:call-seq: attribute_nodes() → Array<Nokogiri::XML::Attr>
:category: Working With Node Attributes
- Returns
-
Attributes (an Array of Nokogiri::XML::Attr) belonging to this node.
Note that this is the preferred alternative to #attributes when the simple (non-namespace-prefixed) attribute names may collide.
Example:
Contrast this with the colliding-name example from #attributes.
doc = Nokogiri::XML(<<~EOF)
<root xmlns:width='http://example.com/widths'
xmlns:height='http://example.com/heights'>
<child width:size='broad' height:size='tall'/>
</root>
EOF
doc.at_css("child").attribute_nodes
# => [#(Attr:0x550 {
# name = "size",
# namespace = #(Namespace:0x564 {
# prefix = "width",
# href = "http://example.com/widths"
# }),
# value = "broad"
# }),
# #(Attr:0x578 {
# name = "size",
# namespace = #(Namespace:0x58c {
# prefix = "height",
# href = "http://example.com/heights"
# }),
# value = "tall"
# })]
555 556 557 558 559 560 561 562 563 |
# File 'ext/nokogiri/xml_node.c', line 555
static VALUE
rb_xml_node_attribute_nodes(VALUE rb_node)
{
xmlNodePtr c_node;
Data_Get_Struct(rb_node, xmlNode, c_node);
return noko_xml_node_attrs(c_node);
}
|
#attribute_with_ns(name, namespace) ⇒ Object
:call-seq: attribute_with_ns(name, namespace) → Nokogiri::XML::Attr
:category: Working With Node Attributes
- Returns
-
Attribute (Nokogiri::XML::Attr) belonging to this node with matching
name
andnamespace
.
-
name
(String): the simple (non-namespace-prefixed) name of the attribute -
namespace
(String): the URI of the attribute’s namespace
See related: #attribute
Example:
doc = Nokogiri::XML(<<~EOF)
<root xmlns:width='http://example.com/widths'
xmlns:height='http://example.com/heights'>
<child width:size='broad' height:size='tall'/>
</root>
EOF
doc.at_css("child").attribute_with_ns("size", "http://example.com/widths")
# => #(Attr:0x550 {
# name = "size",
# namespace = #(Namespace:0x564 {
# prefix = "width",
# href = "http://example.com/widths"
# }),
# value = "broad"
# })
doc.at_css("child").attribute_with_ns("size", "http://example.com/heights")
# => #(Attr:0x578 {
# name = "size",
# namespace = #(Namespace:0x58c {
# prefix = "height",
# href = "http://example.com/heights"
# }),
# value = "tall"
# })
607 608 609 610 611 612 613 614 615 616 617 618 |
# File 'ext/nokogiri/xml_node.c', line 607
static VALUE
rb_xml_node_attribute_with_ns(VALUE self, VALUE name, VALUE namespace)
{
xmlNodePtr node;
xmlAttrPtr prop;
Data_Get_Struct(self, xmlNode, node);
prop = xmlHasNsProp(node, (xmlChar *)StringValueCStr(name),
NIL_P(namespace) ? NULL : (xmlChar *)StringValueCStr(namespace));
if (! prop) { return Qnil; }
return noko_xml_node_wrap(Qnil, (xmlNodePtr)prop);
}
|
#attributes ⇒ Object
:call-seq: attributes() → Hash<String ⇒ Nokogiri::XML::Attr>
Fetch this node’s attributes.
⚠ Because the keys do not include any namespace information for the attribute, in case of a simple name collision, not all attributes will be returned. In this case, you will need to use #attribute_nodes.
- Returns
-
Hash containing attributes belonging to
self
. The hash keys are String attribute names (without the namespace), and the hash values are Nokogiri::XML::Attr.
Example with no namespaces:
doc = Nokogiri::XML("<root><child size='large' class='big wide tall'/></root>")
doc.at_css("child").attributes
# => {"size"=>#(Attr:0x550 { name = "size", value = "large" }),
# "class"=>#(Attr:0x564 { name = "class", value = "big wide tall" })}
Example with a namespace:
doc = Nokogiri::XML("<root xmlns:desc='http://example.com/sizes'><child desc:size='large'/></root>")
doc.at_css("child").attributes
# => {"size"=>
# #(Attr:0x550 {
# name = "size",
# namespace = #(Namespace:0x564 {
# prefix = "desc",
# href = "http://example.com/sizes"
# }),
# value = "large"
# })}
Example with an attribute name collision:
⚠ Note that only one of the attributes is returned in the Hash.
doc = Nokogiri::XML(<<~EOF)
<root xmlns:width='http://example.com/widths'
xmlns:height='http://example.com/heights'>
<child width:size='broad' height:size='tall'/>
</root>
EOF
doc.at_css("child").attributes
# => {"size"=>
# #(Attr:0x550 {
# name = "size",
# namespace = #(Namespace:0x564 {
# prefix = "height",
# href = "http://example.com/heights"
# }),
# value = "tall"
# })}
516 517 518 519 520 |
# File 'lib/nokogiri/xml/node.rb', line 516 def attributes attribute_nodes.each_with_object({}) do |node, hash| hash[node.node_name] = node end end |
#before(node_or_tags) ⇒ Object
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
.
231 232 233 234 |
# File 'lib/nokogiri/xml/node.rb', line 231 def before() add_previous_sibling() self end |
#blank? ⇒ Boolean
- Returns
-
true
if the node is an empty or whitespace-only text or cdata node, elsefalse
.
Example:
Nokogiri("<root><child/></root>").root.child.blank? # => false
Nokogiri("<root>\t \n</root>").root.child.blank? # => true
Nokogiri("<root><![CDATA[\t \n]]></root>").root.child.blank? # => true
Nokogiri("<root>not-blank</root>").root.child
.tap { |n| n.content = "" }.blank # => true
635 636 637 638 639 640 641 |
# File 'ext/nokogiri/xml_node.c', line 635
static VALUE
rb_xml_node_blank_eh(VALUE self)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
return (1 == xmlIsBlankNode(node)) ? Qtrue : Qfalse ;
}
|
#canonicalize(mode = XML::XML_C14N_1_0, inclusive_namespaces = nil, with_comments = false) ⇒ Object
1306 1307 1308 1309 1310 1311 1312 |
# File 'lib/nokogiri/xml/node.rb', line 1306 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? ⇒ Boolean
Returns true if this is a CDATA
1037 1038 1039 |
# File 'lib/nokogiri/xml/node.rb', line 1037 def cdata? type == CDATA_SECTION_NODE end |
#child ⇒ Object
:call-seq: child() → Nokogiri::XML::Node
:category: Traversing Document Structure
- Returns
-
First of this node’s children, or
nil
if there are no children
This is a convenience method and is equivalent to:
node.children.first
See related: #children
657 658 659 660 661 662 663 664 665 666 667 |
# File 'ext/nokogiri/xml_node.c', line 657
static VALUE
rb_xml_node_child(VALUE self)
{
xmlNodePtr node, child;
Data_Get_Struct(self, xmlNode, node);
child = node->children;
if (!child) { return Qnil; }
return noko_xml_node_wrap(Qnil, child);
}
|
#children ⇒ Object
:call-seq: children() → Nokogiri::XML::NodeSet
:category: Traversing Document Structure
- Returns
-
Nokogiri::XML::NodeSet containing this node’s children.
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 |
# File 'ext/nokogiri/xml_node.c', line 677
static VALUE
rb_xml_node_children(VALUE self)
{
xmlNodePtr node;
xmlNodePtr child;
xmlNodeSetPtr set;
VALUE document;
VALUE node_set;
Data_Get_Struct(self, xmlNode, node);
child = node->children;
set = xmlXPathNodeSetCreate(child);
document = DOC_RUBY_OBJECT(node->doc);
if (!child) { return noko_xml_node_set_wrap(set, document); }
child = child->next;
while (NULL != child) {
xmlXPathNodeSetAddUnique(set, child);
child = child->next;
}
node_set = noko_xml_node_set_wrap(set, document);
return node_set;
}
|
#children=(node_or_tags) ⇒ Object
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.
Also see related method inner_html=
262 263 264 265 266 267 268 269 270 |
# File 'lib/nokogiri/xml/node.rb', line 262 def children=() = coerce() children.unlink if .is_a?(XML::NodeSet) .each { |n| add_child_node_and_reparent_attrs(n) } else add_child_node_and_reparent_attrs() end end |
#classes ⇒ Object
:call-seq: classes() → Array<String>
Fetch CSS class names of a Node.
This is a convenience function and is equivalent to:
node.kwattr_values("class")
See related: #kwattr_values, #add_class, #append_class, #remove_class
- Returns
-
The CSS classes (Array of String) present in the Node’s “class” attribute. If the attribute is empty or non-existent, the return value is an empty array.
Example
node # => <div class="section title header"></div>
node.classes # => ["section", "title", "header"]
576 577 578 |
# File 'lib/nokogiri/xml/node.rb', line 576 def classes kwattr_values("class") end |
#comment? ⇒ Boolean
Returns true if this is a Comment
1032 1033 1034 |
# File 'lib/nokogiri/xml/node.rb', line 1032 def comment? type == COMMENT_NODE end |
#content ⇒ Object Also known as: inner_text, text, to_str
:call-seq:
content() → String
inner_text() → String
text() → String
to_str() → String
- Returns
-
Contents of all the text nodes in this node’s subtree, concatenated together into a single String.
⚠ Note that entities will always be expanded in the returned String.
See related: #inner_html
Example of how entities are handled:
Note that <
becomes <
in the returned String.
doc = Nokogiri::XML.fragment("<child>a < b</child>")
doc.at_css("child").content
# => "a < b"
Example of how a subtree is handled:
Note that the <span>
tags are omitted and only the text node contents are returned, concatenated into a single string.
doc = Nokogiri::XML.fragment("<child><span>first</span> <span>second</span></child>")
doc.at_css("child").content
# => "first second"
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 |
# File 'ext/nokogiri/xml_node.c', line 739
static VALUE
rb_xml_node_content(VALUE self)
{
xmlNodePtr node;
xmlChar *content;
Data_Get_Struct(self, xmlNode, node);
content = xmlNodeGetContent(node);
if (content) {
VALUE rval = NOKOGIRI_STR_NEW2(content);
xmlFree(content);
return rval;
}
return Qnil;
}
|
#content=(string) ⇒ Object
Set the Node’s content to a Text node containing string
. The string gets XML escaped, not interpreted as markup.
318 319 320 |
# File 'lib/nokogiri/xml/node.rb', line 318 def content=(string) self.native_content = encode_special_chars(string.to_s) end |
#create_external_subset(name, external_id, system_id) ⇒ Object
:call-seq:
create_external_subset(name, external_id, system_id)
Create an external subset
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 |
# File 'ext/nokogiri/xml_node.c', line 857
static VALUE
create_external_subset(VALUE self, VALUE name, VALUE external_id, VALUE system_id)
{
xmlNodePtr node;
xmlDocPtr doc;
xmlDtdPtr dtd;
Data_Get_Struct(self, xmlNode, node);
doc = node->doc;
if (doc->extSubset) {
rb_raise(rb_eRuntimeError, "Document already has an external subset");
}
dtd = xmlNewDtd(
doc,
NIL_P(name) ? NULL : (const xmlChar *)StringValueCStr(name),
NIL_P(external_id) ? NULL : (const xmlChar *)StringValueCStr(external_id),
NIL_P(system_id) ? NULL : (const xmlChar *)StringValueCStr(system_id)
);
if (!dtd) { return Qnil; }
return noko_xml_node_wrap(Qnil, (xmlNodePtr)dtd);
}
|
#create_internal_subset(name, external_id, system_id) ⇒ Object
:call-seq:
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">
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 |
# File 'ext/nokogiri/xml_node.c', line 824
static VALUE
create_internal_subset(VALUE self, VALUE name, VALUE external_id, VALUE system_id)
{
xmlNodePtr node;
xmlDocPtr doc;
xmlDtdPtr dtd;
Data_Get_Struct(self, xmlNode, node);
doc = node->doc;
if (xmlGetIntSubset(doc)) {
rb_raise(rb_eRuntimeError, "Document already has an internal subset");
}
dtd = xmlCreateIntSubset(
doc,
NIL_P(name) ? NULL : (const xmlChar *)StringValueCStr(name),
NIL_P(external_id) ? NULL : (const xmlChar *)StringValueCStr(external_id),
NIL_P(system_id) ? NULL : (const xmlChar *)StringValueCStr(system_id)
);
if (!dtd) { return Qnil; }
return noko_xml_node_wrap(Qnil, (xmlNodePtr)dtd);
}
|
#css_path ⇒ Object
Get the path to this node as a CSS expression
1107 1108 1109 1110 1111 |
# File 'lib/nokogiri/xml/node.rb', line 1107 def css_path path.split(%r{/}).map do |part| part.empty? ? nil : part.gsub(/\[(\d+)\]/, ':nth-of-type(\1)') end.compact.join(" > ") end |
#decorate! ⇒ Object
Decorate this node with the decorators set up in this node’s Document
132 133 134 |
# File 'lib/nokogiri/xml/node.rb', line 132 def decorate! document.decorate(self) end |
#default_namespace=(url) ⇒ Object
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=
334 335 336 |
# File 'lib/nokogiri/xml/node.rb', line 334 def default_namespace=(url) add_namespace_definition(nil, url) end |
#description ⇒ Object
Fetch the Nokogiri::HTML4::ElementDescription for this node. Returns nil on XML documents and on unknown tags.
1074 1075 1076 1077 1078 |
# File 'lib/nokogiri/xml/node.rb', line 1074 def description return nil if document.xml? Nokogiri::HTML4::ElementDescription[name] end |
#do_xinclude(options = XML::ParseOptions::DEFAULT_XML) {|options| ... } ⇒ Object
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.
361 362 363 364 365 366 367 |
# File 'lib/nokogiri/xml/node.rb', line 361 def do_xinclude( = XML::ParseOptions::DEFAULT_XML) = Nokogiri::XML::ParseOptions.new() if Integer === yield if block_given? # call c extension process_xincludes(.to_i) end |
#document ⇒ Object
:call-seq: document() → Nokogiri::XML::Document
:category: Traversing Document Structure
- Returns
-
Parent Nokogiri::XML::Document for this node
764 765 766 767 768 769 770 |
# File 'ext/nokogiri/xml_node.c', line 764
static VALUE
rb_xml_node_document(VALUE self)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
return DOC_RUBY_OBJECT(node->doc);
}
|
#document? ⇒ Boolean
Returns true if this is a Document
1052 1053 1054 |
# File 'lib/nokogiri/xml/node.rb', line 1052 def document? is_a?(XML::Document) end |
#dup(*args) ⇒ Object Also known as: clone
:call-seq:
dup → Nokogiri::XML::Node
dup(depth) → Nokogiri::XML::Node
dup(depth, new_parent_doc) → Nokogiri::XML::Node
Copy this node.
- Parameters
-
depth
0 is a shallow copy, 1 (the default) is a deep copy. -
new_parent_doc
The new node’s parent Document. Defaults to the this node’s document.
- Returns
-
The new Nokgiri::XML::Node
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 |
# File 'ext/nokogiri/xml_node.c', line 949
static VALUE
duplicate_node(int argc, VALUE *argv, VALUE self)
{
VALUE r_level, r_new_parent_doc;
int level;
int n_args;
xmlDocPtr new_parent_doc;
xmlNodePtr node, dup;
Data_Get_Struct(self, xmlNode, node);
n_args = rb_scan_args(argc, argv, "02", &r_level, &r_new_parent_doc);
if (n_args < 1) {
r_level = INT2NUM((long)1);
}
level = (int)NUM2INT(r_level);
if (n_args < 2) {
new_parent_doc = node->doc;
} else {
Data_Get_Struct(r_new_parent_doc, xmlDoc, new_parent_doc);
}
dup = xmlDocCopyNode(node, new_parent_doc, level);
if (dup == NULL) { return Qnil; }
noko_xml_document_pin_node(dup);
return noko_xml_node_wrap(rb_obj_class(self), dup);
}
|
#each ⇒ Object
Iterate over each attribute name and value pair for this Node.
542 543 544 545 546 |
# File 'lib/nokogiri/xml/node.rb', line 542 def each attribute_nodes.each do |node| yield [node.node_name, node.value] end end |
#element? ⇒ Boolean Also known as: elem?
Returns true if this is an Element node
1088 1089 1090 |
# File 'lib/nokogiri/xml/node.rb', line 1088 def element? type == ELEMENT_NODE end |
#element_children ⇒ Object Also known as: elements
:call-seq:
element_children() → NodeSet
elements() → NodeSet
- Returns
-
The node’s child elements as a NodeSet. Only children that are elements will be returned, which notably excludes Text nodes.
Example:
Note that #children returns the Text node “hello” while #element_children does not.
div = Nokogiri::HTML5("<div>hello<span>world</span>").at_css("div")
div.element_children
# => [#<Nokogiri::XML::Element:0x50 name="span" children=[#<Nokogiri::XML::Text:0x3c "world">]>]
div.children
# => [#<Nokogiri::XML::Text:0x64 "hello">,
# #<Nokogiri::XML::Element:0x50 name="span" children=[#<Nokogiri::XML::Text:0x3c "world">]>]
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 |
# File 'ext/nokogiri/xml_node.c', line 1110
static VALUE
rb_xml_node_element_children(VALUE self)
{
xmlNodePtr node;
xmlNodePtr child;
xmlNodeSetPtr set;
VALUE document;
VALUE node_set;
Data_Get_Struct(self, xmlNode, node);
child = xmlFirstElementChild(node);
set = xmlXPathNodeSetCreate(child);
document = DOC_RUBY_OBJECT(node->doc);
if (!child) { return noko_xml_node_set_wrap(set, document); }
child = xmlNextElementSibling(child);
while (NULL != child) {
xmlXPathNodeSetAddUnique(set, child);
child = xmlNextElementSibling(child);
}
node_set = noko_xml_node_set_wrap(set, document);
return node_set;
}
|
#encode_special_chars(string) ⇒ Object
:call-seq: encode_special_chars(string) → String
Encode any special characters in string
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 |
# File 'ext/nokogiri/xml_node.c', line 793
static VALUE
encode_special_chars(VALUE self, VALUE string)
{
xmlNodePtr node;
xmlChar *encoded;
VALUE encoded_str;
Data_Get_Struct(self, xmlNode, node);
encoded = xmlEncodeSpecialChars(
node->doc,
(const xmlChar *)StringValueCStr(string)
);
encoded_str = NOKOGIRI_STR_NEW2(encoded);
xmlFree(encoded);
return encoded_str;
}
|
#external_subset ⇒ Object
:call-seq:
external_subset()
Get the external subset
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 |
# File 'ext/nokogiri/xml_node.c', line 890
static VALUE
external_subset(VALUE self)
{
xmlNodePtr node;
xmlDocPtr doc;
xmlDtdPtr dtd;
Data_Get_Struct(self, xmlNode, node);
if (!node->doc) { return Qnil; }
doc = node->doc;
dtd = doc->extSubset;
if (!dtd) { return Qnil; }
return noko_xml_node_wrap(Qnil, (xmlNodePtr)dtd);
}
|
#first_element_child ⇒ Object
:call-seq:
first_element_child() → Node
- Returns
-
The first child Node that is an element.
Example:
Note that the “hello” child, which is a Text node, is skipped and the <span>
element is returned.
div = Nokogiri::HTML5("<div>hello<span>world</span>").at_css("div")
div.first_element_child
# => #(Element:0x3c { name = "span", children = [ #(Text "world")] })
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 |
# File 'ext/nokogiri/xml_node.c', line 1154
static VALUE
rb_xml_node_first_element_child(VALUE self)
{
xmlNodePtr node, child;
Data_Get_Struct(self, xmlNode, node);
child = xmlFirstElementChild(node);
if (!child) { return Qnil; }
return noko_xml_node_wrap(Qnil, child);
}
|
#fragment(tags) ⇒ Object
Create a DocumentFragment containing tags
that is relative to this context node.
929 930 931 |
# File 'lib/nokogiri/xml/node.rb', line 929 def fragment() document.("DocumentFragment").new(document, , self) end |
#fragment? ⇒ Boolean
Returns true if this is a DocumentFragment
1067 1068 1069 |
# File 'lib/nokogiri/xml/node.rb', line 1067 def fragment? type == DOCUMENT_FRAG_NODE end |
#html? ⇒ Boolean
Returns true if this is an HTML4::Document or HTML5::Document node
1047 1048 1049 |
# File 'lib/nokogiri/xml/node.rb', line 1047 def html? type == HTML_DOCUMENT_NODE end |
#inner_html(*args) ⇒ Object
Get the inner_html for this node’s Node#children
1102 1103 1104 |
# File 'lib/nokogiri/xml/node.rb', line 1102 def inner_html(*args) children.map { |x| x.to_html(*args) }.join end |
#inner_html=(node_or_tags) ⇒ Object
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.
Also see related method children=
253 254 255 |
# File 'lib/nokogiri/xml/node.rb', line 253 def inner_html=() self.children = end |
#internal_subset ⇒ Object
:call-seq:
internal_subset()
Get the internal subset
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 |
# File 'ext/nokogiri/xml_node.c', line 915
static VALUE
internal_subset(VALUE self)
{
xmlNodePtr node;
xmlDocPtr doc;
xmlDtdPtr dtd;
Data_Get_Struct(self, xmlNode, node);
if (!node->doc) { return Qnil; }
doc = node->doc;
dtd = xmlGetIntSubset(doc);
if (!dtd) { return Qnil; }
return noko_xml_node_wrap(Qnil, (xmlNodePtr)dtd);
}
|
#key?(attribute) ⇒ Boolean Also known as: has_attribute?
Returns true if attribute
is set
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 |
# File 'ext/nokogiri/xml_node.c', line 1199
static VALUE
key_eh(VALUE self, VALUE attribute)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
if (xmlHasProp(node, (xmlChar *)StringValueCStr(attribute))) {
return Qtrue;
}
return Qfalse;
}
|
#keys ⇒ Object
Get the attribute names for this Node.
536 537 538 |
# File 'lib/nokogiri/xml/node.rb', line 536 def keys attribute_nodes.map(&:node_name) end |
#kwattr_add(attribute_name, keywords) ⇒ Object
:call-seq:
kwattr_add(attribute_name, keywords) → self
Ensure that values are present in a keyword attribute.
Any values in keywords
that already exist in the Node’s attribute values are not added. Note that any existing duplicates in the attribute values are not removed. Compare with #kwattr_append.
A “keyword attribute” is a node attribute that contains a set of space-delimited values. Perhaps the most familiar example of this is the HTML “class” attribute used to contain CSS classes. But other keyword attributes exist, for instance the “rel” attribute.
See also #add_class, #kwattr_values, #kwattr_append, #kwattr_remove
- Parameters
-
attribute_name
(String) The name of the keyword attribute to be modified. -
keywords
(String, Array<String>) Keywords to be added to the attribute namedattribute_name
. May be a string containing whitespace-delimited values, or an Array of String values. Any values already present will not be added. Any values not present will be added. If the named attribute does not exist, it is created.
- Returns
-
self
(Nokogiri::XML::Node) for ease of chaining method calls.
Example: Ensure that a Node
has “nofollow” in its rel
attribute.
Note that duplicates are not added.
node # => <a></a>
node.kwattr_add("rel", "nofollow") # => <a rel="nofollow"></a>
node.kwattr_add("rel", "nofollow") # => <a rel="nofollow"></a>
Example: Ensure that a Node
has “nofollow” and “noreferrer” in its rel
attribute, via a String argument.
Note that "nofollow" is not added because it is already present. Note also that the
pre-existing duplicate "nofollow" is not removed.
node # => <a rel="nofollow nofollow"></a>
node.kwattr_add("rel", "nofollow noreferrer") # => <a rel="nofollow nofollow noreferrer"></a>
Example: Ensure that a Node
has “nofollow” and “noreferrer” in its rel
attribute, via an Array argument.
node # => <a></a>
node.kwattr_add("rel", ["nofollow", "noreferrer"]) # => <a rel="nofollow noreferrer"></a>
Since v1.11.0
799 800 801 802 803 804 805 |
# File 'lib/nokogiri/xml/node.rb', line 799 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 |
#kwattr_append(attribute_name, keywords) ⇒ Object
:call-seq:
kwattr_append(attribute_name, keywords) → self
Add keywords to a Node’s keyword attribute, regardless of duplication. Compare with #kwattr_add.
A “keyword attribute” is a node attribute that contains a set of space-delimited values. Perhaps the most familiar example of this is the HTML “class” attribute used to contain CSS classes. But other keyword attributes exist, for instance the “rel” attribute.
See also #append_class, #kwattr_values, #kwattr_add, #kwattr_remove
- Parameters
-
attribute_name
(String) The name of the keyword attribute to be modified. -
keywords
(String, Array<String>) Keywords to be added to the attribute namedattribute_name
. May be a string containing whitespace-delimited values, or an Array of String values. All values passed in will be appended to the named attribute even if they are already present in the attribute. If the named attribute does not exist, it is created.
- Returns
-
self
(Node) for ease of chaining method calls.
Example: Append “nofollow” to the rel
attribute.
Note that duplicates are added.
node # => <a></a>
node.kwattr_append("rel", "nofollow") # => <a rel="nofollow"></a>
node.kwattr_append("rel", "nofollow") # => <a rel="nofollow nofollow"></a>
Example: Append “nofollow” and “noreferrer” to the rel
attribute, via a String argument.
Note that “nofollow” is appended even though it is already present.
node # => <a rel="nofollow"></a>
node.kwattr_append("rel", "nofollow noreferrer") # => <a rel="nofollow nofollow noreferrer"></a>
Example: Append “nofollow” and “noreferrer” to the rel
attribute, via an Array argument.
node # => <a></a>
node.kwattr_append("rel", ["nofollow", "noreferrer"]) # => <a rel="nofollow noreferrer"></a>
Since v1.11.0
852 853 854 855 856 857 858 |
# File 'lib/nokogiri/xml/node.rb', line 852 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 |
#kwattr_remove(attribute_name, keywords) ⇒ Object
:call-seq:
kwattr_remove(attribute_name, keywords) → self
Remove keywords from a keyword attribute. Any matching keywords that exist in the named attribute are removed, including any multiple entries.
If no keywords remain after this operation, or if keywords
is nil
, the attribute is deleted from the node.
A “keyword attribute” is a node attribute that contains a set of space-delimited values. Perhaps the most familiar example of this is the HTML “class” attribute used to contain CSS classes. But other keyword attributes exist, for instance the “rel” attribute.
See also #remove_class, #kwattr_values, #kwattr_add, #kwattr_append
- Parameters
-
attribute_name
(String) The name of the keyword attribute to be modified. -
keywords
(String, Array<String>) Keywords to be removed from the attribute namedattribute_name
. May be a string containing whitespace-delimited values, or an Array of String values. Any keywords present in the named attribute will be removed. If no keywords remain, or ifkeywords
is nil, the attribute is deleted.
- Returns
-
self
(Node) for ease of chaining method calls.
Example:
Note that the rel
attribute is deleted when empty.
node # => <a rel="nofollow noreferrer">link</a>
node.kwattr_remove("rel", "nofollow") # => <a rel="noreferrer">link</a>
node.kwattr_remove("rel", "noreferrer") # => <a>link</a>
Since v1.11.0
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 |
# File 'lib/nokogiri/xml/node.rb', line 895 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 |
#kwattr_values(attribute_name) ⇒ Object
:call-seq:
kwattr_values(attribute_name) → Array<String>
Fetch values from a keyword attribute of a Node.
A “keyword attribute” is a node attribute that contains a set of space-delimited values. Perhaps the most familiar example of this is the HTML “class” attribute used to contain CSS classes. But other keyword attributes exist, for instance the “rel” attribute.
See also #classes, #kwattr_add, #kwattr_append, #kwattr_remove
- Parameters
-
attribute_name
(String) The name of the keyword attribute to be inspected.
- Returns
-
(Array<String>) The values present in the Node’s
attribute_name
attribute. If the attribute is empty or non-existent, the return value is an empty array.
Example:
node # => <a rel="nofollow noopener external">link</a>
node.kwattr_values("rel") # => ["nofollow", "noopener", "external"]
Since v1.11.0
745 746 747 |
# File 'lib/nokogiri/xml/node.rb', line 745 def kwattr_values(attribute_name) keywordify(get_attribute(attribute_name) || []) end |
#lang ⇒ Object
Searches the language of a node, i.e. the values of the xml:lang attribute or the one carried by the nearest ancestor.
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 |
# File 'ext/nokogiri/xml_node.c', line 1539
static VALUE
get_lang(VALUE self_rb)
{
xmlNodePtr self ;
xmlChar *lang ;
VALUE lang_rb ;
Data_Get_Struct(self_rb, xmlNode, self);
lang = xmlNodeGetLang(self);
if (lang) {
lang_rb = NOKOGIRI_STR_NEW2(lang);
xmlFree(lang);
return lang_rb ;
}
return Qnil ;
}
|
#lang= ⇒ Object
Set the language of a node, i.e. the values of the xml:lang attribute.
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 |
# File 'ext/nokogiri/xml_node.c', line 1518
static VALUE
set_lang(VALUE self_rb, VALUE lang_rb)
{
xmlNodePtr self ;
xmlChar *lang ;
Data_Get_Struct(self_rb, xmlNode, self);
lang = (xmlChar *)StringValueCStr(lang_rb);
xmlNodeSetLang(self, lang);
return Qnil ;
}
|
#last_element_child ⇒ Object
:call-seq:
last_element_child() → Node
- Returns
-
The last child Node that is an element.
Example:
Note that the “hello” child, which is a Text node, is skipped and the <span>yes</span>
element is returned.
div = Nokogiri::HTML5("<div><span>no</span><span>yes</span>skip</div>").at_css("div")
div.last_element_child
# => #(Element:0x3c { name = "span", children = [ #(Text "yes")] })
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 |
# File 'ext/nokogiri/xml_node.c', line 1181
static VALUE
rb_xml_node_last_element_child(VALUE self)
{
xmlNodePtr node, child;
Data_Get_Struct(self, xmlNode, node);
child = xmlLastElementChild(node);
if (!child) { return Qnil; }
return noko_xml_node_wrap(Qnil, child);
}
|
#line ⇒ Object
:call-seq:
line() → Integer
- Returns
-
The line number of this Node.
⚠ The CRuby and JRuby implementations differ in important ways!
Semantic differences:
-
The CRuby method reflects the node’s line number in the parsed string
-
The JRuby method reflects the node’s line number in the final DOM structure after corrections have been applied
Performance differences:
-
The CRuby method is O(1) (constant time)
-
The JRuby method is O(n) (linear time, where n is the number of nodes before/above the element in the DOM)
If you’d like to help improve the JRuby implementation, please review these issues and reach out to the maintainers:
1727 1728 1729 1730 1731 1732 1733 1734 |
# File 'ext/nokogiri/xml_node.c', line 1727
static VALUE
rb_xml_node_line(VALUE rb_node)
{
xmlNodePtr c_node;
Data_Get_Struct(rb_node, xmlNode, c_node);
return INT2NUM(xmlGetLineNo(c_node));
}
|
#line=(num) ⇒ Object
Sets the line for this Node. num must be less than 65535.
1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 |
# File 'ext/nokogiri/xml_node.c', line 1742
static VALUE
rb_xml_node_line_set(VALUE rb_node, VALUE rb_line_number)
{
xmlNodePtr c_node;
int line_number = NUM2INT(rb_line_number);
Data_Get_Struct(rb_node, xmlNode, c_node);
// libxml2 optionally uses xmlNode.psvi to store longer line numbers, but only for text nodes.
// search for "psvi" in SAX2.c and tree.c to learn more.
if (line_number < 65535) {
c_node->line = (short) line_number;
} else {
c_node->line = 65535;
if (c_node->type == XML_TEXT_NODE) {
c_node->psvi = (void *)(ptrdiff_t) line_number;
}
}
return rb_line_number;
}
|
#matches?(selector) ⇒ Boolean
Returns true if this Node matches selector
922 923 924 |
# File 'lib/nokogiri/xml/node.rb', line 922 def matches?(selector) ancestors.last.search(selector).include?(self) end |
#namespace ⇒ Object
:call-seq:
namespace() → Namespace
- Returns
-
The Namespace of the element or attribute node, or
nil
if there is no namespace.
Example:
doc = Nokogiri::XML(<<~EOF)
<root>
<first/>
<second xmlns="http://example.com/child"/>
<foo:third xmlns:foo="http://example.com/foo"/>
</root>
EOF
doc.at_xpath("//first").namespace
# => nil
doc.at_xpath("//xmlns:second", "xmlns" => "http://example.com/child").namespace
# => #(Namespace:0x3c { href = "http://example.com/child" })
doc.at_xpath("//foo:third", "foo" => "http://example.com/foo").namespace
# => #(Namespace:0x50 { prefix = "foo", href = "http://example.com/foo" })
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 |
# File 'ext/nokogiri/xml_node.c', line 1359
static VALUE
rb_xml_node_namespace(VALUE rb_node)
{
xmlNodePtr c_node ;
Data_Get_Struct(rb_node, xmlNode, c_node);
if (c_node->ns) {
return noko_xml_namespace_wrap(c_node->ns, c_node->doc);
}
return Qnil ;
}
|
#namespace=(ns) ⇒ Object
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.
344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/nokogiri/xml/node.rb', line 344 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 |
#namespace_definitions ⇒ Object
:call-seq:
namespace_definitions() → Array<Nokogiri::XML::Namespace>
- Returns
-
Namespaces that are defined directly on this node, as an Array of Namespace objects. The array will be empty if no namespaces are defined on this node.
Example:
doc = Nokogiri::XML(<<~EOF)
<root xmlns="http://example.com/root">
<first/>
<second xmlns="http://example.com/child" xmlns:unused="http://example.com/unused"/>
<foo:third xmlns:foo="http://example.com/foo"/>
</root>
EOF
doc.at_xpath("//root:first", "root" => "http://example.com/root").namespace_definitions
# => []
doc.at_xpath("//xmlns:second", "xmlns" => "http://example.com/child").namespace_definitions
# => [#(Namespace:0x3c { href = "http://example.com/child" }),
# #(Namespace:0x50 {
# prefix = "unused",
# href = "http://example.com/unused"
# })]
doc.at_xpath("//foo:third", "foo" => "http://example.com/foo").namespace_definitions
# => [#(Namespace:0x64 { prefix = "foo", href = "http://example.com/foo" })]
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 |
# File 'ext/nokogiri/xml_node.c', line 1400
static VALUE
namespace_definitions(VALUE rb_node)
{
/* this code in the mode of xmlHasProp() */
xmlNodePtr c_node ;
xmlNsPtr c_namespace;
VALUE definitions = rb_ary_new();
Data_Get_Struct(rb_node, xmlNode, c_node);
c_namespace = c_node->nsDef;
if (!c_namespace) {
return definitions;
}
while (c_namespace != NULL) {
rb_ary_push(definitions, noko_xml_namespace_wrap(c_namespace, c_node->doc));
c_namespace = c_namespace->next;
}
return definitions;
}
|
#namespace_scopes ⇒ Object
:call-seq:
namespace_scopes() → Array<Nokogiri::XML::Namespace>
- Returns
-
Array of all the Namespaces on this node and its ancestors.
See also #namespaces
Example:
doc = Nokogiri::XML(<<~EOF)
<root xmlns="http://example.com/root" xmlns:bar="http://example.com/bar">
<first/>
<second xmlns="http://example.com/child"/>
<third xmlns:foo="http://example.com/foo"/>
</root>
EOF
doc.at_xpath("//root:first", "root" => "http://example.com/root").namespace_scopes
# => [#(Namespace:0x3c { href = "http://example.com/root" }),
# #(Namespace:0x50 { prefix = "bar", href = "http://example.com/bar" })]
doc.at_xpath("//child:second", "child" => "http://example.com/child").namespace_scopes
# => [#(Namespace:0x64 { href = "http://example.com/child" }),
# #(Namespace:0x50 { prefix = "bar", href = "http://example.com/bar" })]
doc.at_xpath("//root:third", "root" => "http://example.com/root").namespace_scopes
# => [#(Namespace:0x78 { prefix = "foo", href = "http://example.com/foo" }),
# #(Namespace:0x3c { href = "http://example.com/root" }),
# #(Namespace:0x50 { prefix = "bar", href = "http://example.com/bar" })]
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 |
# File 'ext/nokogiri/xml_node.c', line 1451
static VALUE
rb_xml_node_namespace_scopes(VALUE rb_node)
{
xmlNodePtr c_node ;
xmlNsPtr *namespaces;
VALUE scopes = rb_ary_new();
int j;
Data_Get_Struct(rb_node, xmlNode, c_node);
namespaces = xmlGetNsList(c_node->doc, c_node);
if (!namespaces) {
return scopes;
}
for (j = 0 ; namespaces[j] != NULL ; ++j) {
rb_ary_push(scopes, noko_xml_namespace_wrap(namespaces[j], c_node->doc));
}
xmlFree(namespaces);
return scopes;
}
|
#namespaced_key?(attribute, namespace) ⇒ Boolean
Returns true if attribute
is set with namespace
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 |
# File 'ext/nokogiri/xml_node.c', line 1216
static VALUE
namespaced_key_eh(VALUE self, VALUE attribute, VALUE namespace)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
if (xmlHasNsProp(node, (xmlChar *)StringValueCStr(attribute),
NIL_P(namespace) ? NULL : (xmlChar *)StringValueCStr(namespace))) {
return Qtrue;
}
return Qfalse;
}
|
#namespaces ⇒ Object
:call-seq:
namespaces() → Hash<String(Namespace#prefix) ⇒ String(Namespace#href)>
Fetch all the namespaces on this node and its ancestors.
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.
The default namespace for this node will be included with key “xmlns”.
See also #namespace_scopes
- Returns
-
Hash containing all the namespaces on this node and its ancestors. The hash keys are the namespace prefix, and the hash value for each key is the namespace URI.
Example:
doc = Nokogiri::XML(<<~EOF)
<root xmlns="http://example.com/root" xmlns:in_scope="http://example.com/in_scope">
<first/>
<second xmlns="http://example.com/child"/>
<third xmlns:foo="http://example.com/foo"/>
</root>
EOF
doc.at_xpath("//root:first", "root" => "http://example.com/root").namespaces
# => {"xmlns"=>"http://example.com/root",
# "xmlns:in_scope"=>"http://example.com/in_scope"}
doc.at_xpath("//child:second", "child" => "http://example.com/child").namespaces
# => {"xmlns"=>"http://example.com/child",
# "xmlns:in_scope"=>"http://example.com/in_scope"}
doc.at_xpath("//root:third", "root" => "http://example.com/root").namespaces
# => {"xmlns:foo"=>"http://example.com/foo",
# "xmlns"=>"http://example.com/root",
# "xmlns:in_scope"=>"http://example.com/in_scope"}
1023 1024 1025 1026 1027 1028 1029 |
# File 'lib/nokogiri/xml/node.rb', line 1023 def namespaces namespace_scopes.each_with_object({}) do |ns, hash| prefix = ns.prefix key = prefix ? "xmlns:#{prefix}" : "xmlns" hash[key] = ns.href end end |
#content= ⇒ Object
Set the content for this Node
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 |
# File 'ext/nokogiri/xml_node.c', line 1494
static VALUE
set_native_content(VALUE self, VALUE content)
{
xmlNodePtr node, child, next ;
Data_Get_Struct(self, xmlNode, node);
child = node->children;
while (NULL != child) {
next = child->next ;
xmlUnlinkNode(child) ;
noko_xml_document_pin_node(child);
child = next ;
}
xmlNodeSetContent(node, (xmlChar *)StringValueCStr(content));
return content;
}
|
#next_element ⇒ Object
Returns the next Nokogiri::XML::Element type sibling node.
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 |
# File 'ext/nokogiri/xml_node.c', line 1040
static VALUE
next_element(VALUE self)
{
xmlNodePtr node, sibling;
Data_Get_Struct(self, xmlNode, node);
sibling = xmlNextElementSibling(node);
if (!sibling) { return Qnil; }
return noko_xml_node_wrap(Qnil, sibling);
}
|
#next_sibling ⇒ Object Also known as: next
Returns the next sibling node
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 |
# File 'ext/nokogiri/xml_node.c', line 1004
static VALUE
next_sibling(VALUE self)
{
xmlNodePtr node, sibling;
Data_Get_Struct(self, xmlNode, node);
sibling = node->next;
if (!sibling) { return Qnil; }
return noko_xml_node_wrap(Qnil, sibling) ;
}
|
#name ⇒ Object Also known as: name
Returns the name for this Node
1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 |
# File 'ext/nokogiri/xml_node.c', line 1604
static VALUE
get_name(VALUE self)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
if (node->name) {
return NOKOGIRI_STR_NEW2(node->name);
}
return Qnil;
}
|
#name=(new_name) ⇒ Object Also known as: name=
Set the name for this Node
1589 1590 1591 1592 1593 1594 1595 1596 |
# File 'ext/nokogiri/xml_node.c', line 1589
static VALUE
set_name(VALUE self, VALUE new_name)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
xmlNodeSetName(node, (xmlChar *)StringValueCStr(new_name));
return new_name;
}
|
#node_type ⇒ Object Also known as: type
Get the type for this Node
1480 1481 1482 1483 1484 1485 1486 |
# File 'ext/nokogiri/xml_node.c', line 1480
static VALUE
node_type(VALUE self)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
return INT2NUM((long)node->type);
}
|
#parent ⇒ Object
Get the parent Node for this Node
1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 |
# File 'ext/nokogiri/xml_node.c', line 1571
static VALUE
get_parent(VALUE self)
{
xmlNodePtr node, parent;
Data_Get_Struct(self, xmlNode, node);
parent = node->parent;
if (!parent) { return Qnil; }
return noko_xml_node_wrap(Qnil, parent) ;
}
|
#parent=(parent_node) ⇒ Object
Set the parent Node for this Node
324 325 326 |
# File 'lib/nokogiri/xml/node.rb', line 324 def parent=(parent_node) parent_node.add_child(self) end |
#parse(string_or_io, options = nil) {|options| ... } ⇒ Object
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
.
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 |
# File 'lib/nokogiri/xml/node.rb', line 937 def parse(string_or_io, = 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, ) end ||= (document.html? ? ParseOptions::DEFAULT_HTML : ParseOptions::DEFAULT_XML) = Nokogiri::XML::ParseOptions.new() if Integer === yield 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? # libxml2 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. # # 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 # FIXME bug report: https://github.com/sparklemotion/nokogiri/issues/2092 error_count = document.errors.length node_set = in_context(contents, .to_i) if node_set.empty? && (document.errors.length > error_count) if .recover? fragment = document.("DocumentFragment").parse(contents) node_set = fragment.children else raise document.errors[error_count] end end node_set end |
#path ⇒ Object
Returns the path associated with this Node
1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 |
# File 'ext/nokogiri/xml_node.c', line 1621
static VALUE
rb_xml_node_path(VALUE rb_node)
{
xmlNodePtr c_node;
xmlChar *c_path ;
VALUE rval;
Data_Get_Struct(rb_node, xmlNode, c_node);
c_path = xmlGetNodePath(c_node);
if (c_path == NULL) {
// see https://github.com/sparklemotion/nokogiri/issues/2250
// this behavior is clearly undesirable, but is what libxml <= 2.9.10 returned, and so we
// do this for now to preserve the behavior across libxml2 versions.
rval = NOKOGIRI_STR_NEW2("?");
} else {
rval = NOKOGIRI_STR_NEW2(c_path);
xmlFree(c_path);
}
return rval ;
}
|
#pointer_id ⇒ Object
:call-seq: pointer_id() → Integer
- Returns
-
A unique id for this node based on the internal memory structures. This method is used by #== to determine node identity.
779 780 781 782 783 784 785 786 |
# File 'ext/nokogiri/xml_node.c', line 779
static VALUE
rb_xml_node_pointer_id(VALUE self)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
return INT2NUM((long)(node));
}
|
#prepend_child(node_or_tags) ⇒ Object
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
.
162 163 164 165 166 167 168 169 170 171 |
# File 'lib/nokogiri/xml/node.rb', line 162 def prepend_child() if (first = children.first) # Mimic the error add_child would raise. raise "Document already has a root node" if document? && !(.comment? || .processing_instruction?) first.__send__(:add_sibling, :previous, ) else add_child() end end |
#previous_element ⇒ Object
Returns the previous Nokogiri::XML::Element type sibling node.
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 |
# File 'ext/nokogiri/xml_node.c', line 1058
static VALUE
previous_element(VALUE self)
{
xmlNodePtr node, sibling;
Data_Get_Struct(self, xmlNode, node);
/*
* note that we don't use xmlPreviousElementSibling here because it's buggy pre-2.7.7.
*/
sibling = node->prev;
if (!sibling) { return Qnil; }
while (sibling && sibling->type != XML_ELEMENT_NODE) {
sibling = sibling->prev;
}
return sibling ? noko_xml_node_wrap(Qnil, sibling) : Qnil ;
}
|
#previous_sibling ⇒ Object Also known as: previous
Returns the previous sibling node
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 |
# File 'ext/nokogiri/xml_node.c', line 1022
static VALUE
previous_sibling(VALUE self)
{
xmlNodePtr node, sibling;
Data_Get_Struct(self, xmlNode, node);
sibling = node->prev;
if (!sibling) { return Qnil; }
return noko_xml_node_wrap(Qnil, sibling);
}
|
#processing_instruction? ⇒ Boolean
Returns true if this is a ProcessingInstruction node
1057 1058 1059 |
# File 'lib/nokogiri/xml/node.rb', line 1057 def processing_instruction? type == PI_NODE end |
#read_only? ⇒ Boolean
Is this a read only node?
1082 1083 1084 1085 |
# File 'lib/nokogiri/xml/node.rb', line 1082 def read_only? # According to gdome2, these are read-only node types [NOTATION_NODE, ENTITY_NODE, ENTITY_DECL].include?(type) end |
#remove_attribute(name) ⇒ Object Also known as: delete
Remove the attribute named name
550 551 552 553 554 |
# File 'lib/nokogiri/xml/node.rb', line 550 def remove_attribute(name) attr = attributes[name].remove if key?(name) clear_xpath_context if Nokogiri.jruby? attr end |
#remove_class(names = nil) ⇒ Object
:call-seq:
remove_class(css_classes) → self
Remove HTML CSS classes from this node. Any CSS class names in css_classes
that exist in this node’s “class” attribute are removed, including any multiple entries.
If no CSS classes remain after this operation, or if css_classes
is nil
, the “class” attribute is deleted from the node.
This is a convenience function and is equivalent to:
node.kwattr_remove("class", css_classes)
Also see #kwattr_remove, #classes, #add_class, #append_class
- Parameters
-
css_classes
(String, Array<String>)CSS class names to be removed from the Node’s “class” attribute. May be a string containing whitespace-delimited names, or an Array of String names. Any class names already present will be removed. If no CSS classes remain, the “class” attribute is deleted.
- Returns
-
self
(Nokogiri::XML::Node) for ease of chaining method calls.
Example: Deleting a CSS class
Note that all instances of the class “section” are removed from the “class” attribute.
node # => <div class="section header section"></div>
node.remove_class("section") # => <div class="header"></div>
Example: Deleting the only remaining CSS class
Note that the attribute is removed once there are no remaining classes.
node # => <div class="section"></div>
node.remove_class("section") # => <div></div>
Example: Deleting multiple CSS classes
Note that the “class” attribute is deleted once it’s empty.
node # => <div class="section header float"></div>
node.remove_class(["section", "float"]) # => <div class="header"></div>
716 717 718 |
# File 'lib/nokogiri/xml/node.rb', line 716 def remove_class(names = nil) kwattr_remove("class", names) end |
#replace(node_or_tags) ⇒ Object
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
.
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/nokogiri/xml/node.rb', line 279 def replace() 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() end = parent.coerce() if .is_a?(XML::NodeSet) .each { |n| add_previous_sibling(n) } unlink else replace_node() end end |
#serialize(*args, &block) ⇒ Object
Serialize Node using options
. Save options can also be set using a block.
See also Nokogiri::XML::Node::SaveOptions and Node@Serialization+and+Generating+Output.
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
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 |
# File 'lib/nokogiri/xml/node.rb', line 1187 def serialize(*args, &block) = if args.first.is_a?(Hash) args.shift else { encoding: args[0], save_with: args[1], } end encoding = [:encoding] || document.encoding [:encoding] = encoding outstring = +"" outstring.force_encoding(Encoding.find(encoding || "utf-8")) io = StringIO.new(outstring) write_to(io, , &block) io.string end |
#swap(node_or_tags) ⇒ Object
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
.
311 312 313 314 |
# File 'lib/nokogiri/xml/node.rb', line 311 def swap() replace() self end |
#text? ⇒ Boolean
Returns true if this is a Text node
1062 1063 1064 |
# File 'lib/nokogiri/xml/node.rb', line 1062 def text? type == TEXT_NODE end |
#to_html(options = {}) ⇒ Object
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.
1214 1215 1216 |
# File 'lib/nokogiri/xml/node.rb', line 1214 def to_html( = {}) to_format(SaveOptions::DEFAULT_HTML, ) end |
#to_s ⇒ Object
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.
1097 1098 1099 |
# File 'lib/nokogiri/xml/node.rb', line 1097 def to_s document.xml? ? to_xml : to_html end |
#to_xhtml(options = {}) ⇒ Object
Serialize this Node to XHTML using options
doc.to_xhtml(:indent => 5, :encoding => 'UTF-8')
See Node#write_to for a list of options
1235 1236 1237 |
# File 'lib/nokogiri/xml/node.rb', line 1235 def to_xhtml( = {}) to_format(SaveOptions::DEFAULT_XHTML, ) end |
#to_xml(options = {}) ⇒ Object
Serialize this Node to XML using options
doc.to_xml(:indent => 5, :encoding => 'UTF-8')
See Node#write_to for a list of options
1224 1225 1226 1227 |
# File 'lib/nokogiri/xml/node.rb', line 1224 def to_xml( = {}) [:save_with] ||= SaveOptions::DEFAULT_XML serialize() end |
#traverse {|_self| ... } ⇒ Object
Yields self and all children to block
recursively.
1140 1141 1142 1143 |
# File 'lib/nokogiri/xml/node.rb', line 1140 def traverse(&block) children.each { |j| j.traverse(&block) } yield(self) end |
#unlink ⇒ Object Also known as: remove
:call-seq:
unlink() → self
Unlink this node from its current context.
987 988 989 990 991 992 993 994 995 |
# File 'ext/nokogiri/xml_node.c', line 987
static VALUE
unlink_node(VALUE self)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
xmlUnlinkNode(node);
noko_xml_document_pin_node(node);
return self;
}
|
#value?(value) ⇒ Boolean
Does this Node’s attributes include <value>
530 531 532 |
# File 'lib/nokogiri/xml/node.rb', line 530 def value?(value) values.include?(value) end |
#values ⇒ Object
Get the attribute values for this Node.
524 525 526 |
# File 'lib/nokogiri/xml/node.rb', line 524 def values attribute_nodes.map(&:value) end |
#wrap(html) ⇒ Object
Add html around this node
Returns self
177 178 179 180 181 182 |
# File 'lib/nokogiri/xml/node.rb', line 177 def wrap(html) new_parent = document.parse(html).first add_next_sibling(new_parent) new_parent.add_child(self) self end |
#write_html_to(io, options = {}) ⇒ Object
Write Node as HTML to io
with options
See Node#write_to for a list of options
1283 1284 1285 |
# File 'lib/nokogiri/xml/node.rb', line 1283 def write_html_to(io, = {}) write_format_to(SaveOptions::DEFAULT_HTML, io, ) end |
#write_to(io, *options) {|config| ... } ⇒ Object
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)
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 |
# File 'lib/nokogiri/xml/node.rb', line 1256 def write_to(io, *) = .first.is_a?(Hash) ? .shift : {} encoding = [:encoding] || [0] if Nokogiri.jruby? = [:save_with] || [1] indent_times = [:indent] || 0 else = [:save_with] || [1] || SaveOptions::FORMAT indent_times = [:indent] || 2 end indent_text = [: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(.to_i) yield config if block_given? native_write_to(io, encoding, indentation, config.) end |
#write_xhtml_to(io, options = {}) ⇒ Object
Write Node as XHTML to io
with options
See Node#write_to for a list of options
1291 1292 1293 |
# File 'lib/nokogiri/xml/node.rb', line 1291 def write_xhtml_to(io, = {}) write_format_to(SaveOptions::DEFAULT_XHTML, io, ) end |
#write_xml_to(io, options = {}) ⇒ Object
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
1301 1302 1303 1304 |
# File 'lib/nokogiri/xml/node.rb', line 1301 def write_xml_to(io, = {}) [:save_with] ||= SaveOptions::DEFAULT_XML write_to(io, ) end |
#xml? ⇒ Boolean
Returns true if this is an XML::Document node
1042 1043 1044 |
# File 'lib/nokogiri/xml/node.rb', line 1042 def xml? type == DOCUMENT_NODE end |