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

Inherits:
Object
  • Object
show all
Defined in:
ext/libxml/ruby_xml_xpath_expression.c,
ext/libxml/ruby_xml_xpath_expression.c

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#XPath::Expression.new(expression) ⇒ XPath::Expression

Compiles an XPatch 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)


61
62
63
64
65
66
67
68
69
70
71
72
73
# 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)
  {
    xmlErrorPtr xerror = xmlGetLastError();
    rxml_raise(xerror);
  }

  DATA_PTR( self) = compexpr;
  return self;
}

Class Method Details

.XPath::Expression.compile(expression) ⇒ XPath::Expression

Compiles an XPatch 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)

Returns:



45
46
47
48
49
# 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);
}