Using Xpath
From Business Process Management, BPM and Workflow Automation Wiki | BizAgi BPMS
<keywords content="keywords"> getXPath, setXPath, Me.getXPath, Me.setXPath, addRelation, add elements, Me.addRelation </keywords>
Contents |
How to Build XPath expressions
Bizagi as a BPM tool is meant to be very intuitive and allow the agile automation of business processes and their subsequent modifications with a view to continuous improvement.
With this objective in mind, Bizagi presents the XPath concept, which is a mechanism to standardize the language in the tool and makes handling business rules more intuitive for everyone involved.
The Xpath standard is the result of the effort to construct an easy-to-use language in XMLs that is useful to carry out complex tasks. Bizagi incorporated this standard into its language, enabling the user to carry out common tasks on Business Entities, as well as assignations, queries and navigate over business entities.
In addition, XPath offers the possibility of carrying out operations by means of the functions it presents and establishes conditions intuitively to get the filtered data.
Obtaining information with XPath
< XPath expression >
XPath expressions are written between diamond brackets: '<' and '>'
XPath expressions will ALWAYS begin with the Process Entity as the first attribute and navigate the data model using periods between attributes.
The image above will be used as the guide to show how Xpath Expressions work.
A Loan Request process has a Process Entity called Request. This is the entity from which all the data model navigation, using Xpath will be started from.
Each Request has one Client and has many Guarantees (Guarantees is a one-to-many relationship, or collection).
Using XPath you can obtain the value stored in an attribute or you can set a value to one.
For the next examples we will use a variable called ExampleVariable that we will use to store the value of an attribute.
To obtain the Value of the Request, you would use the expression:
ExampleVariable = <Request.TotalValueRequest>
To get the name of the client associated with the request, you would use the expression:
ExampleVariable = <Request.Clients.Name>
To find the identifier of the Client making the Request, you would use the expression:
ExampleVariable = <Request.Clients.id>
In this case, you have to use the attribute id to be able to get the entity identifier, because the expression<Request.Clients> refers to the Client of the Request, and not its identifier. All Bizagi entities have a “hidden” attribute called id, that is related to the entity key.
Assigning values with XPath
The easiest way to assign a value to an element is using the "=" operator.
<Client.Name>= "Peter" Assigns the name "Peter" to the client
<Request.TotalValueRequested> = 1000 Assigns 1,000 to the Total Value Requested
Examples
According to how an XPath is written you can obtain the value of an attribute, or set one to it.
Variable = <BusinessEntity.Attribute>
Stores in the variable the attribute.
<BusinessEntity.Attribute> = Variable
Sets the value of the variable to the attribute.
The diagram shows relationships between four entities in which the following XPath expressions can be used.
Xpath |
Meaning |
<Request.RequestAuthorization> |
RequestAuthorization is a boolean type attribute. The expression is true or a false and can be interpreted as: Has the request been authorized? |
<Request.Clients.Age> |
Age is a integer type attribute. Then the expression is the client's age |
<Request.TotalValueRequested> |
TotalValueRequested is a currency type attribute. The expression is the total amount of a Request |
To access information in the entities that have a one-to-many relationship, it is necessary to use the relationship name, or Fact. If there is a one-to-many relationship, the XPath expression will represent a set of values, that is a collection will be returned.
Remember Guarantees is the name of the fact between the Request and the Guarantees.
Xpath |
Meaning |
<Request.Guarantees> |
Set of guarantees of the request |
<Request.Guarantees.GuaranteedValue> |
Set of values of all the guarantees of the request |
<Request.Guarantees.Type.Code> |
Code of all of the guarantees of the request |
Accessing Collections with XPath
To access information in the entities that have a one-to-many relationship, it is necessary to use the relationship name, or Fact. The name of the relationship is used to navigate from the process entity to the collection. If there is a one-to-many relationship, the XPath expression will represent a set of values.
The relationship's name is the one shown in the arrow connecting the two entities in the diagram: “Guarantees”, and it is used to access the collection. In this case access all the Guarantees of the Request.
ExampleVariable =<Request.Guarantees>
Obtains all the guarantees of the request.
ExampleVariable =<Request.Guarantees.GuaranteedValue>
In addition, the attribute GaranteedValue was specified, which will return a collection of values of the Guarantees of the Request. Thus ExampleVariable will store a collection of all the GuaranteedValues of the Guarantees related to the Request.
Filtering collections using XPath
Collections can be filtered to obtain a smaller set of values to be manipulated. Filters are a powerful tool that restrict the number of elements that are represented in an XPath Expression. Filter
<BusineesEntity.Fact[Filter]>
Filters are written between square brackets: '[' and ']'
When filtering a collection, the attributes inside the brackets belong to the collection entity. Again you can navigate through them using a period and assign conditions like greater than or equal to, to obtain the desired set of values.
The follwoing are examples of filters in our Guarantees collection:
Expression |
Meaning |
<Request.Guarantees[Type.Type= 'Mortgage']> |
This is a set of the Guarantees with Mortgage type |
<Request.Guarantees[GuaranteedValue > 10000 AND GuaranteedValue < 15000]> |
This is a set of guarantees with values between 10000 and 15000 |
Note: In XPath expressions the attribute "id" is used to compare values against attribute identifiers, or foreign keys. The CORRECT filter compares the value of the identifier with a number <Request.Guarantees[Type.id= 1]> |
Masive value assignation: to assign a value to several items of a collection
<Request.Guarantees[Type.Type = 'Mortgage'].Value> = 5000
All Mortgage guarantees will have the value 5000
Please refer to FILTERS for more information and examples about filtering collections
Please refer to Xpath Functions for more information about Xpath calculation
getXPath and setXpath functions
Besides from the diamond brakets, you can also navigate through the data model to obtain of set values, using Xpath with the following functions (Xpath should go in double quotes):
Me.getXPath("XPath")
'Me.setXPath('"XPath",value)
You can use < > diamond brackets or the mentioned functions indistinctly.
The following expressions are exactly the same:
ExampleVariable = <Request.Clients.Age>
ExampleVariable = Me.getXPath("Request.Clients.Age")
<Request.Clients.Age> = ExampleVariable
Me.setXPath("Request.Clients.Age",ExampleVariable)
Using getXpath and setXPath in collections
When collections have to be filtered using variables it is NECESSARY to use setXPath and getXPath functions. In those filters the diamond brackets cannot be used '< >' .
The following is how a filter with variables should be written:
Me.getXPath("BusinessEntity.Fact[AttributeName= "+ variable +"]")
Using an example mentioned above, if the type of guarantee was stored in a variable, the massive assinment of a value to all the Guarantees of that type would be:
var GuaranteeType = 'Mortgage'
Me.setXPath("Request.Guarantees[Type.Type = "+ GuaranteeType +"].Value",5000)
Please refer to FILTERS for more information and examples about filtering collections
Add elements to a relationship
In order to create new rows in a grid (insert records in a collection) within a business rule, Bizagi offers a function called "addRelation" in the Me object.
For example, to add a new record to the entity GuaranteeRequest, the following expression can be included in a business rule:
Me.addRelation("Request.Guarantees");
The line above will create a new GuaranteeRequest row. Once the new row is created, the values for the GuaranteeRequest entity's attributes can be set:
var newGuarantee= Me.addRelation("idRequest.Guarantees");
newGuarantee.setXPath("Value",5000);
The following are the elements of the addRelation function :
Element |
Description |
newGuarantee |
Variable that has the new guarantee |
Me |
Context for the addRelation. When Me is used, the Xpath expression begins from the application entity. |
idRequest.Guarantees |
XPath of the relationship over which the element is added. Guarantees is the name of the Fact that relates the entity Request and the entity Guarantees, in the 1:N relationship |
Note: As shown in the example above, notice that you use .setXPath and .getXPath functions to set or retrieve values for the new record. For example, you may retrieve the new record's automatically assigned Id (surrogate key) by doing newGuarantee.getXPath("id"); |
The Me.addRelation function can also be used to add elements to a multiple-to-multiple relationship, identifying the foreign key of the element to add:
var newGuarantee = Me.addRelation(“Request.Guarantees”, 18);
In this case, the guarantee 18 will be added to the Request_GuaranteeReq relationship.
Example: In a Purchase Request process, a user creates the request and then the boss has to approve it. The request might need changes, be approved or rejected. The Approval Observations are a collection, related to the Request. A record of this approval has to be created for the boss to enter observations. |
Example: In an Inventory process all products have to be uploaded from a Parameter entity table to a table with a one-to-multiple relationship in the Inventory case. |
The follwoing is the data model of the example. Notice that Inventory is the Main Process Entity and has a one-to-many relationship with Products. How ever, the name of the fact is Office Supplies.
1. Add an Expression to the On Enter Action of the first activity of the process.
2. Create the Expression.
The Parameter entity must be called using CEntityManager, saving the array in a variable.This is done because when the case is created there is no relation between the case data and the entity that holds all the information of the products.
When all the products are saved in the variable Products, go through the array adding every product to the relationship.
This is done using the addRelation function.
Finally, add the Product to the relationship using the setXPath function. The object of the product (ProductID) must be set into the attribute that relates the Product to the Parameter Entity: Supply.
3. This is how the user interface will look like
Advanced information
If the user is working with collections and it is necessary to assign a specific value to only one record of the set, in this case to a guarantee, select the record to assgin the value to (nth record).
Assume there is a variable var TotalRows that stores the total number of records the collection has.
- The following expression access the nth record, in this case the TotalRows record. The user must send the nth-1
This expression works with a collection where the first record is the row 0(zero).
Guarantees.get(TotalRows-1).setXPath("Value", 30000)
- The following expression access the nth record by filtering the collection. In this case, this expression access the first record as the row 1 (one). The user must send the nth record, not the nth -1 as the previous expression
Me.setXPath("Request.Guarantees["+TotalRows+"].Value",30000)
Related Articles
<comments />