Formula - XPath
Section titled “ Formula - XPath”The xPath Formula function returns the values in XML content that match an XPath 1.0 expression.

Animated walkthrough: xPath('OrderEntry/OrderHeader/OrderId', body) on the Order Entry XML of the Order-to-Cash demo returns ORD-10005. The XML has a default namespace, and the path matches without a prefix.
Syntax
Section titled “ Syntax”xPath('expression', content)Parameters
Section titled “Parameters”| Parameter | Required | Description |
|---|---|---|
'expression' |
Yes | An XPath 1.0 expression in single quotes, such as '/Orders/Id'. A literal ' inside it is written ''. |
content |
Yes | The XML text: body, context, or the result of another function, such as base64decode (see Formula language rules). |
Returns
Section titled “Returns”The text value of every matching element or attribute, one value each; duplicates are collapsed. When nothing matches, nothing is returned.
Element names match without namespace prefixes or declarations: Orders/Order/Id matches <ns0:Orders xmlns:ns0="…"> (see Formula language rules).
Examples
Section titled “ Examples”Example 1: Read an element from the body
Section titled “Example 1: Read an element from the body”<Id>3</Id>Formula
Section titled “Formula”xPath('/Id', body)Result
Section titled “Result”3
/Id selects the root element Id and returns its text.
Example 2: Read all order IDs from base64-encoded XML
Section titled “Example 2: Read all order IDs from base64-encoded XML”PE9yZGVycz4KICA8SWQ+MzwvSWQ+CiAgPElkPjY8L0lkPgo8L09yZGVycz4=The body decodes to:
<Orders> <Id>3</Id> <Id>6</Id></Orders>Formula
Section titled “Formula”xPath('/Orders/Id', base64decode(body))Result
Section titled “Result”36
base64decode turns the body into XML first; xPath then returns both Id values.
Example 3: Read XML stored in a context property
Section titled “Example 3: Read XML stored in a context property”| Key | Value |
|---|---|
| id | <Id>3</Id> |
Formula
Section titled “Formula”xpath('/Id',context('id'))Result
Section titled “Result”3
The XML in the id context property is queried like a message body.
Example 4: Pick an attribute by its content and clean it up
Section titled “Example 4: Pick an attribute by its content and clean it up”<Root> <Data Data="SENDERID:13"/> <Data Data="VATNO:SE19840410-1337"/> <Data Data="RECEIVERID:37"/></Root>Formula
Section titled “Formula”replace('VATNO:', '', xPath('/Root/Data[contains(@Data, ''VATNO'')]/@Data', body))Result
Section titled “Result”SE19840410-1337The predicate keeps the Data element whose attribute contains VATNO; the doubled ''VATNO'' is the escaped single quote inside the quoted XPath, so XPath receives 'VATNO'. replace then removes the VATNO: prefix.

Of the three Data attributes, only the VATNO one is selected, and replace strips its prefix to leave the bare VAT number.
- Expressions that look back, such as
..orparent::, return no values, and the test bench shows the warning xpath is not supported!; see Allowed XPath expressions for what the forward-only reader supports. - A predicate on a child element, such as
Orders/Order[Id=2]/City, isn't supported either: the test bench shows Can't get the child value. An attribute test such as[contains(@Data, ''VATNO'')]works (Example 4). - A leading
//, such as//Id, returns nothing. Write the path from the root. xPathis the better choice for large XML; use xPath2 only when you need what the forward-only reader refuses.- For one XPath on the body, the XPath plugin does the same without a formula; for XML in a context value, use the XPath on Message Context plugin. See Choose a plugin.