Filtering 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.
A filter indicates one or more conditions to be met within a one-to-many (1:N) relationship. Filters are used in expressions to evaluate records of a collection.
These conditions are established in brackets after the relationship name, which identifies the 1:N relationship.
The data model diagram below will be used as a guide to show how the structure of a filter uses XPath.
Imagine a Purchase Request Process whose data model is displayed below. A Purchase Request Process has a Process Entity called **Purchase Request**. The Process Entity has a one-to-many relationship with the entity called **Products Requested for Purchase**.
- To obtain a collection of the requested products where the unit price is greater than 1000, use the expression:
(PurchaseRequest.ProductsRequestedforPurchase (UnitPrice > 1000))
Every purchase request obtains many quotations to choose the best supplier. For this reason, there is a one-to-many relationship, named **Quotation**, between the **Purchase Request** and **Quotation** entities.
- To obtain the selected quotations, use the expression:
<PurchaseRequest.Quotation (Selected = true) >
- To obtain the supplier email address of each selected quote, use the expression:
<PurchaseRequest.Quotation (Selected = true).Supplier.Email>
When you navigate beyond a collection, as in the above example, the XPath expression can either return a collection or merely a number depending on the number of records that meet the filtered condition.
Therefore, the expression will return a collection of emails where more than one supplier was selected.
You can compose a number of conditions in a filter with the logical operators `AND` and `OR`. Within the filter, don't use parentheses.
If parentheses are needed, you must use the function `Me.getXPath("")`.
- To obtain the Products requested that have a quotation related and where it is for more than 1000, we would include the `AND` operator:
<PurchaseRequest.ProductsRequestedforPurchase (QuotationsRequested = true AND UnitPrice > 1000) >
Operators in Filters
Operator | Description | Example |
---|---|---|
= | equal | <PurchaseRequest.Quotation[Selected = true]> |
!= | not equal | <PurchaseRequest.Quotation[Selected != true]> |
> | greater than | <PurchaseRequest.ProductsRequestedforPurchase[TotalPrice > 1000]> |
< | less than | <PurchaseRequest.ProductsRequestedforPurchase[UnitPrice < 1000]> |
>= | greater than or equal to | <PurchaseRequest.Quotation[QuotationDiscount >= 1000]> |
<= | less than or equal to | <PurchaseRequest.Quotation[DiscountPercentaje <= 0.1]> |
AND | and | <PurchaseRequest.Quotation[QuotationDiscount >= 1000 AND DiscountPercentaje <= 0.1]> |
OR | or | <PurchaseRequest.Quotation[QuotationDiscount >= 1000 OR DiscountPercentaje <= 0.1]> |
(empty) | is true (default) | <PurchaseRequest.ProductsRequestedforPurchase[QuotationsRequested]> |
! | is false | <PurchaseRequest.ProductsRequestedforPurchase[!QuotationsRequested]> |
Assign and Obtain Values with XPath Filtered Collections
The easiest way to assign a value or collection to an element is using the =
operator.
var quantity = <PurchaseRequest.ProductsRequested[QuotationRequested = true].Quantity>
In the variable, this XPath expression stores a collection containing the amount of each quotation selected for every requested product related to a purchase.
<PurchaseRequest.Quotation[Discount >= true].Selected> = true
This expression will set the value of the Boolean attribute, Selected, to true for all the quotations that have a discount.
Image Links:
Understandingxpath2
: Add the appropriate link to the image.
Let me know if you need further adjustments!