Skip to content

SelectSingle

Selects a single node based on the specified XPath expression.

1
myXml.SelectSingle(xpath: string):( Xml | null )

Selects a single node based on the specified XPath expression and executes a callback function if a node is found.

1
myXml.SelectSingle(xpath: string, callback: (node: Xml) => void):( Xml | null )

Parameters

string xpath
    The XPath expression to use for selecting the node.

(node: Xml) => void callback
    A callback function to execute on the selected node.

Returns

An Xml object representing the selected node. Returns null if no matching node is found.

Remarks

Returns null when no node matches. In server scripts, an error is thrown when the XPath selects more than one node; narrow the XPath with a predicate such as [1] or [Id = '1001'], or use Xml.SelectAll.

In server scripts, an error is thrown when the XPath selects more than one node; narrow the XPath with a predicate such as [1] or [Id = '1001'], or use Xml.SelectAll. The callback runs only when a node is found; inside the callback, this also refers to the selected node.

Examples

Select a node and check that it exists

1
2
3
4
5
6
var customer = $Xml.SelectSingle('Customer');

if (customer) {
    var name = customer.Evaluate('Name');                // 'Jane Doe'
    var email = customer.Evaluate('Email');              // '[email protected]'
}

Select a node with a predicate

1
2
var notebookLine = $Xml.SelectSingle("Lines/Line[Product = 'Notebook']");
var quantity = notebookLine ? notebookLine.EvaluateNumber('Quantity') : 0;   // 1

Update a node only when it exists

1
2
3
$Xml.SelectSingle('Customer', function (customer) {
    customer.SetValue('Name', customer.Evaluate('Name').toUpperCase());
});

See Also