Xpath Functions
From Business Process Management, BPM and Workflow Automation Wiki | BizAgi BPMS
<keywords content="keywords"> functions, sum, add, adding, substract, min, minimum, max, maximum, remove, remove relation, index, distinct, average, xpath functions </keywords>
Contents |
Xpath Functions
XPath provides a series of functions that enable operations to be carried out and conditions to be established. The functions currently provided by XPath are:
Sum: Add up the value of a collection
Min: Get the minimum value of a collection
Max: Get the maximum value of a collection
Avg: Get the average value of a collection
Count : Count the number of entries in a collection
Empty: Determine whether or not there are any entries
Exist: Determine whether or not there are any entries
Remove: Delete an entry
distinct-values: Return a collection of unique, unrepeated entries
Index-Of: determine the position of one or more entries
Mathematical functions: average, exponentials, absolute value
Example:
In a Loan Request process a single client can request several products.
In the image below, there is a one-to-many relationship between Request and Products.
Sum
Adds all the records of a given collection
This function can only be used on collections that return values of numeric attributes (whole, decimals or currency)
Syntax
<sum( xPath )>
Me.getXPath("sum(XPath)");
In the image above, there is a one-to-many relationship between Request and Products. This last entity has a currency attribute called Value Requested, that can be added to obtain the total amount requested:
var Result= <sum(Request.Products.ValueRequested)>;
The statement Request.Products is the name of the relationship between Request and Products (the one with the star in the arrow).
Attributes can also be added using Me and Me.Context:
var Result= Me.getXPath("sum(Request.Products.ValueRequested)");
Filter
For example, the value of the requested products that have been approved is required.
When filtering with variables it is necessary to use Me.getXPath("sum(XPath)"); and NOT use the diamond brackets "< >"
See Filters, for more information.
var Result= <sum(Request.Products[Approved= true].ValueRequested)>;
Min
When using this function, the least value is obtained within the records of a collection.
This function can only be used on collections that return values of numeric attributes (whole, decimals or currency) .
Syntax
<min( xPath )>
Me.getXPath("min(XPath)");
Example: Using the same image, the least value of the Products is:
var min = <min(Request.Products.ValueRequested)>;
The same result will be obtained when using the expression Me
var min = Me.getXPath("min(Request.Products.ValueRequested)");
Filter
Using filters, the least value of the requested loans that have been approved can be found:
var min = <min(Request.Products[Approved= true].ValueRequested)>;
The same result can be obtained using Me and Me.Context:
'var min = Me.getXPath("min(Request.Products[Approved= true].ValueRequested)");'
When filtering with variables it is necessary to use Me.getXPath("sum(XPath)"); and NOT use the diamond brackets "< >"
Obtaining the minimum value of of all Credit Cards, needs the use of a variable in which the code is stored.
Assume the variable Code is used and the sentence will be written like this:
var min = Me.getXPath("min(Request.Products[ProductType.Code = " + Code+"].ValueRequested)");
Max
When using this function, the maximum value is obtained within the records of a collection.
This function can only be used on collections that return values of numeric attributes (whole, decimals or currency)
Syntax
<max(xPath )>
Me.getXPath("max(XPath)");
Filter
Using filters, the greatest value of the requested loans that have been approved can be found:
var max = <max(Request.Products[Approved= true].ValueRequested)>;
The same result can be obtained using Me and Me.Context:
var max = Me.getXPath("max(Request.Products[Approved= true].ValueRequested)");
When filtering with variables it is necessary to use Me.getXPath("sum(XPath)"); and NOT use the diamond brackets "< >"
var max = Me.getXPath("max(Request.Products[ProductType.Code = " + Code+"].ValueRequested)");
Avg (average)
When using this function, the average of the collection is obtained
This function can only be used on collections that return values of numeric attributes (whole, decimals or currency) .
Syntax
<avg(XPath )>
Me.getXPath("avg(XPath)");
Example: The average value of Loans Requested will be obtained:
var average= <avg(Request.Products.ValueRequested)>;
The examples used before work exactly the same, changing only the syntax.
Count
When using this function, the number of elements in the collection is obtained.
Syntax
<count( xPath )>
Me.getXPath("count(XPath)");
Example:
var TotalGuarantees = <count(Request.Products)>;
Returns how many products are in the Request
var validguarantees = <count(Request.Products[Approved= true])>;
Returns how many approved products are in the Request
Empty
This function returns True when the collection or relationship is empty and False otherwise.
Syntax
<empty( xPath )>
Me.getXPath("empty(XPath)");
Example:
var empty= <empty(Request.Products[Approved= true])>;
if (empty) {
CHelper.ThrowValidationError("There are no products included");
}
Exists
The function returns True when the collection or relationship has at least one element and False otherwise.
Syntax
<exists( xPath )>
Me.getXPath("exists(XPath)");
Example:
var exists= <exists(Request.Products)>;
if (exists) {
CHelper.ThrowValidationError("There are products included");
}
Example:
var exists= <exists(Request.Products[Approved = true])>;
if (!exists) {
CHelper.ThrowValidationError("There are no products included");
}
Remove
This function is used to delete an element from a result of an XPath expression . However it does not delete the record.
Syntax
<remove(Xpath, position)>
Example
A financial company offers a product that does not charge the first installment. The calculation of the insurance will be.
var quotas= <remove(Request.Quota, 1)>;
for (int x = 0; x < quotas.size(); x++) {
quotas.get(x).setXPath("insurance") = 100000;
}
In this case, the first element of the relationship is removed and then the value of each installment is applied.
The method removeRelation is used to delete an element of a relationship
Distinct-Values
This function is used to obtain a collection of elements that are not repeated
Syntax
<distinct-values(Xpath)>
If a client has several Loan requests, and each one has guarantees, the way to add the properties that have been related as guarantees to obtain the indebtedness capacity will be:
Example:
var Types = <distinc-values(Request.Guarantees.GuaranteeType)>;
Guarantees is the name of the fact between Request and the entity Guarantees. In a Request there are many guarantees.
This sentence returns a collection of the different Guarantees a Request has.
Index-Of
Determines the position of one or more entries.
Syntax
<index-of(XPath, Value)>
Example
var index = <index-of(Request.Products.Approved, true)>;
var Approved = Me.getXPath("Request.Products[" + index+ "]");
<comments />