XPath

Last modified: June 28, 2023

1 Introduction

Mendix XPath is one of the Mendix query languages designed to retrieve data. XPath uses path expressions to select data of Mendix objects and their attributes or associations.

XPath queries can be written in Studio Pro, for example when you want to specify a constraint on the data retrieved in a Retrieve microflow activity. For examples on how XPath queries are used in Studio Pro, see XPath Constraints.

XPath queries can also be used directly in code in the .java files of your Java actions. Examples of complete XPath queries in Java code are:

  • //Sales.Customer Retrieve all customers.
  • //Sales.Customer[Name='Jansen'] Retrieve all customers with name ‘Jansen’.
  • avg(//Sales.Order[IsPaid = true()]/TotalPrice) Retrieve the average of the total prices of all paid orders.

2 XPath Elements

A common XPath query consists of several elements.

A B C D
Aggregate function (optional) Entity to retrieve (required) Constraint (optional) Attribute to retrieve (optional)
avg //Sales.Order [IsPaid = true()] /TotalPrice

Element B describes the core of each query and consists of a description of the object being retrieved. This segment always starts with two forward slashes // and includes the name of the entity you wish to access preceded by the module containing the entity separated by a period. For example, //Sales.Order would return all objects of entity Order in the module Sales.

Element C of a query is optional and contains one or more constraints to restrict the data being retrieved. Consider the following complete XPath query:

//Sales.Customer[Name='Jansen']

The constraint is clearly visible between brackets and restricts the objects retrieved to those for which the attribute Name equals Jansen. Objects with any other name than Jansen are excluded from the list. The number of possible constraints on a single query is unlimited. For more information on how to add and manipulate these constraints, see XPath Constraints.

Element D of a query is optional and specifies an attribute of the retrieved entity. This option is rarely used in Studio Pro itself as all data is stored in objects, making it cumbersome and needlessly complicated to deal with a list of single attribute. However, various Java actions have use of such lists. Also, this functionality can be used in conjunction with element A to create aggregates of certain attributes easily.

Element A of a query is optional and specifies an aggregation. Element A can be one of the following functions: avg, count, max, min and sum. With the exception of count, each of these functions requires that a particular attribute is specified in element D.

3 Tokens

For details, see XPath Tokens.

4 Operators

For details, see XPath Operators.

5 Functions

There are two function types. XPath aggregate functions are for use in Java code only and must contain full queries as their arguments. XPath constraint functions can be used both in Java code and in Studio Pro. In Studio Pro, you do not write complete queries, only the constraints.

For details, see XPath aggregate functions and XPath constraint functions.

6 Example

How to find the right path to XPath

7 Read More