Parent

Included Modules

Nokogiri::XML::NodeSet

A NodeSet contains a list of Nokogiri::XML::Node objects. Typically a NodeSet is return as a result of searching a Document via Nokogiri::XML::Node#css or Nokogiri::XML::Node#xpath

Attributes

document[RW]

The Document this NodeSet is associated with

Public Class Methods

new(document, list = []) click to toggle source

Create a NodeSet with document defaulting to list

# File lib/nokogiri/xml/node_set.rb, line 14
      def initialize document, list = []
        @document = document
        list.each { |x| self << x }
        yield self if block_given?
      end

Public Instance Methods

%(path, ns = document.root ? document.root.namespaces : {}) click to toggle source

Alias for at

&(node_set) click to toggle source

Set Intersection — Returns a new NodeSet containing nodes common to the two NodeSets.

static VALUE intersection(VALUE self, VALUE rb_other)
{
  xmlNodeSetPtr node_set;
  xmlNodeSetPtr other;

  if(! rb_funcall(rb_other, rb_intern("is_a?"), 1, cNokogiriXmlNodeSet))
    rb_raise(rb_eArgError, "node_set must be a Nokogiri::XML::NodeSet");

  Data_Get_Struct(self, xmlNodeSet, node_set);
  Data_Get_Struct(rb_other, xmlNodeSet, other);

  return Nokogiri_wrap_xml_node_set(xmlXPathIntersection(node_set, other));
}
+(node_set) click to toggle source

Concatenation - returns a new NodeSet built by concatenating the node set with node_set to produce a third NodeSet

static VALUE plus(VALUE self, VALUE rb_other)
{
  xmlNodeSetPtr node_set;
  xmlNodeSetPtr other;
  xmlNodeSetPtr new;

  if(! rb_funcall(rb_other, rb_intern("is_a?"), 1, cNokogiriXmlNodeSet))
    rb_raise(rb_eArgError, "node_set must be a Nokogiri::XML::NodeSet");

  Data_Get_Struct(self, xmlNodeSet, node_set);
  Data_Get_Struct(rb_other, xmlNodeSet, other);

  new = xmlXPathNodeSetMerge(NULL, node_set);
  new = xmlXPathNodeSetMerge(new, other);

  VALUE new_set = Nokogiri_wrap_xml_node_set(new);

  rb_iv_set(new_set, "@document", rb_iv_get(self, "@document"));

  return new_set;
}
-(node_set) click to toggle source

Difference - returns a new NodeSet that is a copy of this NodeSet, removing each item that also appears in node_set

static VALUE minus(VALUE self, VALUE rb_other)
{
  xmlNodeSetPtr node_set;
  xmlNodeSetPtr other;
  xmlNodeSetPtr new;
  int j ;

  if(! rb_funcall(rb_other, rb_intern("is_a?"), 1, cNokogiriXmlNodeSet))
    rb_raise(rb_eArgError, "node_set must be a Nokogiri::XML::NodeSet");

  Data_Get_Struct(self, xmlNodeSet, node_set);
  Data_Get_Struct(rb_other, xmlNodeSet, other);

  new = xmlXPathNodeSetMerge(NULL, node_set);
  for (j = 0 ; j < other->nodeNr ; ++j) {
    xmlXPathNodeSetDel(new, other->nodeTab[j]);
  }

  return Nokogiri_wrap_xml_node_set(new);
}
/(*paths) click to toggle source

Alias for search

<<(p1) click to toggle source

Alias for push

==(other) click to toggle source

Equality — Two NodeSets are equal if the contain the same number of elements and if each element is equal to the corresponding element in the other NodeSet

# File lib/nokogiri/xml/node_set.rb, line 268
      def == other
        return false unless other.is_a?(Nokogiri::XML::NodeSet)
        return false unless length == other.length
        each_with_index do |node, i|
          return false unless node == other[i]
        end
        true
      end
[index] → Node or nil [start, length] → NodeSet or nil [range] → NodeSet or nil slice(index) → Node or nil slice(start, length) → NodeSet or nil slice(range) → NodeSet or nil click to toggle source

Element reference - returns the node at index, or returns a NodeSet containing nodes starting at start and continuing for length elements, or returns a NodeSet containing nodes specified by range. Negative indices count backward from the end of the node_set (-1 is the last node). Returns nil if the index (or start) are out of range.

