class LibXML::XML::XPath::Expression

The XML::XPath::Expression class is used to compile 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

Public Class Methods

XPath::Expression.compile(expression) → XPath::Expression click to toggle source

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

doc = XML::Document.string('<header><first>hi</first></header>')
expr = XPath::Expression.new('//first')
nodes = doc.find(expr)
static VALUE rxml_xpath_expression_compile(VALUE klass, VALUE expression)
{
  VALUE args[] = {expression};
  return rb_class_new_instance(1, args, cXMLXPathExpression);
}
XPath::Expression.new(expression) → XPath::Expression click to toggle source

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

doc = XML::Document.string('<header><first>hi</first></header>')
expr = XPath::Expression.new('//first')
nodes = doc.find(expr)
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;
}