123456789_123456789_123456789_123456789_123456789_

Class: LibXML::XML::XPath::Expression

Relationships & Source Files
Inherits: Object
Defined in: ext/libxml/ruby_xml_xpath_expression.c,
ext/libxml/ruby_xml_xpath_expression.c

Overview

The Expression class is used to compile ::LibXML::XML::XPath expressions so they can be parsed only once but reused multiple times.

doc = XML::Document.string(IO.read('some xml file'))
expr = XPath::Expression.new('//first')
doc.root.each do |node|
 result = node.find(expr) # many, many, many times
 # ...
end

Class Method Summary

Constructor Details

XPath::Expression.new(expression) ⇒ Expression

Compiles an ::LibXML::XML::XPath expression. This improves performance when an ::LibXML::XML::XPath expression is called multiple times.

doc = XML::Document.string('<header><first>hi</first></header>')
expr = XPath::Expression.new('//first')
nodes = doc.find(expr)
[ GitHub ]

  
# File 'ext/libxml/ruby_xml_xpath_expression.c', line 61

static VALUE rxml_xpath_expression_initialize(VALUE self, VALUE expression)
{
  xmlXPathCompExprPtr compexpr = xmlXPathCompile((const xmlChar*)StringValueCStr(expression));

  if (compexpr == NULL)
  {
    const xmlError *xerror = xmlGetLastError();
    rxml_raise(xerror);
  }

  DATA_PTR( self) = compexpr;
  return self;
}

Class Method Details

XPath::Expression.compile(expression) ⇒ Expression

Compiles an ::LibXML::XML::XPath expression. This improves performance when an ::LibXML::XML::XPath expression is called multiple times.

doc = XML::Document.string('<header><first>hi</first></header>')
expr = XPath::Expression.new('//first')
nodes = doc.find(expr)
[ GitHub ]

  
# File 'ext/libxml/ruby_xml_xpath_expression.c', line 45

static VALUE rxml_xpath_expression_compile(VALUE klass, VALUE expression)
{
  VALUE args[] = {expression};
  return rb_class_new_instance(1, args, cXMLXPathExpression);
}