static VALUE slice(int argc, VALUE *argv, VALUE self)
{
  VALUE arg ;
  long beg, len ;
  xmlNodeSetPtr node_set;
  Data_Get_Struct(self, xmlNodeSet, node_set);

  if (argc == 2) {
    beg = NUM2LONG(argv[0]);
    len = NUM2LONG(argv[1]);
    if (beg < 0) {
      beg += node_set->nodeNr ;
    }
    return subseq(self, beg, len);
  }

  if (argc != 1) {
    rb_scan_args(argc, argv, "11", NULL, NULL);
  }
  arg = argv[0];

  if (FIXNUM_P(arg)) {
    return index_at(self, FIX2LONG(arg));
  }
  
  /* if arg is Range */
  switch (rb_range_beg_len(arg, &beg, &len, node_set->nodeNr, 0)) {
  case Qfalse:
    break;
  case Qnil:
    return Qnil;
  default:
    return subseq(self, beg, len);
  }

  return index_at(self, NUM2LONG(arg));
}
Also aliased as: slice
add_class(name) click to toggle source

Append the class attribute name to all Node objects in the NodeSet.

# File lib/nokogiri/xml/node_set.rb, line 136
      def add_class name
        each do |el|
          next unless el.respond_to? :get_attribute
          classes = el.get_attribute('class').to_s.split(" ")
          el.set_attribute('class', classes.push(name).uniq.join(" "))
        end
        self
      end
after(datum) click to toggle source

Insert datum after the last Node in this NodeSet

# File lib/nokogiri/xml/node_set.rb, line 58
      def after datum
        last.after datum
      end
at(path, ns = document.root ? document.root.namespaces : {}) click to toggle source

If path is a string, search this document for path returning the first Node. Otherwise, index in to the array with path.

# File lib/nokogiri/xml/node_set.rb, line 128
      def at path, ns = document.root ? document.root.namespaces : {}
        return self[path] if path.is_a?(Numeric)
        search(path, ns).first
      end
Also aliased as: %
attr(key, value = nil, &blk) click to toggle source

Set the attribute key to value or the return value of blk on all Node objects in the NodeSet.

# File lib/nokogiri/xml/node_set.rb, line 163
      def attr key, value = nil, &blk
        if value or blk
          each do |el|
            el.set_attribute(key, value || blk[el])
          end
          return self
        end
        if key.is_a? Hash
          key.each { |k,v| self.attr(k,v) }
          return self
        else
          return self[0].get_attribute(key)
        end
      end
Also aliased as: set
before(datum) click to toggle source

Insert datum before the first Node in this NodeSet

# File lib/nokogiri/xml/node_set.rb, line 52
      def before datum
        first.before datum
      end
css(*paths) click to toggle source

Search this NodeSet for css paths

For more information see Nokogiri::XML::Node#css

# File lib/nokogiri/xml/node_set.rb, line 90
      def css *paths
        ns = paths.last.is_a?(Hash) ? paths.pop :
          (document.root ? document.root.namespaces : {})

        sub_set = NodeSet.new(document)

        xpaths = paths.map { |rule|
          [
            CSS.xpath_for(rule.to_s, :prefix => ".//", :ns => ns),
            CSS.xpath_for(rule.to_s, :prefix => "self::", :ns => ns)
          ].join(' | ')
        }
        each do |node|
          sub_set += node.xpath(*(xpaths + [ns]))
        end
        document.decorate(sub_set)
        sub_set
      end
delete(node) click to toggle source

Delete node from the Nodeset, if it is a member. Returns the deleted node if found, otherwise returns nil.

static VALUE delete(VALUE self, VALUE rb_node)
{
  xmlNodeSetPtr node_set ;
  xmlNodePtr node ;

  if(! rb_funcall(rb_node, rb_intern("is_a?"), 1, cNokogiriXmlNode))
    rb_raise(rb_eArgError, "node must be a Nokogiri::XML::Node");
  
  Data_Get_Struct(self, xmlNodeSet, node_set);
  Data_Get_Struct(rb_node, xmlNode, node);

  if (xmlXPathNodeSetContains(node_set, node)) {
    xmlXPathNodeSetDel(node_set, node);
    return rb_node ;
  }

  return Qnil ;
}
dup click to toggle source

Duplicate this node set

static VALUE duplicate(VALUE self)
{
  xmlNodeSetPtr node_set;
  Data_Get_Struct(self, xmlNodeSet, node_set);

  xmlNodeSetPtr dupl = xmlXPathNodeSetMerge(NULL, node_set);

  return Nokogiri_wrap_xml_node_set(dupl);
}
each(&block) click to toggle source

Iterate over each node, yielding to block

