HTML::O...hResult(3)
π NAME π
HTML::Object::DOM::XPathResult β the HTML Object DOM XPath Result Class. It's literally the interface that represents the results generated by evaluating an XPath expression within the context of a given node. π¦
πΈ SYNOPSIS πΈ
use HTML::Object::DOM::XPathResult;
my $result = HTML::Object::DOM::XPathResult->new ||
die( HTML::Object::DOM::XPathResult->error, "\n" );
β¨ VERSION β¨
v0.2.0 β iconic, periodt. π
π DESCRIPTION π
The XPathResult interface represents the results generated by evaluating an XPath expression within the context of a given node.
The methods 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. iykyk π«Ά
π PROPERTIES π
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.
π¦ booleanValue π¦
A boolean representing the value of the result if resultType is BOOLEAN_TYPE, i.e. if the result is a boolean.
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: https://developer.mozilla.org/en-US/docs/Web/API/XPathResult/booleanValue
π¦ invalidIteratorState π¦
This always returns undef under perl, but you can change the value of this boolean to whatever boolean value you want.
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.
Example:
<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';
See also Mozilla documentation: https://developer.mozilla.org/en-US/docs/Web/API/XPathResult/invalidIteratorState
π¦ numberValue π¦
A number representing the value of the result if resultType is NUMBER_TYPE, i.e. if the result is a number.
Example:
<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;
See also Mozilla documentation: https://developer.mozilla.org/en-US/docs/Web/API/XPathResult/numberValue
π¦ result π¦
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. Main character property energy, honestly. π
π¦ resultType π¦
A number code representing the type of the result, as defined by the type constants. See CONSTANTS.
Example:
<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;
See also Mozilla documentation: https://developer.mozilla.org/en-US/docs/Web/API/XPathResult/resultType
π¦ singleNodeValue π¦
A Node representing the value of the single node result, which may be undef. This is set when the result is a single node.
Example:
<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;
See also Mozilla documentation: https://developer.mozilla.org/en-US/docs/Web/API/XPathResult/singleNodeValue
π¦ snapshotLength π¦
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.
Example:
<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;
See also Mozilla documentation: https://developer.mozilla.org/en-US/docs/Web/API/XPathResult/snapshotLength
π¦ stringValue π¦
A string representing the value of the result if resultType is STRING_TYPE, i.e. when the result is a string.
Example:
<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;
See also Mozilla documentation: https://developer.mozilla.org/en-US/docs/Web/API/XPathResult/stringValue
π METHODS π
π§ iterateNext π§
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.
Example:
<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( ', ' );
See also Mozilla documentation: https://developer.mozilla.org/en-US/docs/Web/API/XPathResult/iterateNext
π snapshotItem π
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.
Example:
<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( ', ' );
See also Mozilla documentation: https://developer.mozilla.org/en-US/docs/Web/API/XPathResult/snapshotItem
β CONSTANTS β
The following constants are exported by default:
ANY_TYPE(0) β A result set containing whatever type naturally results from evaluation of the expression. Note that if the result is a node-set thenUNORDERED_NODE_ITERATOR_TYPEis always the resulting type.NUMBER_TYPE(1) β A result containing a single number. This is useful for example, in an XPath expression using thecount()function.STRING_TYPE(2) β A result containing a single string.BOOLEAN_TYPE(3) β A result containing a single boolean value. This is useful for example, in an XPath expression using thenot()function.UNORDERED_NODE_ITERATOR_TYPE(4) β 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.ORDERED_NODE_ITERATOR_TYPE(5) β 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.UNORDERED_NODE_SNAPSHOT_TYPE(6) β 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.ORDERED_NODE_SNAPSHOT_TYPE(7) β 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.ANY_UNORDERED_NODE_TYPE(8) β 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.FIRST_ORDERED_NODE_TYPE(9) β A result node-set containing the first node in the document that matches the expression.
π AUTHOR π
Jacques Deguest β jack@deguest.jp
π SEE ALSO π
Mozilla documentation: https://developer.mozilla.org/en-US/docs/Web/API/XPathResult
W3C specifications: https://dom.spec.whatwg.org/#interface-xpathresult
π 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.
Okay bestie, cute sign-off time: the source/version line says perl v5.42.2, the date is 2026-02-15, and the footer label is HTML::O...hResult(3). Slay, you made it to the end! πβ¨
π¦ where this page comes from
- alpine 3.24: perl-html-object-doc 0.6.0-r1 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms