Parent

Included Modules

Class Index [+]

Quicksearch

Nokogiri::XML::Node

  

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

ELEMENT_NODE

Element node type, see Nokogiri::XML::Node#element?

ATTRIBUTE_NODE

Attribute node type

TEXT_NODE

Text node type, see Nokogiri::XML::Node#text?

CDATA_SECTION_NODE

CDATA node type, see Nokogiri::XML::Node#cdata?

ENTITY_REF_NODE

Entity reference node type

ENTITY_NODE

Entity node type

PI_NODE

PI node type

COMMENT_NODE

Comment node type, see Nokogiri::XML::Node#comment?

DOCUMENT_NODE

Document node type, see Nokogiri::XML::Node#xml?

DOCUMENT_TYPE_NODE

Document type node type

DOCUMENT_FRAG_NODE

Document fragment node type

NOTATION_NODE

Notation node type

HTML_DOCUMENT_NODE

HTML document node type, see Nokogiri::XML::Node#html?

DTD_NODE

DTD node type

ELEMENT_DECL

Element declaration type

ATTRIBUTE_DECL

Attribute declaration type

ENTITY_DECL

Entity declaration type

NAMESPACE_DECL

Namespace declaration type

XINCLUDE_START

XInclude start type

XINCLUDE_END

XInclude end type

DOCB_DOCUMENT_NODE

DOCB document node type

Public Class Methods

new(name, document) click to toggle 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

%(path, ns = document.root ? document.root.namespaces : {}) click to toggle source
Alias for: at
/(*paths) click to toggle source
Alias for: search
<<(node_or_tags) click to toggle 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 self, to support chaining of calls (e.g., root << child1 << child2)

Also see related method add_child.

     # File lib/nokogiri/xml/node.rb, line 288
288:       def << node_or_tags
289:         add_child node_or_tags
290:         self
291:       end
<=>(other) click to toggle source
 

Compare two Node objects with respect to their Document. Nodes from different documents cannot be compared.

     # File lib/nokogiri/xml/node.rb, line 871
871:       def <=> other
872:         return nil unless other.is_a?(Nokogiri::XML::Node)
873:         return nil unless document == other.document
874:         compare other
875:       end
==(other) click to toggle source
 

Test to see if this Node is equal to other

     # File lib/nokogiri/xml/node.rb, line 718
718:       def == other
719:         return false unless other
720:         return false unless other.respond_to?(:pointer_id)
721:         pointer_id == other.pointer_id
722:       end
>(selector) click to toggle source
 

Search this node’s immediate children using CSS selector selector

     # File lib/nokogiri/xml/node.rb, line 219
219:       def > selector
220:         ns = document.root.namespaces
221:         xpath CSS.xpath_for(selector, :prefix => "./", :ns => ns).first
222:       end
[](name) click to toggle source
 

Get the attribute value for the attribute name

     # File lib/nokogiri/xml/node.rb, line 253
253:       def [] name
254:         return nil unless key?(name.to_s)
255:         get(name.to_s)
256:       end
Also aliased as: get_attribute, attr
[]=(name, value) click to toggle source
 

Set the attribute value for the attribute name to value

     # File lib/nokogiri/xml/node.rb, line 260
260:       def []= name, value
261:         set name.to_s, value
262:       end
Also aliased as: set_attribute
accept(visitor) click to toggle source
 

Accept a visitor. This method calls “visit” on visitor with self.

     # File lib/nokogiri/xml/node.rb, line 712
712:       def accept visitor
713:         visitor.visit(self)
714:       end
add_child(node_or_tags) click to toggle 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 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 +<<+.

     # File lib/nokogiri/xml/node.rb, line 271
271:       def add_child node_or_tags
272:         node_or_tags = coerce(node_or_tags)
273:         if node_or_tags.is_a?(XML::NodeSet)
274:           node_or_tags.each { |n| add_child_node n }
275:         else
276:           add_child_node node_or_tags
277:         end
278:         node_or_tags
279:       end
add_namespace(p1, p2) click to toggle source
add_namespace_definition(prefix, href) click to toggle source

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 #, but they will be included as an xmlns attribute when the node is serialized to XML.

static VALUE add_namespace_definition(VALUE self, VALUE prefix, VALUE href)
{
  xmlNodePtr node, namespacee;
  xmlNsPtr ns;

  Data_Get_Struct(self, xmlNode, node);
  namespacee = node ;

  ns = xmlSearchNs(
      node->doc,
      node,
      (const xmlChar *)(NIL_P(prefix) ? NULL : StringValuePtr(prefix))
  );

  if(!ns) {
    if (node->type != XML_ELEMENT_NODE) {
      namespacee = node->parent;
    }
    ns = xmlNewNs(
        namespacee,
        (const xmlChar *)StringValuePtr(href),
        (const xmlChar *)(NIL_P(prefix) ? NULL : StringValuePtr(prefix))
    );
  }

  if (!ns) return Qnil ;

  if(NIL_P(prefix) || node != namespacee) xmlSetNs(node, ns);

  return Nokogiri_wrap_xml_namespace(node->doc, ns);
}
Also aliased as: add_namespace
add_next_sibling(node_or_tags) click to toggle 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 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.

     # File lib/nokogiri/xml/node.rb, line 325
325:       def add_next_sibling node_or_tags
326:         raise ArgumentError.new("A document may not have multiple root nodes.") if parent.is_a?(XML::Document)
327:         
328:         node_or_tags = coerce(node_or_tags)
329:         if node_or_tags.is_a?(XML::NodeSet)
330:           if text?
331:             pivot = Nokogiri::XML::Node.new 'dummy', document
332:             add_next_sibling_node pivot
333:           else
334:             pivot = self
335:           end
336:           node_or_tags.reverse_each { |n| pivot.send :add_next_sibling_node, n }
337:           pivot.unlink if text?
338:         else
339:           add_next_sibling_node node_or_tags
340:         end
341:         node_or_tags
342:       end
add_previous_sibling(node_or_tags) click to toggle 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 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.

     # File lib/nokogiri/xml/node.rb, line 299
299:       def add_previous_sibling node_or_tags
300:         raise ArgumentError.new("A document may not have multiple root nodes.") if parent.is_a?(XML::Document) && !node_or_tags.is_a?(XML::ProcessingInstruction)
301: 
302:         node_or_tags = coerce(node_or_tags)
303:         if node_or_tags.is_a?(XML::NodeSet)
304:           if text?
305:             pivot = Nokogiri::XML::Node.new 'dummy', document
306:             add_previous_sibling_node pivot
307:           else
308:             pivot = self
309:           end
310:           node_or_tags.each { |n| pivot.send :add_previous_sibling_node, n }
311:           pivot.unlink if text?
312:         else
313:           add_previous_sibling_node node_or_tags
314:         end
315:         node_or_tags
316:       end
Also aliased as: previous=
after(node_or_tags) click to toggle 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 363
363:       def after node_or_tags
364:         add_next_sibling node_or_tags
365:         self
366:       end
ancestors(selector = nil) click to toggle 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 653
653:       def ancestors selector = nil
654:         return NodeSet.new(document) unless respond_to?(:parent)
655:         return NodeSet.new(document) unless parent
656: 
657:         parents = [parent]
658: 
659:         while parents.last.respond_to?(:parent)
660:           break unless ctx_parent = parents.last.parent
661:           parents << ctx_parent
662:         end
663: 
664:         return NodeSet.new(document, parents) unless selector
665: 
666:         root = parents.last
667: 
668:         NodeSet.new(document, parents.find_all { |parent|
669:           root.search(selector).include?(parent)
670:         })
671:       end
at(path, ns = document.root ? document.root.namespaces : {}) click to toggle source
 

Search for the first occurrence of path.

Returns nil if nothing is found, otherwise a Node.

     # File lib/nokogiri/xml/node.rb, line 228
228:       def at path, ns = document.root ? document.root.namespaces : {}
229:         search(path, ns).first
230:       end
Also aliased as: %
at_css(*rules) click to toggle 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 247
247:       def at_css *rules
248:         css(*rules).first
249:       end
at_xpath(*paths) click to toggle 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 238
238:       def at_xpath *paths
239:         xpath(*paths).first
240:       end
attr(name) click to toggle source
Alias for: []
attribute(name) click to toggle 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() click to toggle 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(name, namespace) click to toggle 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() click to toggle source
  

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 # instead.

     # File lib/nokogiri/xml/node.rb, line 464
464:       def attributes
465:         Hash[attribute_nodes.map { |node|
466:           [node.node_name, node]
467:         }]
468:       end
before(node_or_tags) click to toggle 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 351
351:       def before node_or_tags
352:         add_previous_sibling node_or_tags
353:         self
354:       end
blank? click to toggle 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 ;
}
canonicalize(mode=XML::XML_C14N_1_0,inclusive_namespaces=nil,with_comments=false) click to toggle source
     # File lib/nokogiri/xml/node.rb, line 891
