HTML::Object::DOM::XPathResult - HTML Object DOM XPath Result Class
use HTML::Object::DOM::XPathResult; my $result = HTML::Object::DOM::XPathResult->new || die( HTML::Object::DOM::XPathResult->error, "\n" );
v0.2.0
The XPathResult interface represents the results generated by evaluating an XPath expression within the context of a given node.
XPathResult
The method you can access vary depending on the type of results returned. The XPath evaluation can return a boolean, a number, a string, or a node set
All properties are read-only, but you can change their returned value by changing the value of "result", which contains the result from the XPath search.
A boolean representing the value of the result if resultType is BOOLEAN_TYPE, i.e. if the result is a boolean.
BOOLEAN_TYPE
Example:
<div>XPath example</div> <p>Text is 'XPath example': <output></output></p> my $xpath = "//div/text() = 'XPath example'"; my $result = $doc->evaluate( $xpath, $doc ); $doc->querySelector( 'output' )->textContent = $result->booleanValue;
See also Mozilla documentation
This always return undef under perl, but you can change the value of this boolean to whatever boolean value you want.
undef
Normally, under JavaScript, this signifies that the iterator has become invalid. It is true if resultType is UNORDERED_NODE_ITERATOR_TYPE or ORDERED_NODE_ITERATOR_TYPE and the document has been modified since this result was returned.
<div>XPath example</div> <p>Iterator state: <output></output></p> my $xpath = '//div'; my $result = $doc->evaluate( $xpath, $doc ); # Invalidates the iterator state $doc->querySelector( 'div' )->remove(); $doc->querySelector( 'output' )->textContent = $result->invalidIteratorState ? 'invalid' : 'valid';
A number representing the value of the result if resultType is NUMBER_TYPE, i.e. if the result is a number.
NUMBER_TYPE
<div>XPath example</div> <div>Number of <div>s: <output></output></div> my $xpath = 'count(//div)'; my $result = $doc->evaluate( $xpath, $doc ); $doc->querySelector( 'output' )->textContent = $result->numberValue;
Sets or gets the resulting object from the XPath search. This could be a node, a boolean, a number, a string, or a set of nodes
A number code representing the type of the result, as defined by the type constants. See "CONSTANTS"
<div>XPath example</div> <div>Is XPath result a node set: <output></output></div> use HTML::Object::DOM::XPathResult; # or use HTML::Object::DOM qw( :xpath ); my $xpath = '//div'; my $result = $doc->evaluate( $xpath, $doc ); $doc->querySelector( 'output' )->textContent = $result->resultType >= UNORDERED_NODE_ITERATOR_TYPE && $result->resultType <= FIRST_ORDERED_NODE_TYPE;
A Node representing the value of the single node result, which may be undef. This is set when the result is a single node.
<div>XPath example</div> <div>Tag name of the element having the text content 'XPath example': <output></output></div> my $xpath = q{//*[text()='XPath example']}; my $result = $doc->evaluate( $xpath, $doc ); $doc->querySelector( 'output' )->textContent = $result->singleNodeValue->localName;
The number of nodes in the result snapshot. As a divergence from the standard, this also applies to the number of elements in the NodeSet returned.
<div>XPath example</div> <div>Number of matched nodes: <output></output></div> my $xpath = '//div'; my $result = $doc->evaluate( $xpath, $doc ); $doc->querySelector( 'output' )->textContent = $result->snapshotLength;
A string representing the value of the result if resultType is STRING_TYPE, i.e. when the result is a string.
STRING_TYPE
<div>XPath example</div> <div>Text content of the <div> above: <output></output></div> my $xpath = '//div/text()'; my $result = $doc->evaluate( $xpath, $doc ); $doc->querySelector( 'output' )->textContent = $result->stringValue;
If the result is a node set, this method iterates over it and returns the next node from it or undef if there are no more nodes.
<div>XPath example</div> <div>Tag names of the matched nodes: <output></output></div> use Module::Generic::Array; my $xpath = '//div'; my $result = $doc->evaluate( $xpath, $doc ); my $node; my $tagNames = Module::Generic::Array->new; while( $node = $result->iterateNext() ) { $tagNames->push( $node->localName ); } $doc->querySelector( 'output' )->textContent = $tagNames->join( ', ' );
Returns an item of the snapshot collection or undef in case the index is not within the range of nodes.
Normally, under JavaScript, unlike the iterator result, the snapshot does not become invalid, but may not correspond to the current document if it is mutated.
<div>XPath example</div> <div>Tag names of the matched nodes: <output></output></div> use Module::Generic::Array; my $xpath = '//div'; my $result = $doc->evaluate( $xpath, $doc ); my $node; my $tagNames = Module::Generic::Array->new; for( my $i = 0; $i < $result->snapshotLength; $i++ ) { my $node = $result->snapshotItem( $i ); $tagNames->push( $node->localName ); } $doc->querySelector( 'output' )->textContent = $tagNames->join( ', ' );
The following constants are exported by default:
A result set containing whatever type naturally results from evaluation of the expression. Note that if the result is a node-set then UNORDERED_NODE_ITERATOR_TYPE is always the resulting type.
UNORDERED_NODE_ITERATOR_TYPE
A result containing a single number. This is useful for example, in an XPath expression using the count() function.
A result containing a single string.
A result containing a single boolean value. This is useful for example, in an XPath expression using the not() function.
A result node-set containing all the nodes matching the expression. The nodes may not necessarily be in the same order that they appear in the document.
A result node-set containing all the nodes matching the expression. The nodes in the result set are in the same order that they appear in the document.
A result node-set containing snapshots of all the nodes matching the expression. The nodes may not necessarily be in the same order that they appear in the document.
A result node-set containing snapshots of all the nodes matching the expression. The nodes in the result set are in the same order that they appear in the document.
A result node-set containing any single node that matches the expression. The node is not necessarily the first node in the document that matches the expression.
A result node-set containing the first node in the document that matches the expression.
Jacques Deguest <jack@deguest.jp>
Mozilla documentation, W3C specifications
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.
To install HTML::Object, copy and paste the appropriate command in to your terminal.
cpanm
cpanm HTML::Object
CPAN shell
perl -MCPAN -e shell install HTML::Object
For more information on module installation, please visit the detailed CPAN module installation guide.