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:
- Nokogiri::XML::Node#parent
- Nokogiri::XML::Node#children
- Nokogiri::XML::Node#next
- Nokogiri::XML::Node#previous
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(name, document) Show Source
-
# File lib/nokogiri/xml/node.rb, line 82 def initialize name, document # ... Ya. This is empty on purpose. end
- 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; VALUE name; VALUE document; VALUE rest; rb_scan_args(argc, argv, "2*", &name, &document, &rest); Data_Get_Struct(document, xmlDoc, doc); xmlNodePtr node = xmlNewNode(NULL, (xmlChar *)StringValuePtr(name)); node->doc = doc->doc; NOKOGIRI_ROOT_NODE(node); VALUE 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
- %(path, ns = document.root ? document.root.namespaces : {})
Alias for at
- /(*paths)
Alias for search
- <<(node)
Alias for add_child
- <=>(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 715 def <=> other return nil unless other.is_a?(Nokogiri::XML::Node) return nil unless document == other.document compare other end
- ==(other) Show Source
Test to see if this Node is equal to other
-
# File lib/nokogiri/xml/node.rb, line 552 def == other return false unless other return false unless other.respond_to?(:pointer_id) pointer_id == other.pointer_id end
- >(selector) Show Source
Search this node’s immidiate children using CSS selector selector
-
# File lib/nokogiri/xml/node.rb, line 197 def > selector ns = document.root.namespaces xpath CSS.xpath_for(selector, :prefix => "./", :ns => ns).first end
- [](name) Show Source
Get the attribute value for the attribute name
-
# File lib/nokogiri/xml/node.rb, line 230 def [] name return nil unless key?(name.to_s) get(name.to_s) 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 530 def accept visitor visitor.visit(self) end
- add_child(node) Show Source
Add node as a child of this Node. The new node must be a Nokogiri::XML::Node or a non-empty String. Returns the new child node.
-
# File lib/nokogiri/xml/node.rb, line 239 def add_child(node) Node.verify_nodeishness(node) if node.type == DOCUMENT_FRAG_NODE node.children.each do |child| add_child_node child end else add_child_node node end end
- add_namespace(p1, p2)
Alias for add_namespace_definition
- 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; Data_Get_Struct(self, xmlNode, node); xmlNsPtr 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) Show Source
Insert node after this Node (as a sibling).
-
# File lib/nokogiri/xml/node.rb, line 265 def add_next_sibling(node) Node.verify_nodeishness(node) if node.type == DOCUMENT_FRAG_NODE node.children.reverse.each do |child| add_next_sibling_node child end else add_next_sibling_node node end end
- add_previous_sibling(node) Show Source
Insert node before this Node (as a sibling).
-
# File lib/nokogiri/xml/node.rb, line 252 def add_previous_sibling(node) Node.verify_nodeishness(node) if node.type == DOCUMENT_FRAG_NODE node.children.each do |child| add_previous_sibling_node child end else add_previous_sibling_node node end end
- after(data) Show Source
Create nodes from data and insert them after this node (as a sibling).
-
# File lib/nokogiri/xml/node.rb, line 355 def after data fragment(data).children.to_a.reverse.each do |node| add_next_sibling node end self 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 482 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 NodeSet.new(document, parents.find_all { |parent| root.search(selector).include?(parent) }) 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 205 def at path, ns = document.root ? document.root.namespaces : {} search(path, ns).first 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 224 def at_css *rules css(*rules).first 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 215 def at_xpath *paths xpath(*paths).first 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; Data_Get_Struct(self, xmlNode, node); VALUE 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 303 def attributes Hash[*(attribute_nodes.map { |node| [node.node_name, node] }.flatten)] end
- before(data) Show Source
Create nodes from data and insert them before this node (as a sibling).
-
# File lib/nokogiri/xml/node.rb, line 345 def before data fragment(data).children.each do |node| add_previous_sibling node end self end
- blank?() Show Source
Is this node blank?
-
static VALUE blank_eh(VALUE self) { xmlNodePtr node; Data_Get_Struct(self, xmlNode, node); if(1 == xmlIsBlankNode(node)) return Qtrue; return Qfalse; }
- cdata?() Show Source
Returns true if this is a CDATA
-
# File lib/nokogiri/xml/node.rb, line 420 def cdata? type == CDATA_SECTION_NODE 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; Data_Get_Struct(self, xmlNode, node); xmlNodePtr child = node->children; xmlNodeSetPtr set = xmlXPathNodeSetCreate(child); if(!child) return Nokogiri_wrap_xml_node_set(set); child = child->next; while(NULL != child) { xmlXPathNodeSetAdd(set, child); child = child->next; } VALUE node_set = Nokogiri_wrap_xml_node_set(set); rb_iv_set(node_set, "@document", DOC_RUBY_OBJECT(node->doc)); return node_set; }
- clone(...)
Alias for dup
- comment?() Show Source
Returns true if this is a Comment
-
# File lib/nokogiri/xml/node.rb, line 415 def comment? type == COMMENT_NODE end
- content() Show Source
Returns the content for this Node
-
static VALUE get_content(VALUE self) { xmlNodePtr node; Data_Get_Struct(self, xmlNode, node); xmlChar * content = xmlNodeGetContent(node); if(content) { VALUE rval = NOKOGIRI_STR_NEW2(content); xmlFree(content); return rval; } return Qnil; }
- content=(string) Show Source
Set the Node content to string. The content gets XML escaped.
-
# File lib/nokogiri/xml/node.rb, line 388 def content= string self.native_content = encode_special_chars(string.to_s) 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; Data_Get_Struct(self, xmlNode, node); doc = node->doc; if(doc->extSubset) rb_raise(rb_eRuntimeError, "Document already has an external subset"); xmlDtdPtr 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; Data_Get_Struct(self, xmlNode, node); doc = node->doc; if(xmlGetIntSubset(doc)) rb_raise(rb_eRuntimeError, "Document already has an internal subset"); xmlDtdPtr 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 179 def css *rules # Pop off our custom function handler if it exists handler = ![ Hash, String, Symbol ].include?(rules.last.class) ? rules.pop : nil ns = rules.last.is_a?(Hash) ? rules.pop : (document.root ? document.root.namespaces : {}) rules = rules.map { |rule| CSS.xpath_for(rule, :prefix => ".//", :ns => ns) }.flatten.uniq + [ns, handler].compact xpath(*rules) end
- css_path() Show Source
Get the path to this node as a CSS expression
-
# File lib/nokogiri/xml/node.rb, line 473 def css_path path.split(/\//).map { |part| part.length == 0 ? nil : part.gsub(/\[(\d+)\]/, ':nth-of-type(\1)') }.compact.join(' > ') end
- decorate!() Show Source
Decorate this node with the decorators set up in this node’s Document
-
# File lib/nokogiri/xml/node.rb, line 88 def decorate! document.decorate(self) end
- default_namespace=(url) Show Source
Set the default namespace for this node to url
-
# File lib/nokogiri/xml/node.rb, line 504 def default_namespace= url add_namespace_definition(nil, url) end
- delete(name)
Alias for remove_attribute
- 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 442 def description return nil if document.xml? Nokogiri::HTML::ElementDescription[name] 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; if(rb_scan_args(argc, argv, "01", &level) == 0) level = INT2NUM((long)1); xmlNodePtr node, dup; 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 323 def each &block attribute_nodes.each { |node| block.call(node.node_name, node.value) } end
- elem?()
Alias for element?
- element?() Show Source
Returns true if this is an Element node
-
# File lib/nokogiri/xml/node.rb, line 455 def element? type == ELEMENT_NODE end
- encode_special_chars(p1) Show Source
Encode any special characters in string
-
static VALUE encode_special_chars(VALUE self, VALUE string) { xmlNodePtr node; Data_Get_Struct(self, xmlNode, node); xmlChar * encoded = xmlEncodeSpecialChars( node->doc, (const xmlChar *)StringValuePtr(string) ); VALUE 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; Data_Get_Struct(self, xmlNode, node); if(!node->doc) return Qnil; doc = node->doc; xmlDtdPtr dtd = doc->extSubset; if(!dtd) return Qnil; return Nokogiri_wrap_xml_node(Qnil, (xmlNodePtr)dtd); }
- has_attribute?(p1)
Alias for key?
- html?() Show Source
Returns true if this is an HTML::Document node
-
# File lib/nokogiri/xml/node.rb, line 430 def html? type == HTML_DOCUMENT_NODE end
- inner_html(*args) Show Source
Get the inner_html for this node’s Node#children
-
# File lib/nokogiri/xml/node.rb, line 468 def inner_html *args children.map { |x| x.to_html(*args) }.join end
- inner_html=(tags) Show Source
Set the inner_html for this Node to tags
-
# File lib/nokogiri/xml/node.rb, line 372 def inner_html= tags children.each { |x| x.remove} fragment(tags).children.to_a.each do |node| add_child node end self end
- inner_text()
Alias for content
- internal_subset() Show Source
Get the internal subset
-
static VALUE internal_subset(VALUE self) { xmlNodePtr node; xmlDocPtr doc; Data_Get_Struct(self, xmlNode, node); if(!node->doc) return Qnil; doc = node->doc; xmlDtdPtr 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 317 def keys attribute_nodes.map { |node| node.node_name } end
- 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 338 def matches? selector ancestors.last.search(selector).include?(self) end
- name()
Alias for node_name
- name=(p1)
Alias for node_name=
- 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 511 def namespace= ns if ns.document != document raise ArgumentError, 'namespace must be declared on the same document' end unless ns.is_a? Nokogiri::XML::Namespace raise TypeError, "#{ns.class} can't be coerced into Nokogiri::XML::Namespace" end set_namespace ns 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 ; Data_Get_Struct(self, xmlNode, node); VALUE list = rb_ary_new(); xmlNsPtr 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; }
- 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 401 def namespaces Hash[*namespace_definitions.map { |nd| key = ['xmlns', nd.prefix].compact.join(':') if RUBY_VERSION >= '1.9' && document.encoding begin key.force_encoding document.encoding rescue ArgumentError end end [key, nd.href] }.flatten] end
- next()
Alias for next_sibling
- 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 = node->next; if(!sibling) return Qnil; while(sibling && sibling->type != XML_ELEMENT_NODE) sibling = sibling->next; return sibling ? Nokogiri_wrap_xml_node(Qnil, sibling) : Qnil ; }
- 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 394 def parent= parent_node parent_node.add_child(self) parent_node end
- path() Show Source
Returns the path associated with this Node
-
static VALUE path(VALUE self) { xmlNodePtr node; xmlChar *path ; Data_Get_Struct(self, xmlNode, node); path = xmlGetNodePath(node); VALUE 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()
Alias for previous_sibling
- previous=(node)
Alias for add_previous_sibling
- 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); 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 449 def read_only? # According to gdome2, these are read-only node types [NOTATION_NODE, ENTITY_NODE, ENTITY_DECL].include?(type) end
- remove()
Alias for unlink
- remove_attribute(name) Show Source
Remove the attribute named name
-
# File lib/nokogiri/xml/node.rb, line 331 def remove_attribute name attributes[name].remove if key? name end
- replace(node) Show Source
replace this Node with the node in the Document. The new node must be a Nokogiri::XML::Node or a non-empty String. Returns the new child node.
-
# File lib/nokogiri/xml/node.rb, line 538 def replace node Node.verify_nodeishness(node) if node.type == DOCUMENT_FRAG_NODE node.children.each do |child| add_previous_sibling child end unlink else replace_node node end 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 96 def search *paths ns = paths.last.is_a?(Hash) ? paths.pop : (document.root ? document.root.namespaces : {}) xpath(*(paths.map { |path| path = path.to_s path =~ /^(\.\/|\/)/ ? path : CSS.xpath_for( path, :prefix => ".//", :ns => ns ) }.flatten.uniq) + [ns]) 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 572 def serialize *args, &block options = args.first.is_a?(Hash) ? args.shift : { :encoding => args[0], :save_with => args[1] || SaveOptions::FORMAT } encoding = options[:encoding] || document.encoding outstring = "" if encoding && outstring.respond_to?(:force_encoding) outstring.force_encoding(Encoding.find(encoding)) end io = StringIO.new(outstring) write_to io, options, &block io.string end
- swap(data) Show Source
Swap this Node for new nodes made from data
-
# File lib/nokogiri/xml/node.rb, line 364 def swap data before(data) remove self end
- text?() Show Source
Returns true if this is a Text node
-
# File lib/nokogiri/xml/node.rb, line 435 def text? type == TEXT_NODE 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 596 def to_html options = {} # FIXME: this is a hack around broken libxml versions return dump_html if %w[2 6] === LIBXML_VERSION.split('.')[0..1] options[:save_with] ||= SaveOptions::FORMAT | SaveOptions::NO_DECLARATION | SaveOptions::NO_EMPTY_TAGS | SaveOptions::AS_HTML serialize(options) 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 463 def to_s document.xml? ? to_xml : to_html 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 628 def to_xhtml options = {} # FIXME: this is a hack around broken libxml versions return dump_html if %w[2 6] === LIBXML_VERSION.split('.')[0..1] options[:save_with] ||= SaveOptions::FORMAT | SaveOptions::NO_DECLARATION | SaveOptions::NO_EMPTY_TAGS | SaveOptions::AS_XHTML serialize(options) 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 614 def to_xml options = {} encoding = nil options[:save_with] ||= SaveOptions::FORMAT | SaveOptions::AS_XML serialize(options) end
- traverse(&block) Show Source
Yields self and all children to block recursively.
-
# File lib/nokogiri/xml/node.rb, line 523 def traverse &block children.each{|j| j.traverse(&block) } block.call(self) end
- type()
Alias for node_type
- 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 311 def values attribute_nodes.map { |node| node.value } 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 675 def write_html_to io, options = {} # FIXME: this is a hack around broken libxml versions return (io << dump_html) if %w[2 6] === LIBXML_VERSION.split('.')[0..1] options[:save_with] ||= SaveOptions::FORMAT | SaveOptions::NO_DECLARATION | SaveOptions::NO_EMPTY_TAGS | SaveOptions::AS_HTML write_to io, options 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 657 def write_to io, *options options = options.first.is_a?(Hash) ? options.shift : {} encoding = options[:encoding] || options[0] save_options = options[:save_with] || options[1] || SaveOptions::FORMAT indent_text = options[:indent_text] || ' ' indent_times = options[:indent] || 2 config = SaveOptions.new(save_options) yield config if block_given? native_write_to(io, encoding, indent_text * indent_times, config.options) 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 690 def write_xhtml_to io, options = {} # FIXME: this is a hack around broken libxml versions return (io << dump_html) if %w[2 6] === LIBXML_VERSION.split('.')[0..1] options[:save_with] ||= SaveOptions::FORMAT | SaveOptions::NO_DECLARATION | SaveOptions::NO_EMPTY_TAGS | SaveOptions::AS_XHTML write_to io, options 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 707 def write_xml_to io, options = {} options[:save_with] ||= SaveOptions::FORMAT | SaveOptions::AS_XML write_to io, options end
- xml?() Show Source
Returns true if this is an XML::Document node
-
# File lib/nokogiri/xml/node.rb, line 425 def xml? type == DOCUMENT_NODE 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 128 def xpath *paths # Pop off our custom function handler if it exists handler = ![ Hash, String, Symbol ].include?(paths.last.class) ? paths.pop : nil ns = paths.last.is_a?(Hash) ? paths.pop : (document.root ? document.root.namespaces : {}) return NodeSet.new(document) unless document sets = paths.map { |path| ctx = XPathContext.new(self) ctx.register_namespaces(ns) set = ctx.evaluate(path, handler).node_set set.document = document document.decorate(set) set } return sets.first if sets.length == 1 NodeSet.new(document) do |combined| document.decorate(combined) sets.each do |set| set.each do |node| combined << node end end end end