891:       def canonicalize(mode=XML::XML_C14N_1_0,inclusive_namespaces=nil,with_comments=false)
892:         c14n_root = self
893:         document.canonicalize(mode, inclusive_namespaces, with_comments) do |node, parent|
894:           tn = node.is_a?(XML::Node) ? node : parent
895:           tn == c14n_root || tn.ancestors.include?(c14n_root)
896:         end
897:       end
cdata?() click to toggle source

Returns true if this is a CDATA

     # File lib/nokogiri/xml/node.rb, line 586
586:       def cdata?
587:         type == CDATA_SECTION_NODE
588:       end
child click to toggle 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 click to toggle 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;
}
children=(node_or_tags) click to toggle source
  

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=

     # File lib/nokogiri/xml/node.rb, line 387
387:       def children= node_or_tags
388:         node_or_tags = coerce(node_or_tags)
389:         children.unlink
390:         if node_or_tags.is_a?(XML::NodeSet)
391:           node_or_tags.each { |n| add_child_node n }
392:         else
393:           add_child_node node_or_tags
394:         end
395:         node_or_tags
396:       end
clone(...) click to toggle source
Alias for: dup
comment?() click to toggle source

Returns true if this is a Comment

     # File lib/nokogiri/xml/node.rb, line 581
581:       def comment?
582:         type == COMMENT_NODE
583:       end
content click to toggle 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;
}
Also aliased as: text, inner_text
content=(string) click to toggle 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 542
542:       def content= string
543:         self.native_content = encode_special_chars(string.to_s)
544:       end
create_external_subset(name, external_id, system_id) click to toggle 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(name, external_id, system_id) click to toggle source

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">
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, [namespace-bindings, custom-pseudo-class] click to toggle 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')

A hash of namespace bindings may be appended. For example:

  node.css('bike|tire', {'bike' => 'http://schwinn.com/'})

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)

Note that the CSS query string is case-sensitive with regards to your document type. That is, if you’re looking for “H1” in an HTML document, you’ll never find anything, since HTML tags will match only lowercase CSS queries. However, “H1” might be found in an XML document, where tags names are case-sensitive (e.g., “H1” is distinct from “h1”).

     # File lib/nokogiri/xml/node.rb, line 205
205:       def css *rules
206:         rules, handler, ns, binds = extract_params(rules)
207: 
208:         prefix = "#{implied_xpath_context}/"
209: 
210:         rules = rules.map { |rule|
211:           CSS.xpath_for(rule, :prefix => prefix, :ns => ns)
212:         }.flatten.uniq + [ns, handler, binds].compact
213: 
214:         xpath(*rules)
215:       end
css_path() click to toggle source

Get the path to this node as a CSS expression

     # File lib/nokogiri/xml/node.rb, line 644
644:       def css_path
645:         path.split(/\//).map { |part|
646:           part.length == 0 ? nil : part.gsub(/\[(\d+)\]/, ':nth-of-type(\1)')
647:         }.compact.join(' > ')
648:       end
decorate!() click to toggle 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) click to toggle source
 

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 #, but when this node is serialized to XML an “xmlns” attribute will appear. See also # and #

     # File lib/nokogiri/xml/node.rb, line 679
679:       def default_namespace= url
680:         add_namespace_definition(nil, url)
681:       end
delete(name) click to toggle source
Alias for: remove_attribute
description() click to toggle 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 613
613:       def description
614:         return nil if document.xml?
615:         Nokogiri::HTML::ElementDescription[name]
616:       end
do_xinclude(options = XML::ParseOptions::DEFAULT_XML, &block) click to toggle source
 

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.

     # File lib/nokogiri/xml/node.rb, line 881
881:       def do_xinclude options = XML::ParseOptions::DEFAULT_XML, &block
882:         options = Nokogiri::XML::ParseOptions.new(options) if Fixnum === options
883: 
884:         # give options to user
885:         yield options if block_given?
886: 
887:         # call c extension
888:         process_xincludes(options.to_i)
889:       end
document click to toggle 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 click to toggle 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;

  nokogiri_root_node(dup);

  return Nokogiri_wrap_xml_node(rb_obj_class(self), dup);
}
Also aliased as: clone
each() click to toggle source
 

Iterate over each attribute name and value pair for this Node.

     # File lib/nokogiri/xml/node.rb, line 484