# File lib/nokogiri/xml/node_set.rb, line 191
      def each(&block)
        0.upto(length - 1) do |x|
          yield self[x]
        end
      end
empty?() click to toggle source

Is this NodeSet empty?

# File lib/nokogiri/xml/node_set.rb, line 39
      def empty?
        length == 0
      end
first(n = nil) click to toggle source

Get the first element of the NodeSet.

# File lib/nokogiri/xml/node_set.rb, line 22
      def first n = nil
        return self[0] unless n
        list = []
        0.upto(n - 1) do |i|
          list << self[i]
        end
        list
      end
include?(node) click to toggle source

Returns true if any member of node set equals node.

static VALUE include_eh(VALUE self, VALUE rb_node)
{
  xmlNodeSetPtr node_set;
  xmlNodePtr node;

  if(! rb_funcall(rb_node, rb_intern("is_a?"), 1, cNokogiriXmlNode))
    rb_raise(rb_eArgError, "node must be a Nokogiri::XML::Node");

  Data_Get_Struct(self, xmlNodeSet, node_set);
  Data_Get_Struct(rb_node, xmlNode, node);

  return (xmlXPathNodeSetContains(node_set, node) ? Qtrue : Qfalse);
}
index(node) click to toggle source

Returns the index of the first node in self that is == to node. Returns nil if no match is found.

# File lib/nokogiri/xml/node_set.rb, line 45
      def index(node)
        each_with_index { |member, j| return j if member == node }
        nil
      end
inner_html() click to toggle source

Get the inner html of all contained Node objects

# File lib/nokogiri/xml/node_set.rb, line 206
      def inner_html
        collect{|j| j.inner_html}.join('')
      end
inner_text() click to toggle source

Get the inner text of all contained Node objects

# File lib/nokogiri/xml/node_set.rb, line 199
      def inner_text
        collect{|j| j.inner_text}.join('')
      end
Also aliased as: text
last() click to toggle source

Get the last element of the NodeSet.

# File lib/nokogiri/xml/node_set.rb, line 33
      def last
        self[length - 1]
      end
length click to toggle source

Get the length of the node set

static VALUE length(VALUE self)
{
  xmlNodeSetPtr node_set;
  Data_Get_Struct(self, xmlNodeSet, node_set);

  if(node_set)
    return INT2NUM(node_set->nodeNr);

  return INT2NUM(0);
}
Also aliased as: size
pop() click to toggle source

Removes the last element from set and returns it, or nil if the set is empty

# File lib/nokogiri/xml/node_set.rb, line 251
      def pop
        return nil if length == 0
        delete last
      end
push(node) click to toggle source

Append node to the NodeSet.

static VALUE push(VALUE self, VALUE rb_node)
{
  xmlNodeSetPtr node_set;
  xmlNodePtr node;

  if(! rb_funcall(rb_node, rb_intern("is_a?"), 1, cNokogiriXmlNode))
    rb_raise(rb_eArgError, "node must be a Nokogiri::XML::Node");

  Data_Get_Struct(self, xmlNodeSet, node_set);
  Data_Get_Struct(rb_node, xmlNode, node);
  xmlXPathNodeSetAdd(node_set, node);
  return self;
}
Also aliased as: <<
remove() click to toggle source

Alias for unlink

remove_attr(name) click to toggle source

Remove the attributed named name from all Node objects in the NodeSet

# File lib/nokogiri/xml/node_set.rb, line 181
      def remove_attr name
        each do |el|
          next unless el.respond_to? :remove_attribute
          el.remove_attribute(name)
        end
        self
      end
remove_class(name = nil) click to toggle source

Remove the class attribute name from all Node objects in the NodeSet.

# File lib/nokogiri/xml/node_set.rb, line 147
      def remove_class name = nil
        each do |el|
          next unless el.respond_to? :get_attribute
          if name
            classes = el.get_attribute('class').to_s.split(" ")
            el.set_attribute('class', (classes - [name]).uniq.join(" "))
          else
            el.remove_attribute("class")
          end
        end
        self
      end
search(*paths) click to toggle source

Search this document for paths

For more information see Nokogiri::XML::Node#css and Nokogiri::XML::Node#xpath

# File lib/nokogiri/xml/node_set.rb, line 70
      def search *paths
        ns = paths.last.is_a?(Hash) ? paths.pop :
          (document.root ? document.root.namespaces : {})

        sub_set = NodeSet.new(document)
        each do |node|
          paths.each do |path|
            sub_set +=
              send(path =~ /^(\.\/|\/)/ ? :xpath : :css, *(paths + [ns]))
          end
        end
        document.decorate(sub_set)
        sub_set
      end
