HTML::O...hResult(3)

πŸ“¦ version 0.6.0, in alpine 3.24

πŸ’– 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 &lt;div&gt;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 &lt;div&gt; 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 then UNORDERED_NODE_ITERATOR_TYPE is always the resulting type.
  • NUMBER_TYPE (1) β€” A result containing a single number. This is useful for example, in an XPath expression using the count() 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 the not() 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(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