Class: LibXML::XML::XPath::Object

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
ext/libxml/ruby_xml_xpath_object.c,
lib/libxml/xpath_object.rb,
ext/libxml/ruby_xml_xpath_object.c

Overview

A collection of nodes returned from the evaluation of an XML::XPath or XML::XPointer expression.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contextObject (readonly)

Instance Method Details

#[](aref) ⇒ Object

xpath_object -> node

array index into set of nodes



205
206
207
208
209
210
211
212
# File 'ext/libxml/ruby_xml_xpath_object.c', line 205

static VALUE rxml_xpath_object_aref(VALUE self, VALUE aref)
{
  if (rxml_xpath_object_empty_q(self) == Qtrue)
    return Qnil;

  return rxml_xpath_object_tabref((xmlXPathObjectPtr) DATA_PTR(self), NUM2INT(
      aref));
}

#debugObject

Dump libxml debugging information to stdout. Requires Libxml be compiled with debugging enabled.



285
286
287
288
289
290
291
292
293
294
295
296
# File 'ext/libxml/ruby_xml_xpath_object.c', line 285

static VALUE rxml_xpath_object_debug(VALUE self)
{
  xmlXPathObjectPtr xpop;

#ifndef LIBXML_DEBUG_ENABLED
  rb_raise(rb_eTypeError, "libxml was not compiled with debug support.");
#endif

  Data_Get_Struct(self, xmlXPathObject, xpop);
  xmlXPathDebugDumpObject(stdout, xpop, 0);
  return Qnil;
}

#each {|node| ... } ⇒ self

Call the supplied block for each node in this set.

Yields:

  • (node)

Returns:

  • (self)


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'ext/libxml/ruby_xml_xpath_object.c', line 168

static VALUE rxml_xpath_object_each(VALUE self)
{
  xmlXPathObjectPtr xpop;
  int i;

  if (rxml_xpath_object_empty_q(self) == Qtrue)
    return Qnil;

  Data_Get_Struct(self, xmlXPathObject, xpop);

  for (i = 0; i < xpop->nodesetval->nodeNr; i++)
  {
    rb_yield(rxml_xpath_object_tabref(xpop, i));
  }
  return (self);
}

#empty?Boolean

Determine whether this nodeset is empty (contains no nodes).

Returns:

  • (Boolean)


149
150
151
152
153
154
155
156
157
158
159
160
# File 'ext/libxml/ruby_xml_xpath_object.c', line 149

static VALUE rxml_xpath_object_empty_q(VALUE self)
{
  xmlXPathObjectPtr xpop;

  Data_Get_Struct(self, xmlXPathObject, xpop);

  if (xpop->type != XPATH_NODESET)
    return Qnil;

  return (xpop->nodesetval == NULL || xpop->nodesetval->nodeNr <= 0) ? Qtrue
      : Qfalse;
}

#firstObject

Returns the first node in this node set, or nil if none exist.



191
192
193
194
195
196
197
# File 'ext/libxml/ruby_xml_xpath_object.c', line 191

static VALUE rxml_xpath_object_first(VALUE self)
{
  if (rxml_xpath_object_empty_q(self) == Qtrue)
    return Qnil;

  return rxml_xpath_object_tabref((xmlXPathObjectPtr) DATA_PTR(self), 0);
}

#lengthNumeric

Obtain the length of the nodesetval node list.

Returns:

  • (Numeric)


220
221
222
223
224
225
226
227
228
229
230
# File 'ext/libxml/ruby_xml_xpath_object.c', line 220

static VALUE rxml_xpath_object_length(VALUE self)
{
  xmlXPathObjectPtr xpop;

  if (rxml_xpath_object_empty_q(self) == Qtrue)
    return INT2FIX(0);

  Data_Get_Struct(self, xmlXPathObject, xpop);

  return INT2NUM(xpop->nodesetval->nodeNr);
}

#setObject



5
6
7
8
# File 'lib/libxml/xpath_object.rb', line 5

def set
  warn("XPath::Object#set is deprecated.  Simply use the XPath::Object API instead")
  self
end

#lengthNumeric

Obtain the length of the nodesetval node list.

Returns:

  • (Numeric)


220
221
222
223
224
225
226
227
228
229
230
# File 'ext/libxml/ruby_xml_xpath_object.c', line 220

static VALUE rxml_xpath_object_length(VALUE self)
{
  xmlXPathObjectPtr xpop;

  if (rxml_xpath_object_empty_q(self) == Qtrue)
    return INT2FIX(0);

  Data_Get_Struct(self, xmlXPathObject, xpop);

  return INT2NUM(xpop->nodesetval->nodeNr);
}

#stringString

Returns the original XPath expression as a string.

Returns:



266
267
268
269
270
271
272
273
274
275
276
# File 'ext/libxml/ruby_xml_xpath_object.c', line 266

static VALUE rxml_xpath_object_string(VALUE self)
{
  xmlXPathObjectPtr xpop;

  Data_Get_Struct(self, xmlXPathObject, xpop);

  if (xpop->stringval == NULL)
    return Qnil;

  return rb_str_new2((const char*) xpop->stringval);
}

#to_aArray

Obtain an array of the nodes in this set.

Returns:

  • (Array)


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'ext/libxml/ruby_xml_xpath_object.c', line 122

static VALUE rxml_xpath_object_to_a(VALUE self)
{
  VALUE set_ary, nodeobj;
  xmlXPathObjectPtr xpop;
  int i;

  Data_Get_Struct(self, xmlXPathObject, xpop);

  set_ary = rb_ary_new();
  if (!((xpop->nodesetval == NULL) || (xpop->nodesetval->nodeNr == 0)))
  {
    for (i = 0; i < xpop->nodesetval->nodeNr; i++)
    {
      nodeobj = rxml_xpath_object_tabref(xpop, i);
      rb_ary_push(set_ary, nodeobj);
    }
  }

  return (set_ary);
}

#xpath_typeInteger

Returns the XPath type of the result object. Possible values are defined as constants on the XML::XPath class and include:

  • XML::XPath::UNDEFINED

  • XML::XPath::NODESET

  • XML::XPath::BOOLEAN

  • XML::XPath::NUMBER

  • XML::XPath::STRING

  • XML::XPath::POINT

  • XML::XPath::RANGE

  • XML::XPath::LOCATIONSET

  • XML::XPath::USERS

  • XML::XPath::XSLT_TREE

Returns:

  • (Integer)


251
252
253
254
255
256
257
258
# File 'ext/libxml/ruby_xml_xpath_object.c', line 251

static VALUE rxml_xpath_object_get_type(VALUE self)
{
  xmlXPathObjectPtr xpop;

  Data_Get_Struct(self, xmlXPathObject, xpop);

  return INT2FIX(xpop->type);
}