- 3 minutes to read

Formula - XPath

The xPath Formula function returns the values in XML content that match an XPath 1.0 expression.

Note

xPath reads XML with a forward-only reader and supports the XPath 1.0 subset listed in Allowed XPath expressions. For XPath 2.0 syntax, use xPath2.

Syntax

xPath('expression', content)

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

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

Example 1: Read an element from the body

Input

<Id>3</Id>

Formula

xPath('/Id', body)

Result

3

Formula xPath expression returning 3 from an XML message body in the Test Expression tab
/Id selects the root element Id and returns its text.

Example 2: Read all order IDs from base64-encoded XML

Input

PE9yZGVycz4KICA8SWQ+MzwvSWQ+CiAgPElkPjY8L0lkPgo8L09yZGVycz4=

The body decodes to:

<Orders>
  <Id>3</Id>
  <Id>6</Id>
</Orders>

Formula

xPath('/Orders/Id', base64decode(body))

Result

3
6

Formula xPath over base64decode of the body returning the order IDs 3 and 6 in the Test Expression tab
base64decode turns the body into XML first; xPath then returns both Id values.

Example 3: Read XML stored in a context property

Input

Key Value
id <Id>3</Id>

Formula

xpath('/Id',context('id'))

Result

3

Test Expression tab: xpath('/Id',context('id')) reads the XML in the id context property and returns 3 (7.10)
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

Input

<Root>
  <Data Data="SENDERID:13"/>
  <Data Data="VATNO:SE19840410-1337"/>
  <Data Data="RECEIVERID:37"/>
</Root>

Formula

replace('VATNO:', '', xPath('/Root/Data[contains(@Data, ''VATNO'')]/@Data', body))

Result

SE19840410-1337

The 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.

Formula replace around xPath returning the VAT number SE19840410-1337 from a Data attribute in the Test Expression tab
Of the three Data attributes, only the VATNO one is selected, and replace strips its prefix to leave the bare VAT number.

Notes

Next Steps