484:       def each
485:         attribute_nodes.each { |node|
486:           yield [node.node_name, node.value]
487:         }
488:       end
elem?() click to toggle source
Alias for: element?
element?() click to toggle source

Returns true if this is an Element node

     # File lib/nokogiri/xml/node.rb, line 626
626:       def element?
627:         type == ELEMENT_NODE
628:       end
Also aliased as: elem?
element_children click to toggle 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;
}
Also aliased as: elements
elements() click to toggle source
Alias for: element_children
encode_special_chars(string) click to toggle 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 click to toggle 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 click to toggle 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) click to toggle source
 

Create a DocumentFragment containing tags that is relative to this context node.

     # File lib/nokogiri/xml/node.rb, line 506
506:       def fragment tags
507:         type = document.html? ? Nokogiri::HTML : Nokogiri::XML
508:         type::DocumentFragment.new(document, tags, self)
509:       end
fragment?() click to toggle source

Returns true if this is a DocumentFragment

     # File lib/nokogiri/xml/node.rb, line 606
606:       def fragment?
607:         type == DOCUMENT_FRAG_NODE
608:       end
get_attribute(name) click to toggle source
Alias for: []
has_attribute?(p1) click to toggle source
Alias for: key?
html?() click to toggle source

Returns true if this is an HTML::Document node

     # File lib/nokogiri/xml/node.rb, line 596
596:       def html?
597:         type == HTML_DOCUMENT_NODE
598:       end
inner_html(*args) click to toggle source

Get the inner_html for this node’s Node#children

     # File lib/nokogiri/xml/node.rb, line 639
639:       def inner_html *args
640:         children.map { |x| x.to_html(*args) }.join
641:       end
inner_html=(node_or_tags) click to toggle 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.

Also see related method children=

     # File lib/nokogiri/xml/node.rb, line 375
375:       def inner_html= node_or_tags
376:         self.children = node_or_tags
377:         self
378:       end
inner_text() click to toggle source
Alias for: content
internal_subset click to toggle 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?(attribute) click to toggle 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;
}
Also aliased as: has_attribute?
keys() click to toggle source
 

Get the attribute names for this Node.

     # File lib/nokogiri/xml/node.rb, line 478
478:       def keys
479:         attribute_nodes.map { |node| node.node_name }
480:       end
last_element_child click to toggle 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 click to toggle 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) click to toggle source
 

Returns true if this Node matches selector

     # File lib/nokogiri/xml/node.rb, line 499
499:       def matches? selector
500:         ancestors.last.search(selector).include?(self)
501:       end
name() click to toggle source
Alias for: node_name
name=(p1) click to toggle source
Alias for: node_name=
namespace() click to toggle source

returns the default namespace set on this node (as with an “xmlns=” attribute), as a Namespace object.

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) click to toggle source
 

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 # instead, or perhaps # with a nil prefix argument.

     # File lib/nokogiri/xml/node.rb, line 690
690:       def namespace= ns
691:         return set_namespace(ns) unless ns
692: 
693:         unless Nokogiri::XML::Namespace === ns
694:           raise TypeError, "#{ns.class} can't be coerced into Nokogiri::XML::Namespace"
695:         end
696:         if ns.document != document
697:           raise ArgumentError, 'namespace must be declared on the same document'
698:         end
699: 
700:         set_namespace ns
701:       end
namespace_definitions() click to toggle source

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=”).

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() click to toggle source
 

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 #

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) click to toggle 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() click to toggle source
 

Returns a Hash of {prefix => value} for all namespaces on this node and its ancestors.

This method returns the same namespaces as #.

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.

     # File lib/nokogiri/xml/node.rb, line 567
567:       def namespaces
568:         Hash[namespace_scopes.map { |nd|
569:           key = ['xmlns', nd.prefix].compact.join(':')
570:           if RUBY_VERSION >= '1.9' && document.encoding
571:             begin
572:               key.force_encoding document.encoding
573:             rescue ArgumentError
574:             end
575:           end
576:           [key, nd.href]
577:         }]
578:       end
next() click to toggle source
Alias for: next_sibling
next_element click to toggle 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 click to toggle 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) ;
}
Also aliased as: next
name click to toggle 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;
}
Also aliased as: name
name=(new_name) click to toggle 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;
}
Also aliased as: name=
node_type click to toggle 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);
}
Also aliased as: type
parent click to toggle source

Get the parent Node for this Node

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) click to toggle source
 

Set the parent Node for this Node

     # File lib/nokogiri/xml/node.rb, line 548
