Class Nokogiri::XML::Node inherits from Object
Nokogiri::XML::Node is your window to the fun filled world of dealing with XML and HTML tags. A Nokogiri::XML::Node may be treated similarly to a hash with regard to attributes. For example (from irb):
irb(main):004:0> node => <a href="#foo" id="link">link</a> irb(main):005:0> node['href'] => "#foo" irb(main):006:0> node.keys => ["href", "id"] irb(main):007:0> node.values => ["#foo", "link"] irb(main):008:0> node['class'] = 'green' => "green" irb(main):009:0> node => <a href="#foo" id="link" class="green">link</a> irb(main):010:0>
See Nokogiri::XML::Node#[] and Nokogiri::XML#[]= for more information.
Nokogiri::XML::Node also has methods that let you move around your tree. For navigating your tree, see:
You may search this node’s subtree using Node#xpath and Node#css
Constants
- ATTRIBUTE_DECL
Attribute declaration type
- ATTRIBUTE_NODE
Attribute node type
- CDATA_SECTION_NODE
CDATA node type, see Nokogiri::XML::Node#cdata?
- COMMENT_NODE
Comment node type, see Nokogiri::XML::Node#comment?
- DOCB_DOCUMENT_NODE
DOCB document node type
- DOCUMENT_FRAG_NODE
Document fragment node type
- DOCUMENT_NODE
Document node type, see Nokogiri::XML::Node#xml?
- DOCUMENT_TYPE_NODE
Document type node type
- DTD_NODE
DTD node type
- ELEMENT_DECL
Element declaration type
- ELEMENT_NODE
Element node type, see Nokogiri::XML::Node#element?
- ENTITY_DECL
Entity declaration type
- ENTITY_NODE
Entity node type
- ENTITY_REF_NODE
Entity reference node type
- HTML_DOCUMENT_NODE
HTML document node type, see Nokogiri::XML::Node#html?
- NAMESPACE_DECL
Namespace declaration type
- NOTATION_NODE
Notation node type
- PI_NODE
PI node type
- TEXT_NODE
Text node type, see Nokogiri::XML::Node#text?
- XINCLUDE_END
XInclude end type
- XINCLUDE_START
XInclude start type
Public Class Methods
- new(...) Show Source
Create a new node with name sharing GC lifecycle with document
-
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 *)StringValuePtr(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; }
Public Instance Methods
- <=>(other) Show Source
Compare two Node objects with respect to their Document. Nodes from different documents cannot be compared.
-
# File lib/nokogiri/xml/node.rb, line 766 766: def <=> other 767: return nil unless other.is_a?(Nokogiri::XML::Node) 768: return nil unless document == other.document 769: compare other 770: end
- ==(other) Show Source
Test to see if this Node is equal to other
-
# File lib/nokogiri/xml/node.rb, line 605 605: def == other 606: return false unless other 607: return false unless other.respond_to?(:pointer_id) 608: pointer_id == other.pointer_id 609: end
- >(selector) Show Source
Search this node’s immediate children using CSS selector selector
-
# File lib/nokogiri/xml/node.rb, line 195 195: def > selector 196: ns = document.root.namespaces 197: xpath CSS.xpath_for(selector, :prefix => "./", :ns => ns).first 198: end
- [](name) Show Source
Get the attribute value for the attribute name
-
# File lib/nokogiri/xml/node.rb, line 229 229: def [] name 230: return nil unless key?(name.to_s) 231: get(name.to_s) 232: end
- []=(p1, p2) Show Source
Set the property to value
-
static VALUE set(VALUE self, VALUE property, VALUE value) { xmlNodePtr node; Data_Get_Struct(self, xmlNode, node); xmlSetProp(node, (xmlChar *)StringValuePtr(property), (xmlChar *)StringValuePtr(value)); return value; }
- accept(visitor) Show Source
Accept a visitor. This method calls “visit” on visitor with self.
-
# File lib/nokogiri/xml/node.rb, line 599 599: def accept visitor 600: visitor.visit(self) 601: end
- add_child(node_or_tags) Show Source
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 new child node.
-
# File lib/nokogiri/xml/node.rb, line 239 239: def add_child node_or_tags 240: node_or_tags = coerce(node_or_tags) 241: if node_or_tags.is_a?(XML::NodeSet) 242: node_or_tags.each { |n| add_child_node n } 243: else 244: add_child_node node_or_tags 245: end 246: end
- add_namespace_definition(p1, p2) Show Source
Adds a namespace definition with prefix using href
-
static VALUE add_namespace_definition(VALUE self, VALUE prefix, VALUE href) { xmlNodePtr node; xmlNsPtr ns; Data_Get_Struct(self, xmlNode, node); ns = xmlNewNs( node, (const xmlChar *)StringValuePtr(href), (const xmlChar *)(NIL_P(prefix) ? NULL : StringValuePtr(prefix)) ); if(!ns) { ns = xmlSearchNs( node->doc, node, (const xmlChar *)(NIL_P(prefix) ? NULL : StringValuePtr(prefix)) ); } if(NIL_P(prefix)) xmlSetNs(node, ns); return Nokogiri_wrap_xml_namespace(node->doc, ns); }
- add_next_sibling(node_or_tags) Show Source
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 new sibling node.
Also see related method after.
-
# File lib/nokogiri/xml/node.rb, line 271 271: def add_next_sibling node_or_tags 272: node_or_tags = coerce(node_or_tags) 273: if node_or_tags.is_a?(XML::NodeSet) 274: if '1.8.6' == RUBY_VERSION 275: node_or_tags.reverse.each { |n| add_next_sibling_node n } 276: else 277: node_or_tags.reverse_each { |n| add_next_sibling_node n } 278: end 279: else 280: add_next_sibling_node node_or_tags 281: end 282: end
- add_previous_sibling(node_or_tags) Show Source
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 new sibling node.
Also see related method before.
-
# File lib/nokogiri/xml/node.rb, line 255 255: def add_previous_sibling node_or_tags 256: node_or_tags = coerce(node_or_tags) 257: if node_or_tags.is_a?(XML::NodeSet) 258: node_or_tags.each { |n| add_previous_sibling_node n } 259: else 260: add_previous_sibling_node node_or_tags 261: end 262: end
- after(node_or_tags) Show Source
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.
-
# File lib/nokogiri/xml/node.rb, line 303 303: def after node_or_tags 304: add_next_sibling node_or_tags 305: self 306: end
- ancestors(selector = nil) Show Source
Get a list of ancestor Node for this Node. If selector is given, the ancestors must match selector
-
# File lib/nokogiri/xml/node.rb, line 548 548: def ancestors selector = nil 549: return NodeSet.new(document) unless respond_to?(:parent) 550: return NodeSet.new(document) unless parent 551: 552: parents = [parent] 553: 554: while parents.last.respond_to?(:parent) 555: break unless ctx_parent = parents.last.parent 556: parents << ctx_parent 557: end 558: 559: return NodeSet.new(document, parents) unless selector 560: 561: root = parents.last 562: 563: NodeSet.new(document, parents.find_all { |parent| 564: root.search(selector).include?(parent) 565: }) 566: end
- at(path, ns = document.root ? document.root.namespaces : {}) Show Source
Search for the first occurrence of path.
Returns nil if nothing is found, otherwise a Node.
-
# File lib/nokogiri/xml/node.rb, line 204 204: def at path, ns = document.root ? document.root.namespaces : {} 205: search(path, ns).first 206: end
- at_css(*rules) Show Source
Search this node for the first occurrence of CSS rules. Equivalent to css(rules).first See Node#css for more information.
-
# File lib/nokogiri/xml/node.rb, line 223 223: def at_css *rules 224: css(*rules).first 225: end
- at_xpath(*paths) Show Source
Search this node for the first occurrence of XPath paths. Equivalent to xpath(paths).first See Node#xpath for more information.
-
# File lib/nokogiri/xml/node.rb, line 214 214: def at_xpath *paths 215: xpath(*paths).first 216: end
- attribute(p1) Show Source
Get the attribute node with name
-
static VALUE attr(VALUE self, VALUE name) { xmlNodePtr node; xmlAttrPtr prop; Data_Get_Struct(self, xmlNode, node); prop = xmlHasProp(node, (xmlChar *)StringValuePtr(name)); if(! prop) return Qnil; return Nokogiri_wrap_xml_node(Qnil, (xmlNodePtr)prop); }
- attribute_nodes() Show Source
returns a list containing the Node attributes.
-
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(p1, p2) Show Source
Get the attribute node with name and namespace
-
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 *)StringValuePtr(name), NIL_P(namespace) ? NULL : (xmlChar *)StringValuePtr(namespace)); if(! prop) return Qnil; return Nokogiri_wrap_xml_node(Qnil, (xmlNodePtr)prop); }
- attributes() Show Source
Returns a hash containing the node’s attributes. The key is the attribute name, the value is a Nokogiri::XML::Attr representing the attribute.
-
# File lib/nokogiri/xml/node.rb, line 381 381: def attributes 382: Hash[*(attribute_nodes.map { |node| 383: [node.node_name, node] 384: }.flatten)] 385: end
- before(node_or_tags) Show Source
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.
-
# File lib/nokogiri/xml/node.rb, line 291 291: def before node_or_tags 292: add_previous_sibling node_or_tags 293: self 294: end
- blank?() Show Source
Is this node blank?
-
static VALUE blank_eh(VALUE self) { xmlNodePtr node; Data_Get_Struct(self, xmlNode, node); return (1 == xmlIsBlankNode(node)) ? Qtrue : Qfalse ; }
- cdata?() Show Source
Returns true if this is a CDATA
-
# File lib/nokogiri/xml/node.rb, line 481 481: def cdata? 482: type == CDATA_SECTION_NODE 483: end
- child() Show Source
Returns the child node
-
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() Show Source
Get the list of children for this node as a NodeSet
-
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; }
- comment?() Show Source
Returns true if this is a Comment
-
# File lib/nokogiri/xml/node.rb, line 476 476: def comment? 477: type == COMMENT_NODE 478: end
- content() Show Source
Returns the content for this Node
-
static VALUE get_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) Show Source
Set the Node’s content to a Text node containing string. The string gets XML escaped, not interpreted as markup.
-
# File lib/nokogiri/xml/node.rb, line 449 449: def content= string 450: self.native_content = encode_special_chars(string.to_s) 451: end
- create_external_subset(p1, p2, p3) Show Source
Create an external subset
-
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 *)StringValuePtr(name), NIL_P(external_id) ? NULL : (const xmlChar *)StringValuePtr(external_id), NIL_P(system_id) ? NULL : (const xmlChar *)StringValuePtr(system_id) ); if(!dtd) return Qnil; return Nokogiri_wrap_xml_node(Qnil, (xmlNodePtr)dtd); }
- create_internal_subset(p1, p2, p3) Show Source
Create an internal subset
-
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 *)StringValuePtr(name), NIL_P(external_id) ? NULL : (const xmlChar *)StringValuePtr(external_id), NIL_P(system_id) ? NULL : (const xmlChar *)StringValuePtr(system_id) ); if(!dtd) return Qnil; return Nokogiri_wrap_xml_node(Qnil, (xmlNodePtr)dtd); }
- css(*rules) Show Source
Search this node for CSS rules. rules must be one or more CSS selectors. For example:
node.css('title') node.css('body h1.bold') node.css('div + p.green', 'div#one')Custom CSS pseudo classes may also be defined. To define custom pseudo classes, create a class and implement the custom pseudo class you want defined. The first argument to the method will be the current matching NodeSet. Any other arguments are ones that you pass in. For example:
node.css('title:regex("\w+")', Class.new { def regex node_set, regex node_set.find_all { |node| node['some_attribute'] =~ /#{regex}/ } end }.new)-
# File lib/nokogiri/xml/node.rb, line 177 177: def css *rules 178: # Pop off our custom function handler if it exists 179: handler = ![ 180: Hash, String, Symbol 181: ].include?(rules.last.class) ? rules.pop : nil 182: 183: ns = rules.last.is_a?(Hash) ? rules.pop : 184: (document.root ? document.root.namespaces : {}) 185: 186: rules = rules.map { |rule| 187: xpath_rule = CSS.xpath_for(rule, :prefix => ".//", :ns => ns) 188: }.flatten.uniq + [ns, handler].compact 189: 190: xpath(*rules) 191: end
- css_path() Show Source
Get the path to this node as a CSS expression
-
# File lib/nokogiri/xml/node.rb, line 539 539: def css_path 540: path.split(/\//).map { |part| 541: part.length == 0 ? nil : part.gsub(/\[(\d+)\]/, ':nth-of-type(\1)') 542: }.compact.join(' > ') 543: end
- decorate!() Show Source
Decorate this node with the decorators set up in this node’s Document
-
# File lib/nokogiri/xml/node.rb, line 89 89: def decorate! 90: document.decorate(self) 91: end
- default_namespace=(url) Show Source
Set the default namespace for this node to url
-
# File lib/nokogiri/xml/node.rb, line 570 570: def default_namespace= url 571: add_namespace_definition(nil, url) 572: end
- description() Show Source
Fetch the Nokogiri::HTML::ElementDescription for this node. Returns nil on XML documents and on unknown tags.
-
# File lib/nokogiri/xml/node.rb, line 508 508: def description 509: return nil if document.xml? 510: Nokogiri::HTML::ElementDescription[name] 511: end
- document() Show Source
Get the document for this Node
-
static VALUE document(VALUE self) { xmlNodePtr node; Data_Get_Struct(self, xmlNode, node); return DOC_RUBY_OBJECT(node->doc); }
- dup(...) Show Source
Copy this node. An optional depth may be passed in, but it defaults to a deep copy. 0 is a shallow copy, 1 is a deep copy.
-
static VALUE duplicate_node(int argc, VALUE *argv, VALUE self) { VALUE level; xmlNodePtr node, dup; if(rb_scan_args(argc, argv, "01", &level) == 0) level = INT2NUM((long)1); Data_Get_Struct(self, xmlNode, node); dup = xmlDocCopyNode(node, node->doc, (int)NUM2INT(level)); if(dup == NULL) return Qnil; return Nokogiri_wrap_xml_node(rb_obj_class(self), dup); }
- each(&block) Show Source
Iterate over each attribute name and value pair for this Node.
-
# File lib/nokogiri/xml/node.rb, line 401 401: def each &block 402: attribute_nodes.each { |node| 403: block.call([node.node_name, node.value]) 404: } 405: end
- element?() Show Source
Returns true if this is an Element node
-
# File lib/nokogiri/xml/node.rb, line 521 521: def element? 522: type == ELEMENT_NODE 523: end
- element_children() Show Source
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-
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(p1) Show Source
Encode any special characters in string
-
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 *)StringValuePtr(string) ); encoded_str = NOKOGIRI_STR_NEW2(encoded); xmlFree(encoded); return encoded_str; }
- external_subset() Show Source
Get the external subset
-
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() Show Source
Returns the first child node of this node that is an element.
Example:
@doc.root.first_element_child.element? # => true
-
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) Show Source
Create a DocumentFragment containing tags that is relative to this context node.
-
# File lib/nokogiri/xml/node.rb, line 423 423: def fragment tags 424: type = document.html? ? Nokogiri::HTML : Nokogiri::XML 425: type::DocumentFragment.new(document, tags, self) 426: end
- fragment?() Show Source
Returns true if this is a DocumentFragment
-
# File lib/nokogiri/xml/node.rb, line 501 501: def fragment? 502: type == DOCUMENT_FRAG_NODE 503: end
- html?() Show Source
Returns true if this is an HTML::Document node
-
# File lib/nokogiri/xml/node.rb, line 491 491: def html? 492: type == HTML_DOCUMENT_NODE 493: end
- inner_html(*args) Show Source
Get the inner_html for this node’s Node#children
-
# File lib/nokogiri/xml/node.rb, line 534 534: def inner_html *args 535: children.map { |x| x.to_html(*args) }.join 536: end
- inner_html=(node_or_tags) Show Source
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.
-
# File lib/nokogiri/xml/node.rb, line 313 313: def inner_html= node_or_tags 314: node_or_tags = coerce(node_or_tags) 315: children.unlink 316: if node_or_tags.is_a?(XML::NodeSet) 317: node_or_tags.each { |n| add_child_node n } 318: else 319: add_child_node node_or_tags 320: end 321: self 322: end
- internal_subset() Show Source
Get the internal subset
-
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?(p1) Show Source
Returns true if attribute is set
-
static VALUE key_eh(VALUE self, VALUE attribute) { xmlNodePtr node; Data_Get_Struct(self, xmlNode, node); if(xmlHasProp(node, (xmlChar *)StringValuePtr(attribute))) return Qtrue; return Qfalse; }
- keys() Show Source
Get the attribute names for this Node.
-
# File lib/nokogiri/xml/node.rb, line 395 395: def keys 396: attribute_nodes.map { |node| node.node_name } 397: end
- last_element_child() Show Source
Returns the last child node of this node that is an element.
Example:
@doc.root.last_element_child.element? # => true
-
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() Show Source
Returns the line for this Node
-
static VALUE line(VALUE self) { xmlNodePtr node; Data_Get_Struct(self, xmlNode, node); return INT2NUM(xmlGetLineNo(node)); }
- matches?(selector) Show Source
Returns true if this Node matches selector
-
# File lib/nokogiri/xml/node.rb, line 416 416: def matches? selector 417: ancestors.last.search(selector).include?(self) 418: end
- namespace() Show Source
returns the Nokogiri::XML::Namespace for the node, if one exists.
-
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) Show Source
Set the namespace for this node to ns
-
# File lib/nokogiri/xml/node.rb, line 577 577: def namespace= ns 578: return set_namespace(ns) unless ns 579: 580: unless Nokogiri::XML::Namespace === ns 581: raise TypeError, "#{ns.class} can't be coerced into Nokogiri::XML::Namespace" 582: end 583: if ns.document != document 584: raise ArgumentError, 'namespace must be declared on the same document' 585: end 586: 587: set_namespace ns 588: end
- namespace_definitions() Show Source
returns a list of Namespace nodes defined on self
-
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() Show Source
returns a list of Namespace nodes in scope for self. this is all namespaces defined in the node, or in any ancestor node.
-
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?(p1, p2) Show Source
Returns true if attribute is set with namespace
-
static VALUE namespaced_key_eh(VALUE self, VALUE attribute, VALUE namespace) { xmlNodePtr node; Data_Get_Struct(self, xmlNode, node); if(xmlHasNsProp(node, (xmlChar *)StringValuePtr(attribute), NIL_P(namespace) ? NULL : (xmlChar *)StringValuePtr(namespace))) return Qtrue; return Qfalse; }
- namespaces() Show Source
Get a hash containing the Namespace definitions for this Node
-
# File lib/nokogiri/xml/node.rb, line 462 462: def namespaces 463: Hash[*namespace_scopes.map { |nd| 464: key = ['xmlns', nd.prefix].compact.join(':') 465: if RUBY_VERSION >= '1.9' && document.encoding 466: begin 467: key.force_encoding document.encoding 468: rescue ArgumentError 469: end 470: end 471: [key, nd.href] 472: }.flatten] 473: end
- next_element() Show Source
Returns the next Nokogiri::XML::Element type sibling node.
-
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() Show Source
Returns the next sibling node
-
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) ; }
- node_name() Show Source
Returns the name for this Node
-
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; }
- node_name=(p1) Show Source
Set the name for this Node
-
static VALUE set_name(VALUE self, VALUE new_name) { xmlNodePtr node; Data_Get_Struct(self, xmlNode, node); xmlNodeSetName(node, (xmlChar*)StringValuePtr(new_name)); return new_name; }
- node_type() Show Source
Get the type for this Node
-
static VALUE node_type(VALUE self) { xmlNodePtr node; Data_Get_Struct(self, xmlNode, node); return INT2NUM((long)node->type); }
- parent() Show Source
-
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) Show Source
-
# File lib/nokogiri/xml/node.rb, line 455 455: def parent= parent_node 456: parent_node.add_child(self) 457: parent_node 458: end
- parse(string_or_io, options = ParseOptions::DEFAULT_XML) Show Source
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.
-
# File lib/nokogiri/xml/node.rb, line 432 432: def parse string_or_io, options = ParseOptions::DEFAULT_XML 433: if Fixnum === options 434: options = Nokogiri::XML::ParseOptions.new(options) 435: end 436: # Give the options to the user 437: yield options if block_given? 438: 439: contents = string_or_io.respond_to?(:read) ? 440: string_or_io.read : 441: string_or_io 442: 443: return Nokogiri::XML::NodeSet.new(document) if contents.empty? 444: in_context(contents, options.to_i) 445: end
- path() Show Source
Returns the path associated with this Node
-
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() Show Source
Get the internal pointer number
-
static VALUE pointer_id(VALUE self) { xmlNodePtr node; Data_Get_Struct(self, xmlNode, node); return INT2NUM((long)(node)); }
- previous_element() Show Source
Returns the previous Nokogiri::XML::Element type sibling node.
-
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() Show Source
Returns the previous sibling node
-
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); }
- read_only?() Show Source
Is this a read only node?
-
# File lib/nokogiri/xml/node.rb, line 515 515: def read_only? 516: # According to gdome2, these are read-only node types 517: [NOTATION_NODE, ENTITY_NODE, ENTITY_DECL].include?(type) 518: end
- remove_attribute(name) Show Source
Remove the attribute named name
-
# File lib/nokogiri/xml/node.rb, line 409 409: def remove_attribute name 410: attributes[name].remove if key? name 411: end
- replace(node_or_tags) Show Source
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 new child node.
Also see related method swap.
-
# File lib/nokogiri/xml/node.rb, line 331 331: def replace node_or_tags 332: node_or_tags = coerce(node_or_tags) 333: if node_or_tags.is_a?(XML::NodeSet) 334: node_or_tags.each { |n| add_previous_sibling n } 335: unlink 336: else 337: replace_node node_or_tags 338: end 339: end
- search(*paths) Show Source
Search this node for paths. paths can be XPath or CSS, and an optional hash of namespaces may be appended. See Node#xpath and Node#css.
-
# File lib/nokogiri/xml/node.rb, line 97 97: def search *paths 98: ns = paths.last.is_a?(Hash) ? paths.pop : 99: (document.root ? document.root.namespaces : {}) 100: xpath(*(paths.map { |path| 101: path = path.to_s 102: path =~ /^(\.\/|\/)/ ? path : CSS.xpath_for( 103: path, 104: :prefix => ".//", 105: :ns => ns 106: ) 107: }.flatten.uniq) + [ns]) 108: end
- serialize(*args, &block) Show Source
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-
# File lib/nokogiri/xml/node.rb, line 625 625: def serialize *args, &block 626: options = args.first.is_a?(Hash) ? args.shift : { 627: :encoding => args[0], 628: :save_with => args[1] || SaveOptions::FORMAT 629: } 630: 631: encoding = options[:encoding] || document.encoding 632: 633: outstring = "" 634: if encoding && outstring.respond_to?(:force_encoding) 635: outstring.force_encoding(Encoding.find(encoding)) 636: end 637: io = StringIO.new(outstring) 638: write_to io, options, &block 639: io.string 640: end
- swap(node_or_tags) Show Source
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.
-
# File lib/nokogiri/xml/node.rb, line 348 348: def swap node_or_tags 349: replace node_or_tags 350: self 351: end
- text?() Show Source
Returns true if this is a Text node
-
# File lib/nokogiri/xml/node.rb, line 496 496: def text? 497: type == TEXT_NODE 498: end
- to_html(options = {}) Show Source
doc.to_html
See Node#write_to for a list of options. For formatted output, use Node#to_xhtml instead.
-
# File lib/nokogiri/xml/node.rb, line 649 649: def to_html options = {} 650: # FIXME: this is a hack around broken libxml versions 651: return dump_html if Nokogiri.uses_libxml? && ]2 6] === LIBXML_VERSION.split('.')[0..1] 652: 653: options[:save_with] ||= SaveOptions::FORMAT | 654: SaveOptions::NO_DECLARATION | 655: SaveOptions::NO_EMPTY_TAGS | 656: SaveOptions::AS_HTML 657: 658: serialize(options) 659: end
- to_s() Show Source
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.
-
# File lib/nokogiri/xml/node.rb, line 529 529: def to_s 530: document.xml? ? to_xml : to_html 531: end
- to_xhtml(options = {}) Show Source
Serialize this Node to XHTML using options
doc.to_xhtml(:indent => 5, :encoding => 'UTF-8')
See Node#write_to for a list of options
-
# File lib/nokogiri/xml/node.rb, line 679 679: def to_xhtml options = {} 680: # FIXME: this is a hack around broken libxml versions 681: return dump_html if Nokogiri.uses_libxml? && ]2 6] === LIBXML_VERSION.split('.')[0..1] 682: 683: options[:save_with] ||= SaveOptions::FORMAT | 684: SaveOptions::NO_DECLARATION | 685: SaveOptions::NO_EMPTY_TAGS | 686: SaveOptions::AS_XHTML 687: 688: serialize(options) 689: end
- to_xml(options = {}) Show Source
Serialize this Node to XML using options
doc.to_xml(:indent => 5, :encoding => 'UTF-8')
See Node#write_to for a list of options
-
# File lib/nokogiri/xml/node.rb, line 667 667: def to_xml options = {} 668: options[:save_with] ||= SaveOptions::FORMAT | SaveOptions::AS_XML 669: 670: serialize(options) 671: end
- traverse(&block) Show Source
Yields self and all children to block recursively.
-
# File lib/nokogiri/xml/node.rb, line 592 592: def traverse &block 593: children.each{|j| j.traverse(&block) } 594: block.call(self) 595: end
- unlink() Show Source
Unlink this node from its current context.
-
static VALUE unlink_node(VALUE self) { xmlNodePtr node; Data_Get_Struct(self, xmlNode, node); xmlUnlinkNode(node); NOKOGIRI_ROOT_NODE(node); return self; }
- values() Show Source
Get the attribute values for this Node.
-
# File lib/nokogiri/xml/node.rb, line 389 389: def values 390: attribute_nodes.map { |node| node.value } 391: end
- write_html_to(io, options = {}) Show Source
Write Node as HTML to io with options
See Node#write_to for a list of options
-
# File lib/nokogiri/xml/node.rb, line 726 726: def write_html_to io, options = {} 727: # FIXME: this is a hack around broken libxml versions 728: return (io << dump_html) if Nokogiri.uses_libxml? && ]2 6] === LIBXML_VERSION.split('.')[0..1] 729: 730: options[:save_with] ||= SaveOptions::FORMAT | 731: SaveOptions::NO_DECLARATION | 732: SaveOptions::NO_EMPTY_TAGS | 733: SaveOptions::AS_HTML 734: write_to io, options 735: end
- write_to(io, *options) Show Source
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
-
# File lib/nokogiri/xml/node.rb, line 708 708: def write_to io, *options 709: options = options.first.is_a?(Hash) ? options.shift : {} 710: encoding = options[:encoding] || options[0] 711: save_options = options[:save_with] || options[1] || SaveOptions::FORMAT 712: indent_text = options[:indent_text] || ' ' 713: indent_times = options[:indent] || 2 714: 715: 716: config = SaveOptions.new(save_options) 717: yield config if block_given? 718: 719: native_write_to(io, encoding, indent_text * indent_times, config.options) 720: end
- write_xhtml_to(io, options = {}) Show Source
Write Node as XHTML to io with options
See Node#write_to for a list of options
-
# File lib/nokogiri/xml/node.rb, line 741 741: def write_xhtml_to io, options = {} 742: # FIXME: this is a hack around broken libxml versions 743: return (io << dump_html) if Nokogiri.uses_libxml? && ]2 6] === LIBXML_VERSION.split('.')[0..1] 744: 745: options[:save_with] ||= SaveOptions::FORMAT | 746: SaveOptions::NO_DECLARATION | 747: SaveOptions::NO_EMPTY_TAGS | 748: SaveOptions::AS_XHTML 749: write_to io, options 750: end
- write_xml_to(io, options = {}) Show Source
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
-
# File lib/nokogiri/xml/node.rb, line 758 758: def write_xml_to io, options = {} 759: options[:save_with] ||= SaveOptions::FORMAT | SaveOptions::AS_XML 760: write_to io, options 761: end
- xml?() Show Source
Returns true if this is an XML::Document node
-
# File lib/nokogiri/xml/node.rb, line 486 486: def xml? 487: type == DOCUMENT_NODE 488: end
- xpath(*paths) Show Source
Search this node for XPath paths. paths must be one or more XPath queries. A hash of namespaces may be appended. For example:
node.xpath('.//title') node.xpath('.//foo:name', { 'foo' => 'http://example.org/' }) node.xpath('.//xmlns:name', node.root.namespaces)Custom XPath functions may also be defined. To define custom functions create a class and implement the # function you want to define. For example:
node.xpath('.//title[regex(., "\w+")]', Class.new { def regex node_set, regex node_set.find_all { |node| node['some_attribute'] =~ /#{regex}/ } end }.new)-
# File lib/nokogiri/xml/node.rb, line 129 129: def xpath *paths 130: # Pop off our custom function handler if it exists 131: handler = ![ 132: Hash, String, Symbol 133: ].include?(paths.last.class) ? paths.pop : nil 134: 135: ns = paths.last.is_a?(Hash) ? paths.pop : 136: (document.root ? document.root.namespaces : {}) 137: 138: return NodeSet.new(document) unless document 139: 140: sets = paths.map { |path| 141: ctx = XPathContext.new(self) 142: ctx.register_namespaces(ns) 143: path = path.gsub(/\/xmlns:/,'/:') unless Nokogiri.uses_libxml? 144: ctx.evaluate(path, handler) 145: } 146: return sets.first if sets.length == 1 147: 148: NodeSet.new(document) do |combined| 149: sets.each do |set| 150: set.each do |node| 151: combined << node 152: end 153: end 154: end 155: end