Class: Nokogiri::XML::Node
- Inherits:
-
Object
- Object
- Nokogiri::XML::Node
- Includes:
- Enumerable, PP::Node, Searchable
- Defined in:
- lib/nokogiri/xml/node.rb,
lib/nokogiri/xml/node/save_options.rb,
ext/nokogiri/xml_dtd.c,
ext/nokogiri/xml_attr.c,
ext/nokogiri/xml_node.c,
ext/nokogiri/xml_text.c,
ext/nokogiri/xml_cdata.c,
ext/nokogiri/xml_comment.c,
ext/nokogiri/xml_document.c,
ext/nokogiri/html_document.c,
ext/nokogiri/xml_entity_decl.c,
ext/nokogiri/xml_element_decl.c,
ext/nokogiri/xml_attribute_decl.c,
ext/nokogiri/xml_entity_reference.c,
ext/nokogiri/xml_document_fragment.c,
ext/nokogiri/xml_processing_instruction.c
Overview
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:
-
Nokogiri::XML::Node#parent
-
Nokogiri::XML::Node#children
-
Nokogiri::XML::Node#next
-
Nokogiri::XML::Node#previous
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: emit plaintext
These methods will all emit the plaintext version of your document, 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: emit well-formed markup
These methods will all emit properly-escaped markup, meaning that it’s suitable for consumption by browsers, parsers, etc.
You may search this node’s subtree using Searchable#xpath and Searchable#css
Direct Known Subclasses
Attr, AttributeDecl, CharacterData, DTD, Document, DocumentFragment, Element, 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 Searchable
Searching via XPath or CSS Queries collapse
-
#>(selector) ⇒ Object
Search this node’s immediate children using CSS selector
selector
.
Manipulating Document Structure collapse
-
#<<(node_or_tags) ⇒ Object
Add
node_or_tags
as a child of this Node. -
#add_child(node_or_tags) ⇒ Object
Add
node_or_tags
as a child of this Node. -
#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). -
#before(node_or_tags) ⇒ Object
Insert
node_or_tags
before this node (as a sibling). -
#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. -
#content=(string) ⇒ Object
Set the Node’s content to a Text node containing
string
. -
#default_namespace=(url) ⇒ Object
Adds a default namespace supplied as a string
url
href, to self. -
#do_xinclude(options = XML::ParseOptions::DEFAULT_XML) {|options| ... } ⇒ Object
Do xinclude substitution on the subtree below node.
-
#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. -
#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
. -
#parent=(parent_node) ⇒ Object
Set the parent Node for this Node.
-
#prepend_child(node_or_tags) ⇒ Object
Add
node_or_tags
as the first child of this Node. -
#replace(node_or_tags) ⇒ Object
Replace this Node with
node_or_tags
. -
#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. -
#wrap(html) ⇒ Object
Add html around this node.
Working With Node Attributes collapse
-
#[](name) ⇒ Object
(also: #get_attribute, #attr)
Get the attribute value for the attribute
name
. -
#[]=(name, value) ⇒ Object
(also: #set_attribute)
Set the attribute value for the attribute
name
tovalue
. -
#add_class(names) ⇒ Node
Ensure HTML CSS classes are present on a
Node
. -
#append_class(names) ⇒ Node
Add HTML CSS classes to a
Node
, regardless of duplication. -
#attributes ⇒ Object
Returns a hash containing the node’s attributes.
-
#classes ⇒ Array<String>
Get the CSS class names of a Node.
-
#each ⇒ Object
Iterate over each attribute name and value pair for this Node.
-
#keys ⇒ Object
Get the attribute names for this Node.
-
#kwattr_add(attribute_name, keywords) ⇒ Node
Ensure that values are present in a keyword attribute.
-
#kwattr_append(attribute_name, keywords) ⇒ Node
Add keywords to a Node’s keyword attribute, regardless of duplication.
-
#kwattr_remove(attribute_name, keywords) ⇒ Node
Remove keywords from a keyword attribute.
-
#kwattr_values(attribute_name) ⇒ Array<String>
Retrieve values from a keyword attribute of a Node.
-
#remove_attribute(name) ⇒ Object
(also: #delete)
Remove the attribute named
name
. -
#remove_class(names = nil) ⇒ Node
Remove HTML CSS classes from a
Node
. -
#value?(value) ⇒ Boolean
Does this Node’s attributes include <value>.
-
#values ⇒ Object
Get the attribute values for this Node.
Serialization and Generating Output collapse
- #canonicalize(mode = XML::XML_C14N_1_0, inclusive_namespaces = nil, with_comments = false) ⇒ Object
-
#serialize(*args, &block) ⇒ Object
Serialize Node using
options
. -
#to_html(options = {}) ⇒ Object
Serialize this Node to HTML.
-
#to_xhtml(options = {}) ⇒ Object
Serialize this Node to XHTML using
options
. -
#to_xml(options = {}) ⇒ Object
Serialize this Node to XML using
options
. -
#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
.
Class Method Summary collapse
-
.new(name, document) ⇒ Object
Create a new node with
name
sharing GC lifecycle withdocument
.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compare two Node objects with respect to their Document.
-
#==(other) ⇒ Object
Test to see if this Node is equal to
other
. -
#accept(visitor) ⇒ Object
Accept a visitor.
-
#add_namespace_definition(prefix, href) ⇒ Object
(also: #add_namespace)
Adds a namespace definition with
prefix
usinghref
value. -
#ancestors(selector = nil) ⇒ Object
Get a list of ancestor Node for this Node.
-
#attribute(name) ⇒ Object
Get the attribute node with
name
. -
#attribute_nodes ⇒ Object
returns a list containing the Node attributes.
-
#attribute_with_ns(name, namespace) ⇒ Object
Get the attribute node with
name
andnamespace
. -
#blank? ⇒ Boolean
Is this node blank?.
-
#cdata? ⇒ Boolean
Returns true if this is a CDATA.
-
#child ⇒ Object
Returns the child node.
-
#children ⇒ Object
Get the list of children for this node as a NodeSet.
-
#comment? ⇒ Boolean
Returns true if this is a Comment.
-
#content ⇒ Object
(also: #text, #inner_text)
Returns the plaintext content for this Node.
-
#create_external_subset(name, external_id, system_id) ⇒ Object
Create an external subset.
-
#create_internal_subset(name, external_id, system_id) ⇒ Object
Create the internal subset of a document.
-
#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.
-
#description ⇒ Object
Fetch the Nokogiri::HTML::ElementDescription for this node.
-
#document ⇒ Object
Get the document for this Node.
-
#document? ⇒ Boolean
Returns true if this is a Document.
-
#dup(*args) ⇒ Object
(also: #clone)
Copy this node.
-
#element? ⇒ Boolean
(also: #elem?)
Returns true if this is an Element node.
-
#element_children ⇒ Object
(also: #elements)
Get the list of children for this node as a NodeSet.
-
#encode_special_chars(string) ⇒ Object
Encode any special characters in
string
. -
#external_subset ⇒ Object
Get the external subset.
-
#first_element_child ⇒ Object
Returns the first child node of this node that is an element.
-
#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 HTML::Document node.
-
#initialize(name, document) ⇒ Node
constructor
:nodoc:.
-
#inner_html(*args) ⇒ Object
Get the inner_html for this node’s Node#children.
-
#internal_subset ⇒ Object
Get the internal subset.
-
#key?(attribute) ⇒ Boolean
(also: #has_attribute?)
Returns true if
attribute
is set. -
#lang ⇒ Object
Searches the language of a node, i.e.
-
#lang= ⇒ Object
Set the language of a node, i.e.
-
#last_element_child ⇒ Object
Returns the last child node of this node that is an element.
-
#line ⇒ Object
Returns the line for this Node.
-
#line=(num) ⇒ Object
Sets the line for this Node.
-
#matches?(selector) ⇒ Boolean
Returns true if this Node matches
selector
. -
#namespace ⇒ Object
returns the namespace of the element or attribute node as a Namespace object, or nil if there is no namespace for the element or attribute.
-
#namespace_definitions ⇒ Object
returns namespaces defined on self element directly, as an array of Namespace objects.
-
#namespace_scopes ⇒ Object
returns namespaces in scope for self – those defined on self element directly or any ancestor node – as an array of Namespace objects.
-
#namespaced_key?(attribute, namespace) ⇒ Boolean
Returns true if
attribute
is set withnamespace
. -
#namespaces ⇒ Object
Returns a Hash of {prefix => value} for all namespaces on this node and its ancestors.
-
#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.
-
#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
Get the internal pointer number.
-
#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?.
-
#text? ⇒ Boolean
Returns true if this is a Text node.
-
#to_s ⇒ Object
Turn this node in to a string.
-
#traverse(&block) ⇒ Object
Yields self and all children to
block
recursively. -
#unlink ⇒ Object
(also: #remove)
Unlink this node from its current context.
-
#xml? ⇒ Boolean
Returns true if this is an XML::Document node.
Methods included from Searchable
#at, #at_css, #at_xpath, #css, #search, #xpath
Methods included from PP::Node
Constructor Details
#initialize(name, document) ⇒ Node
:nodoc:
102 103 104 |
# File 'lib/nokogiri/xml/node.rb', line 102 def initialize(name, document) # :nodoc: # ... Ya. This is empty on purpose. end |
Class Method Details
.new(name, document) ⇒ Object
Create a new node with name
sharing GC lifecycle with document
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 |
# File 'ext/nokogiri/xml_node.c', line 1403
static VALUE new(int argc, VALUE *argv, VALUE klass)
{
xmlDocPtr doc;
xmlNodePtr node;
VALUE name;
VALUE document;
VALUE rest;
VALUE rb_node;
rb_scan_args(argc, argv, "2*", &name, &document, &rest);
Data_Get_Struct(document, xmlDoc, doc);
node = xmlNewNode(NULL, (xmlChar *)StringValueCStr(name));
node->doc = doc->doc;
nokogiri_root_node(node);
rb_node = Nokogiri_wrap_xml_node(
klass == cNokogiriXmlNode ? (VALUE)NULL : klass,
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
.
177 178 179 180 |
# File 'lib/nokogiri/xml/node.rb', line 177 def <<() add_child self end |
#<=>(other) ⇒ Object
Compare two Node objects with respect to their Document. Nodes from different documents cannot be compared.
1003 1004 1005 1006 1007 |
# File 'lib/nokogiri/xml/node.rb', line 1003 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
994 995 996 997 998 |
# File 'lib/nokogiri/xml/node.rb', line 994 def ==(other) return false unless other return false unless other.respond_to?(:pointer_id) pointer_id == other.pointer_id end |
#>(selector) ⇒ Object
Search this node’s immediate children using CSS selector selector
116 117 118 119 |
# File 'lib/nokogiri/xml/node.rb', line 116 def >(selector) ns = document.root.namespaces xpath CSS.xpath_for(selector, :prefix => "./", :ns => ns).first end |
#[](name) ⇒ Object Also known as: get_attribute, attr
Get the attribute value for the attribute name
384 385 386 |
# File 'lib/nokogiri/xml/node.rb', line 384 def [](name) get(name.to_s) end |
#[]=(name, value) ⇒ Object Also known as: set_attribute
Set the attribute value for the attribute name
to value
390 391 392 |
# File 'lib/nokogiri/xml/node.rb', line 390 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.
988 989 990 |
# File 'lib/nokogiri/xml/node.rb', line 988 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 <<.
132 133 134 135 136 137 138 139 140 |
# File 'lib/nokogiri/xml/node.rb', line 132 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) ⇒ Node
Ensure HTML CSS classes are present on a Node
. Any CSS classes in names
that already exist in the Node
‘s 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)
503 504 505 |
# File 'lib/nokogiri/xml/node.rb', line 503 def add_class(names) kwattr_add("class", names) end |
#add_namespace_definition(prefix, href) ⇒ Object Also known as: add_namespace
Adds a namespace definition with prefix
using href
value. The result is as if parsed XML for this node had included an attribute ‘xmlns:prefix=value’. A default namespace for this node (“xmlns=”) can be added by passing ‘nil’ for prefix. Namespaces added this way will not show up in #attributes, but they will be included as an xmlns attribute when the node is serialized to XML.
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 |
# File 'ext/nokogiri/xml_node.c', line 1365
static VALUE add_namespace_definition(VALUE self, VALUE prefix, VALUE href)
{
xmlNodePtr node, namespace;
xmlNsPtr ns;
Data_Get_Struct(self, xmlNode, node);
namespace = node ;
ns = xmlSearchNs(
node->doc,
node,
(const xmlChar *)(NIL_P(prefix) ? NULL : StringValueCStr(prefix))
);
if(!ns) {
if (node->type != XML_ELEMENT_NODE) {
namespace = node->parent;
}
ns = xmlNewNs(
namespace,
(const xmlChar *)StringValueCStr(href),
(const xmlChar *)(NIL_P(prefix) ? NULL : StringValueCStr(prefix))
);
}
if (!ns) { return Qnil ; }
if(NIL_P(prefix) || node != namespace) { xmlSetNs(node, ns); }
return Nokogiri_wrap_xml_namespace(node->doc, ns);
}
|
#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
.
202 203 204 205 206 |
# File 'lib/nokogiri/xml/node.rb', line 202 def add_next_sibling() raise ArgumentError.new("A document may not have multiple root nodes.") if (parent && 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
.
189 190 191 192 193 |
# File 'lib/nokogiri/xml/node.rb', line 189 def add_previous_sibling() raise ArgumentError.new("A document may not have multiple root nodes.") if (parent && 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
.
227 228 229 230 |
# File 'lib/nokogiri/xml/node.rb', line 227 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
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 |
# File 'lib/nokogiri/xml/node.rb', line 958 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 { |parent| search_results.include?(parent) }) end |
#append_class(names) ⇒ Node
Add HTML CSS classes to a Node
, regardless of duplication. Compare with #add_class.
This is a convenience function and is equivalent to:
node.kwattr_append("class", names)
544 545 546 |
# File 'lib/nokogiri/xml/node.rb', line 544 def append_class(names) kwattr_append("class", names) end |
#attribute(name) ⇒ Object
Get the attribute node with name
963 964 965 966 967 968 969 970 971 972 |
# File 'ext/nokogiri/xml_node.c', line 963
static VALUE attr(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 Nokogiri_wrap_xml_node(Qnil, (xmlNodePtr)prop);
}
|
#attribute_nodes ⇒ Object
returns a list containing the Node attributes.
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 |
# File 'ext/nokogiri/xml_node.c', line 998
static VALUE attribute_nodes(VALUE self)
{
/* this code in the mode of xmlHasProp() */
xmlNodePtr node;
VALUE attr;
Data_Get_Struct(self, xmlNode, node);
attr = rb_ary_new();
Nokogiri_xml_node_properties(node, attr);
return attr ;
}
|
#attribute_with_ns(name, namespace) ⇒ Object
Get the attribute node with name
and namespace
980 981 982 983 984 985 986 987 988 989 990 |
# File 'ext/nokogiri/xml_node.c', line 980
static VALUE 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 Nokogiri_wrap_xml_node(Qnil, (xmlNodePtr)prop);
}
|
#attributes ⇒ Object
Returns a hash containing the node’s attributes. The key is the attribute name without any namespace, the value is a Nokogiri::XML::Attr representing the attribute. If you need to distinguish attributes with the same name, with different namespaces use #attribute_nodes instead.
400 401 402 403 404 |
# File 'lib/nokogiri/xml/node.rb', line 400 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
.
215 216 217 218 |
# File 'lib/nokogiri/xml/node.rb', line 215 def before() add_previous_sibling self end |
#blank? ⇒ Boolean
Is this node blank?
592 593 594 595 596 597 |
# File 'ext/nokogiri/xml_node.c', line 592
static VALUE 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
1140 1141 1142 1143 1144 1145 1146 |
# File 'lib/nokogiri/xml/node.rb', line 1140 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
880 881 882 |
# File 'lib/nokogiri/xml/node.rb', line 880 def cdata? type == CDATA_SECTION_NODE end |
#child ⇒ Object
Returns the child node
765 766 767 768 769 770 771 772 773 774 |
# File 'ext/nokogiri/xml_node.c', line 765
static VALUE child(VALUE self)
{
xmlNodePtr node, child;
Data_Get_Struct(self, xmlNode, node);
child = node->children;
if(!child) { return Qnil; }
return Nokogiri_wrap_xml_node(Qnil, child);
}
|
#children ⇒ Object
Get the list of children for this node as a NodeSet
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 |
# File 'ext/nokogiri/xml_node.c', line 692
static VALUE 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 Nokogiri_wrap_xml_node_set(set, document); }
child = child->next;
while(NULL != child) {
xmlXPathNodeSetAddUnique(set, child);
child = child->next;
}
node_set = Nokogiri_wrap_xml_node_set(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.
Returns the reparented node (if node_or_tags
is a Node), or NodeSet (if node_or_tags
is a DocumentFragment, NodeSet, or string).
Also see related method inner_html=
251 252 253 254 255 256 257 258 259 260 |
# File 'lib/nokogiri/xml/node.rb', line 251 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 ⇒ Array<String>
Get the CSS class names of a Node.
This is a convenience function and is equivalent to:
node.kwattr_values("class")
460 461 462 |
# File 'lib/nokogiri/xml/node.rb', line 460 def classes kwattr_values("class") end |
#comment? ⇒ Boolean
Returns true if this is a Comment
875 876 877 |
# File 'lib/nokogiri/xml/node.rb', line 875 def comment? type == COMMENT_NODE end |
#content ⇒ Object Also known as: text, inner_text
Returns the plaintext content for this Node. Note that entities will always be expanded in the returned string.
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 |
# File 'ext/nokogiri/xml_node.c', line 1135
static VALUE get_native_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.
308 309 310 |
# File 'lib/nokogiri/xml/node.rb', line 308 def content=(string) self.native_content = encode_special_chars(string.to_s) end |
#create_external_subset(name, external_id, system_id) ⇒ Object
Create an external subset
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 |
# File 'ext/nokogiri/xml_node.c', line 454
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 Nokogiri_wrap_xml_node(Qnil, (xmlNodePtr)dtd);
}
|
#create_internal_subset(name, external_id, system_id) ⇒ Object
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">
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 |
# File 'ext/nokogiri/xml_node.c', line 422
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 Nokogiri_wrap_xml_node(Qnil, (xmlNodePtr)dtd);
}
|
#css_path ⇒ Object
Get the path to this node as a CSS expression
949 950 951 952 953 |
# File 'lib/nokogiri/xml/node.rb', line 949 def css_path path.split(/\//).map { |part| part.length == 0 ? nil : part.gsub(/\[(\d+)\]/, ':nth-of-type(\1)') }.compact.join(" > ") end |
#decorate! ⇒ Object
Decorate this node with the decorators set up in this node’s Document
108 109 110 |
# File 'lib/nokogiri/xml/node.rb', line 108 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=
325 326 327 |
# File 'lib/nokogiri/xml/node.rb', line 325 def default_namespace=(url) add_namespace_definition(nil, url) end |
#description ⇒ Object
Fetch the Nokogiri::HTML::ElementDescription for this node. Returns nil on XML documents and on unknown tags.
917 918 919 920 |
# File 'lib/nokogiri/xml/node.rb', line 917 def description return nil if document.xml? Nokogiri::HTML::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.
352 353 354 355 356 357 358 359 360 |
# File 'lib/nokogiri/xml/node.rb', line 352 def do_xinclude( = XML::ParseOptions::DEFAULT_XML) = Nokogiri::XML::ParseOptions.new() if Integer === # give options to user yield if block_given? # call c extension process_xincludes(.to_i) end |
#document ⇒ Object
Get the document for this Node
365 366 367 368 369 370 |
# File 'ext/nokogiri/xml_node.c', line 365
static VALUE 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
895 896 897 |
# File 'lib/nokogiri/xml/node.rb', line 895 def document? is_a? XML::Document end |
#dup ⇒ Object #dup(depth) ⇒ Object #dup(depth, new_parent_doc) ⇒ Object Also known as: clone
Copy this node. An optional depth may be passed in. 0 is a shallow copy, 1 (the default) is a deep copy. An optional new_parent_doc may also be passed in, which will be the new node’s parent document. Defaults to the current node’s document. current document.
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 |
# File 'ext/nokogiri/xml_node.c', line 540
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; }
nokogiri_root_node(dup);
return Nokogiri_wrap_xml_node(rb_obj_class(self), dup);
}
|
#each ⇒ Object
Iterate over each attribute name and value pair for this Node.
426 427 428 429 430 |
# File 'lib/nokogiri/xml/node.rb', line 426 def each attribute_nodes.each { |node| yield [node.node_name, node.value] } end |
#element? ⇒ Boolean Also known as: elem?
Returns true if this is an Element node
930 931 932 |
# File 'lib/nokogiri/xml/node.rb', line 930 def element? type == ELEMENT_NODE end |
#element_children ⇒ Object Also known as: elements
Get the list of children for this node as a NodeSet. All nodes will be element nodes.
Example:
@doc.root.element_children.all? { |x| x.element? } # => true
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 |
# File 'ext/nokogiri/xml_node.c', line 731
static VALUE 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 Nokogiri_wrap_xml_node_set(set, document); }
child = xmlNextElementSibling(child);
while(NULL != child) {
xmlXPathNodeSetAddUnique(set, child);
child = xmlNextElementSibling(child);
}
node_set = Nokogiri_wrap_xml_node_set(set, document);
return node_set;
}
|
#encode_special_chars(string) ⇒ Object
Encode any special characters in string
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'ext/nokogiri/xml_node.c', line 392
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
Get the external subset
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 |
# File 'ext/nokogiri/xml_node.c', line 486
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 Nokogiri_wrap_xml_node(Qnil, (xmlNodePtr)dtd);
}
|
#first_element_child ⇒ Object
Returns the first child node of this node that is an element.
Example:
@doc.root.first_element_child.element? # => true
786 787 788 789 790 791 792 793 794 795 |
# File 'ext/nokogiri/xml_node.c', line 786
static VALUE first_element_child(VALUE self)
{
xmlNodePtr node, child;
Data_Get_Struct(self, xmlNode, node);
child = xmlFirstElementChild(node);
if(!child) { return Qnil; }
return Nokogiri_wrap_xml_node(Qnil, child);
}
|
#fragment(tags) ⇒ Object
Create a DocumentFragment containing tags
that is relative to this context node.
792 793 794 795 |
# File 'lib/nokogiri/xml/node.rb', line 792 def fragment() type = document.html? ? Nokogiri::HTML : Nokogiri::XML type::DocumentFragment.new(document, , self) end |
#fragment? ⇒ Boolean
Returns true if this is a DocumentFragment
910 911 912 |
# File 'lib/nokogiri/xml/node.rb', line 910 def fragment? type == DOCUMENT_FRAG_NODE end |
#html? ⇒ Boolean
Returns true if this is an HTML::Document node
890 891 892 |
# File 'lib/nokogiri/xml/node.rb', line 890 def html? type == HTML_DOCUMENT_NODE end |
#inner_html(*args) ⇒ Object
Get the inner_html for this node’s Node#children
944 945 946 |
# File 'lib/nokogiri/xml/node.rb', line 944 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.
Returns self.
Also see related method children=
239 240 241 242 |
# File 'lib/nokogiri/xml/node.rb', line 239 def inner_html=() self.children = self end |
#internal_subset ⇒ Object
Get the internal subset
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
# File 'ext/nokogiri/xml_node.c', line 510
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 Nokogiri_wrap_xml_node(Qnil, (xmlNodePtr)dtd);
}
|
#key?(attribute) ⇒ Boolean Also known as: has_attribute?
Returns true if attribute
is set
824 825 826 827 828 829 830 831 832 |
# File 'ext/nokogiri/xml_node.c', line 824
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.
420 421 422 |
# File 'lib/nokogiri/xml/node.rb', line 420 def keys attribute_nodes.map(&:node_name) end |
#kwattr_add(attribute_name, keywords) ⇒ Node
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 [‘rel`](developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel).
660 661 662 663 664 665 666 |
# File 'lib/nokogiri/xml/node.rb', line 660 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) ⇒ Node
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 [‘rel`](developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel).
712 713 714 715 716 717 718 |
# File 'lib/nokogiri/xml/node.rb', line 712 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) ⇒ Node
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 [‘rel`](developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel).
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 |
# File 'lib/nokogiri/xml/node.rb', line 758 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) ⇒ Array<String>
Retrieve 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 [‘rel`](developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel).
608 609 610 |
# File 'lib/nokogiri/xml/node.rb', line 608 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.
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 |
# File 'ext/nokogiri/xml_node.c', line 1177
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.
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 |
# File 'ext/nokogiri/xml_node.c', line 1157
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
Returns the last child node of this node that is an element.
Example:
@doc.root.last_element_child.element? # => true
807 808 809 810 811 812 813 814 815 816 |
# File 'ext/nokogiri/xml_node.c', line 807
static VALUE last_element_child(VALUE self)
{
xmlNodePtr node, child;
Data_Get_Struct(self, xmlNode, node);
child = xmlLastElementChild(node);
if(!child) { return Qnil; }
return Nokogiri_wrap_xml_node(Qnil, child);
}
|
#line ⇒ Object
Returns the line for this Node
1327 1328 1329 1330 1331 1332 1333 |
# File 'ext/nokogiri/xml_node.c', line 1327
static VALUE line(VALUE self)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
return INT2NUM(xmlGetLineNo(node));
}
|
#line=(num) ⇒ Object
Sets the line for this Node. num must be less than 65535.
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 |
# File 'ext/nokogiri/xml_node.c', line 1341
static VALUE set_line(VALUE self, VALUE num)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
int value = NUM2INT(num);
if (value < 65535) {
node->line = value;
}
return num;
}
|
#matches?(selector) ⇒ Boolean
Returns true if this Node matches selector
785 786 787 |
# File 'lib/nokogiri/xml/node.rb', line 785 def matches?(selector) ancestors.last.search(selector).include?(self) end |
#namespace ⇒ Object
returns the namespace of the element or attribute node as a Namespace object, or nil if there is no namespace for the element or attribute.
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 |
# File 'ext/nokogiri/xml_node.c', line 1020
static VALUE namespace(VALUE self)
{
xmlNodePtr node ;
Data_Get_Struct(self, xmlNode, node);
if (node->ns) {
return Nokogiri_wrap_xml_namespace(node->doc, node->ns);
}
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.
335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/nokogiri/xml/node.rb', line 335 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
returns namespaces defined on self element directly, as an array of Namespace objects. Includes both a default namespace (as in“xmlns=”), and prefixed namespaces (as in “xmlns:prefix=”).
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 |
# File 'ext/nokogiri/xml_node.c', line 1038
static VALUE namespace_definitions(VALUE self)
{
/* this code in the mode of xmlHasProp() */
xmlNodePtr node ;
VALUE list;
xmlNsPtr ns;
Data_Get_Struct(self, xmlNode, node);
list = rb_ary_new();
ns = node->nsDef;
if(!ns) { return list; }
while(NULL != ns) {
rb_ary_push(list, Nokogiri_wrap_xml_namespace(node->doc, ns));
ns = ns->next;
}
return list;
}
|
#namespace_scopes ⇒ Object
returns namespaces in scope for self – those defined on self element directly or any ancestor node – as an array of Namespace objects. Default namespaces (“xmlns=” style) for self are included in this array; Default namespaces for ancestors, however, are not. See also #namespaces
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 |
# File 'ext/nokogiri/xml_node.c', line 1070
static VALUE namespace_scopes(VALUE self)
{
xmlNodePtr node ;
VALUE list;
xmlNsPtr *ns_list;
int j;
Data_Get_Struct(self, xmlNode, node);
list = rb_ary_new();
ns_list = xmlGetNsList(node->doc, node);
if(!ns_list) { return list; }
for (j = 0 ; ns_list[j] != NULL ; ++j) {
rb_ary_push(list, Nokogiri_wrap_xml_namespace(node->doc, ns_list[j]));
}
xmlFree(ns_list);
return list;
}
|
#namespaced_key?(attribute, namespace) ⇒ Boolean
Returns true if attribute
is set with namespace
840 841 842 843 844 845 846 847 848 849 |
# File 'ext/nokogiri/xml_node.c', line 840
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
Returns a Hash of {prefix => value} for all namespaces on this node and its ancestors.
This method returns the same namespaces as #namespace_scopes.
Returns namespaces in scope for self – those defined on self element directly or any ancestor node – as a Hash of attribute-name/value pairs. Note that the keys in this hash XML attributes that would be used to define this namespace, such as “xmlns:prefix”, not just the prefix. Default namespace set on self will be included with key “xmlns”. However, default namespaces set on ancestor will NOT be, even if self has no explicit default namespace.
866 867 868 869 870 871 872 |
# File 'lib/nokogiri/xml/node.rb', line 866 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
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 |
# File 'ext/nokogiri/xml_node.c', line 1111
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) ;
nokogiri_root_node(child);
child = next ;
}
xmlNodeSetContent(node, (xmlChar *)StringValueCStr(content));
return content;
}
|
#next_element ⇒ Object
Returns the next Nokogiri::XML::Element type sibling node.
639 640 641 642 643 644 645 646 647 648 |
# File 'ext/nokogiri/xml_node.c', line 639
static VALUE next_element(VALUE self)
{
xmlNodePtr node, sibling;
Data_Get_Struct(self, xmlNode, node);
sibling = xmlNextElementSibling(node);
if(!sibling) { return Qnil; }
return Nokogiri_wrap_xml_node(Qnil, sibling);
}
|
#next_sibling ⇒ Object Also known as: next
Returns the next sibling node
605 606 607 608 609 610 611 612 613 614 |
# File 'ext/nokogiri/xml_node.c', line 605
static VALUE next_sibling(VALUE self)
{
xmlNodePtr node, sibling;
Data_Get_Struct(self, xmlNode, node);
sibling = node->next;
if(!sibling) { return Qnil; }
return Nokogiri_wrap_xml_node(Qnil, sibling) ;
}
|
#name ⇒ Object Also known as: name
Returns the name for this Node
1238 1239 1240 1241 1242 1243 1244 1245 1246 |
# File 'ext/nokogiri/xml_node.c', line 1238
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
1224 1225 1226 1227 1228 1229 1230 |
# File 'ext/nokogiri/xml_node.c', line 1224
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
1098 1099 1100 1101 1102 1103 |
# File 'ext/nokogiri/xml_node.c', line 1098
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
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 |
# File 'ext/nokogiri/xml_node.c', line 1207
static VALUE get_parent(VALUE self)
{
xmlNodePtr node, parent;
Data_Get_Struct(self, xmlNode, node);
parent = node->parent;
if(!parent) { return Qnil; }
return Nokogiri_wrap_xml_node(Qnil, parent) ;
}
|
#parent=(parent_node) ⇒ Object
Set the parent Node for this Node
314 315 316 317 |
# File 'lib/nokogiri/xml/node.rb', line 314 def parent=(parent_node) parent_node.add_child(self) parent_node 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
.
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 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 850 |
# File 'lib/nokogiri/xml/node.rb', line 801 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) if Integer === = Nokogiri::XML::ParseOptions.new() end # Give the options to the user yield if block_given? contents = string_or_io.respond_to?(:read) ? string_or_io.read : string_or_io 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 = Nokogiri::HTML::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
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 |
# File 'ext/nokogiri/xml_node.c', line 1254
static VALUE path(VALUE self)
{
xmlNodePtr node;
xmlChar *path ;
VALUE rval;
Data_Get_Struct(self, xmlNode, node);
path = xmlGetNodePath(node);
rval = NOKOGIRI_STR_NEW2(path);
xmlFree(path);
return rval ;
}
|
#pointer_id ⇒ Object
Get the internal pointer number
378 379 380 381 382 383 384 |
# File 'ext/nokogiri/xml_node.c', line 378
static VALUE 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
.
149 150 151 152 153 154 155 156 157 |
# File 'lib/nokogiri/xml/node.rb', line 149 def prepend_child() if first = children.first # Mimic the error add_child would raise. raise RuntimeError, "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.
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 |
# File 'ext/nokogiri/xml_node.c', line 656
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 ? Nokogiri_wrap_xml_node(Qnil, sibling) : Qnil ;
}
|
#previous_sibling ⇒ Object Also known as: previous
Returns the previous sibling node
622 623 624 625 626 627 628 629 630 631 |
# File 'ext/nokogiri/xml_node.c', line 622
static VALUE previous_sibling(VALUE self)
{
xmlNodePtr node, sibling;
Data_Get_Struct(self, xmlNode, node);
sibling = node->prev;
if(!sibling) { return Qnil; }
return Nokogiri_wrap_xml_node(Qnil, sibling);
}
|
#processing_instruction? ⇒ Boolean
Returns true if this is a ProcessingInstruction node
900 901 902 |
# File 'lib/nokogiri/xml/node.rb', line 900 def processing_instruction? type == PI_NODE end |
#read_only? ⇒ Boolean
Is this a read only node?
924 925 926 927 |
# File 'lib/nokogiri/xml/node.rb', line 924 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
434 435 436 437 438 |
# File 'lib/nokogiri/xml/node.rb', line 434 def remove_attribute(name) attr = attributes[name].remove if key? name clear_xpath_context if Nokogiri.jruby? attr end |
#remove_class(names = nil) ⇒ Node
Remove HTML CSS classes from a Node
. Any CSS classes in names
that exist in the Node
‘s class
attribute are removed, including any multiple entries.
If no CSS classes remain after this operation, or if names
is nil
, the class
attribute is deleted from the node.
This is a convenience function and is equivalent to:
node.kwattr_remove("class", names)
577 578 579 |
# File 'lib/nokogiri/xml/node.rb', line 577 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
.
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/nokogiri/xml/node.rb', line 269 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 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
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 |
# File 'lib/nokogiri/xml/node.rb', line 1025 def serialize(*args, &block) = args.first.is_a?(Hash) ? args.shift : { :encoding => args[0], :save_with => args[1], } encoding = [:encoding] || document.encoding [:encoding] = encoding outstring = String.new 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
.
301 302 303 304 |
# File 'lib/nokogiri/xml/node.rb', line 301 def swap() replace self end |
#text? ⇒ Boolean
Returns true if this is a Text node
905 906 907 |
# File 'lib/nokogiri/xml/node.rb', line 905 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.
1048 1049 1050 |
# File 'lib/nokogiri/xml/node.rb', line 1048 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.
939 940 941 |
# File 'lib/nokogiri/xml/node.rb', line 939 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
1069 1070 1071 |
# File 'lib/nokogiri/xml/node.rb', line 1069 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
1058 1059 1060 1061 |
# File 'lib/nokogiri/xml/node.rb', line 1058 def to_xml( = {}) [:save_with] ||= SaveOptions::DEFAULT_XML serialize() end |
#traverse(&block) ⇒ Object
Yields self and all children to block
recursively.
981 982 983 984 |
# File 'lib/nokogiri/xml/node.rb', line 981 def traverse(&block) children.each { |j| j.traverse(&block) } block.call(self) end |
#unlink ⇒ Object Also known as: remove
Unlink this node from its current context.
577 578 579 580 581 582 583 584 |
# File 'ext/nokogiri/xml_node.c', line 577
static VALUE unlink_node(VALUE self)
{
xmlNodePtr node;
Data_Get_Struct(self, xmlNode, node);
xmlUnlinkNode(node);
nokogiri_root_node(node);
return self;
}
|
#value?(value) ⇒ Boolean
Does this Node’s attributes include <value>
414 415 416 |
# File 'lib/nokogiri/xml/node.rb', line 414 def value?(value) values.include? value end |
#values ⇒ Object
Get the attribute values for this Node.
408 409 410 |
# File 'lib/nokogiri/xml/node.rb', line 408 def values attribute_nodes.map(&:value) end |
#wrap(html) ⇒ Object
Add html around this node
Returns self
163 164 165 166 167 168 |
# File 'lib/nokogiri/xml/node.rb', line 163 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
1117 1118 1119 |
# File 'lib/nokogiri/xml/node.rb', line 1117 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)
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 |
# File 'lib/nokogiri/xml/node.rb', line 1090 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
1125 1126 1127 |
# File 'lib/nokogiri/xml/node.rb', line 1125 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
1135 1136 1137 1138 |
# File 'lib/nokogiri/xml/node.rb', line 1135 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
885 886 887 |
# File 'lib/nokogiri/xml/node.rb', line 885 def xml? type == DOCUMENT_NODE end |