548:       def parent= parent_node
549:         parent_node.add_child(self)
550:         parent_node
551:       end
parse(string_or_io, options = nil) click to toggle 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 515
515:       def parse string_or_io, options = nil
516:         options ||= (document.html? ? ParseOptions::DEFAULT_HTML : ParseOptions::DEFAULT_XML)
517:         if Fixnum === options
518:           options = Nokogiri::XML::ParseOptions.new(options)
519:         end
520:         # Give the options to the user
521:         yield options if block_given?
522: 
523:         contents = string_or_io.respond_to?(:read) ?
524:           string_or_io.read :
525:           string_or_io
526: 
527:         return Nokogiri::XML::NodeSet.new(document) if contents.empty?
528: 
529:         ##
530:         # This is a horrible hack, but I don't care. See #313 for background.
531:         error_count = document.errors.length
532:         node_set = in_context(contents, options.to_i)
533:         if node_set.empty? and document.errors.length > error_count and options.recover?
534:           fragment = Nokogiri::HTML::DocumentFragment.parse contents
535:           node_set = fragment.children
536:         end
537:         node_set
538:       end
path click to toggle 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 click to toggle 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() click to toggle source
Alias for: previous_sibling
previous=(node_or_tags) click to toggle source
previous_element click to toggle 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 click to toggle 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);
}
Also aliased as: previous
read_only?() click to toggle source
 

Is this a read only node?

     # File lib/nokogiri/xml/node.rb, line 620
620:       def read_only?
621:         # According to gdome2, these are read-only node types
622:         [NOTATION_NODE, ENTITY_NODE, ENTITY_DECL].include?(type)
623:       end
remove() click to toggle source
Alias for: unlink
remove_attribute(name) click to toggle source
 

Remove the attribute named name

     # File lib/nokogiri/xml/node.rb, line 492
492:       def remove_attribute name
493:         attributes[name].remove if key? name
494:       end
Also aliased as: delete
replace(node_or_tags) click to toggle 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 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.

     # File lib/nokogiri/xml/node.rb, line 405
405:       def replace node_or_tags
406:         node_or_tags = coerce(node_or_tags)
407:         if node_or_tags.is_a?(XML::NodeSet)
408:           if text?
409:             replacee = Nokogiri::XML::Node.new 'dummy', document
410:             add_previous_sibling_node replacee
411:             unlink
412:           else
413:             replacee = self
414:           end
415:           node_or_tags.each { |n| replacee.add_previous_sibling n }
416:           replacee.unlink
417:         else
418:           replace_node node_or_tags
419:         end
420:         node_or_tags
421:       end
search(*paths) click to toggle 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:         # TODO use         paths, handler, ns, binds = extract_params(paths)
 99:         ns = paths.last.is_a?(Hash) ? paths.pop :
100:           (document.root ? document.root.namespaces : {})
101: 
102:         prefix = "#{implied_xpath_context}/"
103: 
104:         xpath(*(paths.map { |path|
105:           path = path.to_s
106:           path =~ /^(\.\/|\/|\.\.|\.$)/ ? path : CSS.xpath_for(
107:             path,
108:             :prefix => prefix,
109:             :ns     => ns
110:           )
111:         }.flatten.uniq) + [ns])
112:       end
Also aliased as: /
serialize(*args, &block) click to toggle 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 738
738:       def serialize *args, &block
739:         options = args.first.is_a?(Hash) ? args.shift : {
740:           :encoding   => args[0],
741:           :save_with  => args[1]
742:         }
743: 
744:         encoding = options[:encoding] || document.encoding
745:         options[:encoding] = encoding
746: 
747:         outstring = ""
748:         if encoding && outstring.respond_to?(:force_encoding)
749:           outstring.force_encoding(Encoding.find(encoding))
750:         end
751:         io = StringIO.new(outstring)
752:         write_to io, options, &block
753:         io.string
754:       end
set_attribute(name, value) click to toggle source
Alias for: []=
swap(node_or_tags) click to toggle 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 430
430:       def swap node_or_tags
431:         replace node_or_tags
432:         self
433:       end
text() click to toggle source
Also aliased as: to_str
Alias for: content
text?() click to toggle source

Returns true if this is a Text node

     # File lib/nokogiri/xml/node.rb, line 601
601:       def text?
602:         type == TEXT_NODE
603:       end
to_html(options = {}) click to toggle source
 

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.

     # File lib/nokogiri/xml/node.rb, line 763
