NAME

HTML::Object::DOM::XPathEvaluator - HTML Object DOM XPathEvaluator Class

SYNOPSIS

use HTML::Object::DOM::XPathEvaluator;
my $this = HTML::Object::DOM::XPathEvaluator->new || 
    die( HTML::Object::DOM::XPathEvaluator->error, "\n" );

VERSION

v0.2.0

DESCRIPTION

The XPathEvaluator interface allows to compile and evaluate XPath expressions.

PROPERTIES

There are no properties.

METHODS

createExpression

Creates a parsed XPath expression with resolved namespaces.

Example:

<div>XPath example</div>
<div>Number of &lt;div&gt;s: <output></output></div>

use HTML::Object::DOM;
my $parser = HTML::Object::DOM->new;
my $doc = $parser->parse_data( $html ) || die( $parser->error );
my $evaluator = HTML::Object::DOM::XPathEvaluator->new;
my $expression = $evaluator->createExpression( '//div' );
my $result = $expression->evaluate( $document );
$doc->querySelector( 'output' )->textContent = $result->snapshotLength;

See also Mozilla documentation

createNSResolver

This always returns undef, since this HTML::Object does not work on XML documents.

Normally, this would adapt any DOM node to resolve namespaces allowing the XPath expression to be evaluated relative to the context of the node where it appeared within the document.

See also Mozilla documentation

evaluate

Evaluates an XPath expression string and returns a result of the specified type if possible.

Parameters:

expression

A string representing the XPath expression to be parsed and evaluated, or an XPath expression object to be evaluated. The latter would be equivalent to:

$xpath_expression->evaluate( $context );

With $context being a node object provided, such as HTML::Object::DOM::Document.

contextNode

A Node representing the context to use for evaluating the expression.

Example:

<div>XPath example</div>
<div>Number of &lt;div&gt;s: <output></output></div>

use HTML::Object::DOM;
my $parser = HTML::Object::DOM->new;
my $doc = $parser->parse_data( $html ) || die( $parser->error );
my $evaluator = HTML::Object::DOM::XPathEvaluator->new;
my $result = $evaluator->evaluate( '//div', $doc );
$doc->querySelector( 'output' )->textContent = $result->snapshotLength;

See also Mozilla documentation

AUTHOR

Jacques Deguest <jack@deguest.jp>

SEE ALSO

HTML::Selector::XPath, HTML::Object::XPath, HTML::Object::XPath::Expr

COPYRIGHT & LICENSE

Copyright(c) 2022 DEGUEST Pte. Ltd.

All rights reserved

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.