- 5 minutes to read

XPath with RegEx Search Field Expression Plugin

The XPath with RegEx plugin extracts values from XML in the message body in two steps: an XPath 1.0 expression selects an element, and a regular expression extracts the part of its value you need. Use it when the value you want is only part of an element, such as a file name at the end of a path.

  • ✅ Cut a file name, an ID, or a code out of a longer XML value
  • ✅ Treat sub XML as a string lets the RegEx read the markup of a sub-tree, including element names
  • ✅ RegEx groups returns only the capturing groups you choose

When to use it

Your data Use
Plain XML in the message body; you want element or attribute values as they are XPath
Only part of an XML value (a file name in a path, an ID in a formatted string), or tag names inside an XML sub-tree XPath with RegEx (this page)
XML stored as escaped text inside an element of another XML message XPath on Wrapped XPath
XML stored in a context property instead of the body XPath on Message Context
XPath 2.0, or XPath combined with other functions (decode Base64 first, concatenate, compare) Formula with xPath2

For non-XML bodies, use RegEx or RegEx with capturing groups on the whole message; for the other plugins, see Choose a plugin.

Expression syntax

graph LR A["fa:fa-file-code XML message body"] --> B["XPath
selects the element"] B --> C["RegEx to style value
extracts from its value"] C --> D["fa:fa-magnifying-glass-plus Unique values"] style A fill:#2678BD,color:#fff style B fill:#E47528,color:#fff style C fill:#9E59BB,color:#fff style D fill:#93d16d,color:#000

XPath runs first (orange) on the body (blue); the RegEx (purple) runs on each selected value; the matches become the Search Field values (green).

The plugin shows these fields:

Field What goes in it
XPath An XPath 1.0 expression that selects the element, written from the root without a leading / (Orders/Header/FileName). Element names match without namespace prefixes; the supported syntax is listed in Allowed XPath expressions.
Treat sub XML as a string Off: the RegEx runs on the element's text value. On: the RegEx runs on everything from the selected XPath as a string, markup included, so it can match tags and attributes.
RegEx to style value The regular expression that extracts data from the XPath value. Despite the field label, it extracts; it doesn't format the value.
RegEx groups Which capturing groups to return: group numbers or named groups, comma-separated (1,3 or value1,value2). Leave it empty to take all groups.
Global On: keep matching after the first match (Don't return after first match). Off: return only the first match.

Duplicates are collapsed into unique values (How expressions run). Pick the plugin in the Expression Type list when you add or manage a Search Field, and check the result in the Test Expression tab (Test Your Expression).

Expression Type list with XPath with RegEx selected
Selecting XPath with RegEx in the Expression Type list.

Examples

Example 1: Get the file name from a full file path

The header holds the full path of the received file; the Search Field should hold only the file name.

Input

<ns0:Orders xmlns:ns0="Common.Schemas/Nodinite/1.0">
    <Header>
        <FileName>\\nodinitesrv01\ftp\public\INT001\Order\OrderFile_123.xml</FileName>
    </Header>
    <Order>
        <Id>101</Id>
        <Amount>1000</Amount>
        <City>Karlstad</City>
    </Order>
</ns0:Orders>

Expression

Field Value
XPath Orders/Header/FileName
Treat sub XML as a string Off
RegEx to style value ([^\\]+$)

Result

OrderFile_123.xml

XPath returns \\nodinitesrv01\ftp\public\INT001\Order\OrderFile_123.xml; the RegEx matches the characters after the last backslash. The whole match and group 1 are the same here.

Test Expression tab with XPath with RegEx returning OrderFile_123.xml
XPath Orders/Header/FileName plus RegEx ([^\\]+$) returns one unique match, OrderFile_123.xml.

Example 2: List the element names inside a sub-tree

Input

<Envelope>
    <Any>
        <Nodes>
            <node1 id="1">
                <node2/>
            </node1>
        </Nodes>
    </Any>
</Envelope>

Expression

Field Value
XPath Envelope/Any/Nodes
Treat sub XML as a string On
RegEx to style value <([a-z0-9]{0,})
RegEx groups 1
Global On

Result

node1
node2

Nodes contains only elements, so its text value is empty: without Treat sub XML as a string the RegEx has nothing to match. With it on, the RegEx reads the markup. Group 1 returns the name after each <, and Global keeps matching after the first one. The closing tag </node1> gives an empty group, which doesn't appear in the result.

Test Expression tab with XPath with RegEx returning node1 and node2
Treat sub XML as a string, RegEx group 1, and Global turned on: 2 unique matches, node1 and node2.

Limitations

  • The XPath step uses the same forward-only reader as the XPath plugin, so only the syntax in Allowed XPath expressions works.
  • The RegEx runs on the value the XPath selects, never on the rest of the message.

An unsupported expression, or one that matches nothing, gives this result in the test:

Test result: Processed with warning, the provided expression yielded no result
No match: the test reports a warning instead of values.

Next Steps