763:       def to_html options = {}
764:         # FIXME: this is a hack around broken libxml versions
765:         return dump_html if Nokogiri.uses_libxml? && ]2 6] === LIBXML_VERSION.split('.')[0..1]
766: 
767:         options[:save_with] |= SaveOptions::DEFAULT_HTML if options[:save_with]
768:         options[:save_with] = SaveOptions::DEFAULT_HTML unless options[:save_with]
769:         serialize(options)
770:       end
to_s() click to toggle 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 634
634:       def to_s
635:         document.xml? ? to_xml : to_html
636:       end
to_str() click to toggle source
Alias for: text
to_xhtml(options = {}) click to toggle 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 789
789:       def to_xhtml options = {}
790:         # FIXME: this is a hack around broken libxml versions
791:         return dump_html if Nokogiri.uses_libxml? && ]2 6] === LIBXML_VERSION.split('.')[0..1]
792: 
793:         options[:save_with] |= SaveOptions::DEFAULT_XHTML if options[:save_with]
794:         options[:save_with] = SaveOptions::DEFAULT_XHTML unless options[:save_with]
795:         serialize(options)
796:       end
to_xml(options = {}) click to toggle 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 778
778:       def to_xml options = {}
779:         options[:save_with] ||= SaveOptions::DEFAULT_XML
780:         serialize(options)
781:       end
traverse(&block) click to toggle source
  

Yields self and all children to block recursively.

     # File lib/nokogiri/xml/node.rb, line 705
705:       def traverse &block
706:         children.each{|j| j.traverse(&block) }
707:         block.call(self)
708:       end
type() click to toggle source
Alias for: node_type
values() click to toggle source
 

Get the attribute values for this Node.

     # File lib/nokogiri/xml/node.rb, line 472
472:       def values
473:         attribute_nodes.map { |node| node.value }
474:       end
write_html_to(io, options = {}) click to toggle 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 837
837:       def write_html_to io, options = {}
838:         # FIXME: this is a hack around broken libxml versions
839:         return (io << dump_html) if Nokogiri.uses_libxml? && ]2 6] === LIBXML_VERSION.split('.')[0..1]
840: 
841:         options[:save_with] ||= SaveOptions::DEFAULT_HTML
842:         write_to io, options
843:       end
write_to(io, *options) click to toggle 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 815
815:       def write_to io, *options
816:         options       = options.first.is_a?(Hash) ? options.shift : {}
817:         encoding      = options[:encoding] || options[0]
818:         if Nokogiri.jruby?
819:           save_options  = options[:save_with] || options[1]
820:           indent_times  = options[:indent] || 0
821:         else
822:           save_options  = options[:save_with] || options[1] || SaveOptions::FORMAT
823:           indent_times  = options[:indent] || 2
824:         end
825:         indent_text   = options[:indent_text] || ' '
826: 
827:         config = SaveOptions.new(save_options.to_i)
828:         yield config if block_given?
829: 
830:         native_write_to(io, encoding, indent_text * indent_times, config.options)
831:       end
write_xhtml_to(io, options = {}) click to toggle 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 849
849:       def write_xhtml_to io, options = {}
850:         # FIXME: this is a hack around broken libxml versions
851:         return (io << dump_html) if Nokogiri.uses_libxml? && ]2 6] === LIBXML_VERSION.split('.')[0..1]
852: 
853:         options[:save_with] ||= SaveOptions::DEFAULT_XHTML
854:         write_to io, options
855:       end
write_xml_to(io, options = {}) click to toggle 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 863
863:       def write_xml_to io, options = {}
864:         options[:save_with] ||= SaveOptions::DEFAULT_XML
865:         write_to io, options
866:       end
xml?() click to toggle source

Returns true if this is an XML::Document node

     # File lib/nokogiri/xml/node.rb, line 591
591:       def xml?
592:         type == DOCUMENT_NODE
593:       end
xpath *paths, [namespace-bindings, variable-bindings, custom-handler-class] click to toggle source
 

Search this node for XPath paths. paths must be one or more XPath queries.

  node.xpath('.//title')

A hash of namespace bindings may be appended. For example:

  node.xpath('.//foo:name', {'foo' => 'http://example.org/'})
  node.xpath('.//xmlns:name', node.root.namespaces)

A hash of variable bindings may also be appended to the namespace bindings. For example:

  node.xpath('.//address[@domestic=$value]', nil, {:value => 'Yes'})

