SelectAll¶
Selects all matching nodes based on the specified XPath expression.
1 | |
Selects all matching nodes based on the specified XPath expression and executes a callback function for each selected node. The callback maps results; it does not filter nodes.
1 | |
Parameters¶
string xpath
The XPath expression to use for selecting nodes.
(node?: Xml, i?: number) => any callback optional
A callback function to execute for each selected node. Optional. The callback function receives the selected node and its index as arguments.
Returns¶
(xpath): An array of Xml objects representing the selected nodes.(xpath, callback): An array with the return value of the callback for each selected node, or the node itself when the callback returns nothing.
Remarks¶
Returns an empty array when no node matches.
The callback receives the selected node and its zero-based index; inside the callback, this also refers to the selected node. The callback's return value becomes the array entry. When the callback returns nothing (undefined or null), the selected node itself is used as the entry.
The callback maps results; it does not filter them. Returning false still produces an array entry. To test whether any node matches, use SelectAll without a callback followed by Array.some; to keep only some nodes, use an XPath predicate or Array.filter.
Examples¶
Select all order lines¶
1 2 3 4 5 | |
Filter selected nodes with an XPath predicate¶
1 | |
Calculate the order total from its lines¶
1 2 3 4 5 6 7 | |
Map selected nodes to values¶
1 2 3 4 | |
Test whether any node has a value¶
1 2 3 4 | |