Skip to content

Query

Executes an XQuery against the repository and returns the results as an array of strings.

1
$XmlRepository.Query(xquery: string, parameters?: object):Array<string>

Parameters

string xquery
    The XQuery expression to execute against the database.

object parameters optional
    An object containing parameters to bind to the XQuery expression.

Returns

An array of strings representing the results of the XQuery. Returns an empty array if the query yields no results.

Remarks

The query runs against the domain database. Each property of parameters is bound to the XQuery variable with the same name, for example { id : '42' } binds $id. Parameter values are passed as strings. Bind values with parameters instead of concatenating them into the query text.

The query syntax is described in detail on the XQuery standard page. XQuery failures are surfaced as script errors. Update expressions are executed by the repository, so use this method only when the query is intentionally mutating.

Info

To query a process database, use the collection function in the query and provide the process ID.

For the current process, you can obtain it with $Instance.ProcessId.

Examples

Read a value with a parameterized query

1
2
3
4
5
var names = $XmlRepository.Query('//Customer[Id = $id]/Name/text()', {
    id : $Xml.Evaluate('CustomerId')
});

var customerName = names.length ? names[0] : '';

Copy query results into the pool data

1
2
3
4
5
var customers = $XmlRepository.Query('//Customer[City = $city]', {
    city : $Xml.Evaluate('City')
});

$Xml.SelectSingle('Customers').InnerXml(customers.join(''));

Update a value with XQuery Update

1
2
3
4
5
6
$XmlRepository.Query(
    'for $customer in //Customer[Id = $id] ' +
    'return replace value of node $customer/Name with $name', {
        id : $Xml.Evaluate('CustomerId'),
        name : $Xml.Evaluate('CustomerName')
    });

Insert a node with XQuery Update

1
2
3
4
5
6
$XmlRepository.Query(
    'insert node <Source><Id>{ $id }</Id><Name>{ $name }</Name></Source> ' +
    'as last into /Definitions/Sources', {
        id : NewId(),
        name : $Xml.Evaluate('SourceName')
    });

Delete nodes with XQuery Update

1
2
3
4
$XmlRepository.Query('delete node //Customer[Id = $id]/History/Record[InstanceId = $instanceId]', {
    id : $Xml.Evaluate('CustomerId'),
    instanceId : $Instance.Id
});

Query the database of the current process

1
2
3
var results = $XmlRepository.Query('collection("' + $Instance.ProcessId + '")//Customer[Id = $id]', {
    id : $Xml.Evaluate('CustomerId')
});

See Also