Custom XPath functions may also be defined. To define custom functions create a class and implement the function you want to define. The first argument to the method will be the current matching NodeSet. Any other arguments are ones that you pass in. Note that this class may appear anywhere in the argument list. 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 145
145:       def xpath *paths
146:         return NodeSet.new(document) unless document
147: 
148:         paths, handler, ns, binds = extract_params(paths)
149: 
150:         sets = paths.map { |path|
151:           ctx = XPathContext.new(self)
152:           ctx.register_namespaces(ns)
153:           path = path.gsub(/\/xmlns:/,'/:') unless Nokogiri.uses_libxml?
154: 
155:           binds.each do |key,value|
156:             ctx.register_variable key.to_s, value
157:           end if binds
158: 
159:           ctx.evaluate(path, handler)
160:         }
161:         return sets.first if sets.length == 1
162: 
163:         NodeSet.new(document) do |combined|
164:           sets.each do |set|
165:             set.each do |node|
166:               combined << node
167:             end
168:           end
169:         end
170:       end

Private Instance Methods

compare(other) click to toggle source

Compare this Node to other with respect to their Document

static VALUE compare(VALUE self, VALUE _other)
{
  xmlNodePtr node, other;
  Data_Get_Struct(self, xmlNode, node);
  Data_Get_Struct(_other, xmlNode, other);

  return INT2NUM((long)xmlXPathCmpNodes(other, node));
}
dump_html click to toggle source

Returns the Node as html.

static VALUE dump_html(VALUE self)
{
  xmlBufferPtr buf ;
  xmlNodePtr node ;
  VALUE html;

  Data_Get_Struct(self, xmlNode, node);

  buf = xmlBufferCreate() ;
  htmlNodeDump(buf, node->doc, node);
  html = NOKOGIRI_STR_NEW2(buf->content);
  xmlBufferFree(buf);
  return html ;
}
get(attribute) click to toggle source

Get the value for attribute

static VALUE get(VALUE self, VALUE attribute)
{
  xmlNodePtr node;
  xmlChar* propstr ;
  VALUE rval ;
  Data_Get_Struct(self, xmlNode, node);

  if(NIL_P(attribute)) return Qnil;

  propstr = xmlGetProp(node, (xmlChar *)StringValuePtr(attribute));

  if(!propstr) return Qnil;

  rval = NOKOGIRI_STR_NEW2(propstr);

  xmlFree(propstr);
  return rval ;
}
implied_xpath_context() click to toggle source
     # File lib/nokogiri/xml/node.rb, line 937
937:       def implied_xpath_context
938:         "./"
939:       end
in_context(p1, p2) click to toggle source

TODO: DOCUMENT ME

static VALUE in_context(VALUE self, VALUE _str, VALUE _options)
{
    xmlNodePtr node, list = 0, child_iter, node_children, doc_children;
    xmlNodeSetPtr set;
    xmlParserErrors error;
    VALUE doc, err;
    int doc_is_empty;

    Data_Get_Struct(self, xmlNode, node);

    doc = DOC_RUBY_OBJECT(node->doc);
    err = rb_iv_get(doc, "@errors");
    doc_is_empty = (node->doc->children == NULL) ? 1 : 0;
    node_children = node->children;
    doc_children  = node->doc->children;

    xmlSetStructuredErrorFunc((void *)err, Nokogiri_error_array_pusher);

    /* Twiddle global variable because of a bug in libxml2.
     * http://git.gnome.org/browse/libxml2/commit/?id=e20fb5a72c83cbfc8e4a8aa3943c6be8febadab7
     */
#ifndef HTML_PARSE_NOIMPLIED
    htmlHandleOmittedElem(0);
#endif

    /* This function adds a fake node to the child of +node+.  If the parser
     * does not exit cleanly with XML_ERR_OK, the list is freed.  This can
     * leave the child pointers in a bad state if they were originally empty.
     *
     * http://git.gnome.org/browse/libxml2/tree/parser.c#n13177
     * */
    error = xmlParseInNodeContext(node, StringValuePtr(_str),
                                  (int)RSTRING_LEN(_str),
                                  (int)NUM2INT(_options), &list);

    /* xmlParseInNodeContext should not mutate the original document or node,
     * so reassigning these pointers should be OK.  The reason we're reassigning
     * is because if there were errors, it's possible for the child pointers
     * to be manipulated. */
    if (error != XML_ERR_OK) {
      node->doc->children = doc_children;
      node->children = node_children;
    }

    /* make sure parent/child pointers are coherent so an unlink will work
     * properly (#331)
     */
    child_iter = node->doc->children ;
    while (child_iter) {
      if (child_iter->parent != (xmlNodePtr)node->doc)
        child_iter->parent = (xmlNodePtr)node->doc;
      child_iter = child_iter->next;
    }

#ifndef HTML_PARSE_NOIMPLIED
    htmlHandleOmittedElem(1);
#endif

    xmlSetStructuredErrorFunc(NULL, NULL);

    /* Workaround for a libxml2 bug where a parsing error may leave a broken
     * node reference in node->doc->children.
     * This workaround is limited to when a parse error occurs, the document
     * went from having no children to having children, and the context node is
     * part of a document fragment.
     * https://bugzilla.gnome.org/show_bug.cgi?id=668155
     */
    if (error != XML_ERR_OK && doc_is_empty && node->doc->children != NULL) {
      child_iter = node;
      while (child_iter->parent)
        child_iter = child_iter->parent;
        
      if (child_iter->type == XML_DOCUMENT_FRAG_NODE)
        node->doc->children = NULL;
    }

    /* FIXME: This probably needs to handle more constants... */
    switch (error) {
      case XML_ERR_INTERNAL_ERROR:
      case XML_ERR_NO_MEMORY:
        rb_raise(rb_eRuntimeError, "error parsing fragment (%d)", error);
        break;
      default:
        break;
    }

    set = xmlXPathNodeSetCreate(NULL);

    while (list) {
      xmlXPathNodeSetAddUnique(set, list);
      list = list->next;
    }

    return Nokogiri_wrap_xml_node_set(set, doc);
}
inspect_attributes() click to toggle source
     # File lib/nokogiri/xml/node.rb, line 941
