Skip to content

SelectAll

Selects all matching nodes based on the specified XPath expression.

1
myXml.SelectAll(xpath: string):Array<Xml>

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
myXml.SelectAll(xpath: string, callback?: (node?: Xml, i?: number) => any):Array<any>

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
var lines = $Xml.SelectAll('Lines/Line');

for (var i = 0; i < lines.length; i++) {
    console.log(lines[i].Evaluate('Product'));          // 'Pen', 'Notebook'
}

Filter selected nodes with an XPath predicate

1
var bulkLines = $Xml.SelectAll('Lines/Line[Quantity > 1]');  // only the 'Pen' line

Calculate the order total from its lines

1
2
3
4
5
6
7
var total = 0;

$Xml.SelectAll('Lines/Line', function (line) {
    total += line.EvaluateNumber('Quantity') * line.EvaluateNumber('Price');
});

$Xml.SetValue('Total', total);                            // 7

Map selected nodes to values

1
2
3
4
var products = $Xml.SelectAll('Lines/Line', function (line, index) {
    return (index + 1) + '. ' + line.Evaluate('Product');
});
// products: ['1. Pen', '2. Notebook']

Test whether any node has a value

1
2
3
4
var hasNotebook = $Xml.SelectAll('Lines/Line/Product').some(function (product) {
    return product.Evaluate() === 'Notebook';
});
// hasNotebook: true

See Also