Also aliased as: /
set(key, value = nil, &blk) click to toggle source

Alias for attr

shift() click to toggle source

Returns the first element of the NodeSet and removes it. Returns nil if the set is empty.

# File lib/nokogiri/xml/node_set.rb, line 259
      def shift
        return nil if length == 0
        delete first
      end
size() click to toggle source

Alias for length

slice(...) click to toggle source

Alias for #[]

[index] → Node or nil [start, length] → NodeSet or nil [range] → NodeSet or nil slice(index) → Node or nil slice(start, length) → NodeSet or nil slice(range) → NodeSet or nil click to toggle source

Element reference - returns the node at index, or returns a NodeSet containing nodes starting at start and continuing for length elements, or returns a NodeSet containing nodes specified by range. Negative indices count backward from the end of the node_set (-1 is the last node). Returns nil if the index (or start) are out of range.

static VALUE slice(int argc, VALUE *argv, VALUE self)
{
  VALUE arg ;
  long beg, len ;
  xmlNodeSetPtr node_set;
  Data_Get_Struct(self, xmlNodeSet, node_set);

  if (argc == 2) {
    beg = NUM2LONG(argv[0]);
    len = NUM2LONG(argv[1]);
    if (beg < 0) {
      beg += node_set->nodeNr ;
    }
    return subseq(self, beg, len);
  }

  if (argc != 1) {
    rb_scan_args(argc, argv, "11", NULL, NULL);
  }
  arg = argv[0];

  if (FIXNUM_P(arg)) {
    return index_at(self, FIX2LONG(arg));
  }
  
  /* if arg is Range */
  switch (rb_range_beg_len(arg, &beg, &len, node_set->nodeNr, 0)) {
  case Qfalse:
    break;
  case Qnil:
    return Qnil;
  default:
    return subseq(self, beg, len);
  }

  return index_at(self, NUM2LONG(arg));
}
text() click to toggle source

Alias for inner_text

to_a click to toggle source

Return this list as an Array

static VALUE to_array(VALUE self, VALUE rb_node)
{
  xmlNodeSetPtr set;
  Data_Get_Struct(self, xmlNodeSet, set);

  VALUE *elts = calloc((size_t)set->nodeNr, sizeof(VALUE *));
  int i;
  for(i = 0; i < set->nodeNr; i++) {
    if(set->nodeTab[i]->_private) {
      elts[i] = (VALUE)set->nodeTab[i]->_private;
    } else {
      elts[i] = Nokogiri_wrap_xml_node(Qnil, set->nodeTab[i]);
    }
  }

  VALUE list = rb_ary_new4(set->nodeNr, elts);

  free(elts);

  return list;
}
Also aliased as: to_ary
to_ary() click to toggle source

Alias for to_a

to_html(*args) click to toggle source

Convert this NodeSet to HTML

# File lib/nokogiri/xml/node_set.rb, line 229
      def to_html *args
        map { |x| x.to_html(*args) }.join
      end
to_s() click to toggle source

Convert this NodeSet to a string.

# File lib/nokogiri/xml/node_set.rb, line 223
      def to_s
        map { |x| x.to_s }.join
      end
to_xhtml(*args) click to toggle source

Convert this NodeSet to XHTML

# File lib/nokogiri/xml/node_set.rb, line 235
      def to_xhtml *args
        map { |x| x.to_xhtml(*args) }.join
      end
to_xml(*args) click to toggle source

Convert this NodeSet to XML

# File lib/nokogiri/xml/node_set.rb, line 241
      def to_xml *args
        map { |x| x.to_xml(*args) }.join
      end
wrap(html, &blk) click to toggle source

Wrap this NodeSet with html or the results of the builder in blk

# File lib/nokogiri/xml/node_set.rb, line 212
      def wrap(html, &blk)
        each do |j|
          new_parent = Nokogiri.make(html, &blk)
          j.parent.add_child(new_parent)
          new_parent.add_child(j)
        end
        self
      end
xpath(*paths) click to toggle source

Search this NodeSet for XPath paths

For more information see Nokogiri::XML::Node#xpath

# File lib/nokogiri/xml/node_set.rb, line 113
      def xpath *paths
        ns = paths.last.is_a?(Hash) ? paths.pop :
          (document.root ? document.root.namespaces : {})

        sub_set = NodeSet.new(document)
        each do |node|
          sub_set += node.xpath(*(paths + [ns]))
        end
        document.decorate(sub_set)
        sub_set
      end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.