941:       def inspect_attributes
942:         [:name, :namespace, :attribute_nodes, :children]
943:       end
content= click to toggle source

Set the content for this Node

static VALUE set_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 *)StringValuePtr(content));
  return content;
}
native_write_to(io, encoding, options) click to toggle source

Write this Node to io with encoding and options

static VALUE native_write_to(
    VALUE self,
    VALUE io,
    VALUE encoding,
    VALUE indent_string,
    VALUE options
) 
process_xincludes(options) click to toggle source

Loads and substitutes all xinclude elements below the node. The parser context will be initialized with options.

static VALUE process_xincludes(VALUE self, VALUE options)
{
  int rcode ;
  xmlNodePtr node;
  VALUE error_list = rb_ary_new();

  Data_Get_Struct(self, xmlNode, node);

  xmlSetStructuredErrorFunc((void *)error_list, Nokogiri_error_array_pusher);
  rcode = xmlXIncludeProcessTreeFlags(node, (int)NUM2INT(options));
  xmlSetStructuredErrorFunc(NULL, NULL);

  if (rcode < 0) {
    xmlErrorPtr error;

    error = xmlGetLastError();
    if(error)
      rb_exc_raise(Nokogiri_wrap_xml_syntax_error((VALUE)NULL, error));
    else
      rb_raise(rb_eRuntimeError, "Could not perform xinclude substitution");
  }

  return self;
}
[]=(property, value) click to toggle source

Set the property to value

static VALUE set(VALUE self, VALUE property, VALUE value)
{
  xmlNodePtr node, cur;
  xmlAttrPtr prop;
  Data_Get_Struct(self, xmlNode, node);

  /* If a matching attribute node already exists, then xmlSetProp will destroy
   * the existing node's children. However, if Nokogiri has a node object
   * pointing to one of those children, we are left with a broken reference.
   *
   * We can avoid this by unlinking these nodes first.
   */
  if (node->type != XML_ELEMENT_NODE)
    return(Qnil);
  prop = xmlHasProp(node, (xmlChar *)StringValuePtr(property));
  if (prop && prop->children) {
    for (cur = prop->children; cur; cur = cur->next) {
      if (cur->_private) {
        nokogiri_root_node(cur);
        xmlUnlinkNode(cur);
      }
    }
  }

  xmlSetProp(node, (xmlChar *)StringValuePtr(property),
      (xmlChar *)StringValuePtr(value));

  return value;
}
set_namespace(namespace) click to toggle source

Set the namespace to namespace

static VALUE set_namespace(VALUE self, VALUE namespace)
{
  xmlNodePtr node;
  xmlNsPtr ns = NULL;

  Data_Get_Struct(self, xmlNode, node);

  if(!NIL_P(namespace))
    Data_Get_Struct(namespace, xmlNs, ns);

  xmlSetNs(node, ns);

  return self;
}

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.