Document toolboxDocument toolbox

XPath

Description

This unit is a Processor type unit.

The XPath unit executes XPath queries against XML documents. The result is provided as a new XML document.

An event comes in through the in port. The expression specified in the properties is executed and the data field content is parsed.

Successfully parsed input events are enriched with a new field containing the xpath result, and the event is output through the out port.

If an error occurs, the input event is enriched with new fields describing the problem, and the event is sent through the error port.

Configuration

After dragging this unit into the Flow canvas, double-click it to access its configuration options. The following table describes the configuration options of this unit:

Tab

Field

Description

Tab

Field

Description

General

Name

Enter a name for the unit. It must start with a letter, and cannot contain spaces. Only letters, numbers, and underscores are allowed.

Description

Enter a description detailing the scope of the unit.

Expression

Enter an XPath valid expression to determine the behavior of the unit.

Result type

The type of the expected result. XML stands for XML String. Java stands for generic java types (Map, List, String, ...).

In field

Enter the name of the input event field containing JSON data.

Out field

Enter a name for the output event field containing the result.

Input ports

Port

Description

Port

Description

in

Events containing a properly formatted XML.

Output ports

Port

Description

Port

Description

out

 Outputs events enriched with an extra field containing the new XML.

error

Signals when an error occurred. Events are input events enriched with standard error fields.

Example

Imagine you receive an XML with a HTTP sender and you wish to extract certain values to send via email. Here, we have received an XML document containing CD information and we wish to extract the year in a new XML using Xpath queries.

For this, we will use an XPath unit to extract certain values and output them.

To simulate the HTTP sender information, we will use a Tick unit. In the Fields tab, add the XML example as a string Type to include in the output event:

<CATALOG> <CD> <TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Columbia</COMPANY> <PRICE>10.90</PRICE> <YEAR>1985</YEAR> </CD> <CD> <TITLE>Hide your heart</TITLE> <ARTIST>Bonnie Tyler</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>CBS Records</COMPANY> <PRICE>9.90</PRICE> <YEAR>1988</YEAR> </CD> </CATALOG>

Next, we add the XPath unit and in the properties specify the XPath expression to execute against the XML, in this case, /CATALOG/CD/YEAR.

You must specify the In field that will receive the data from the Tick unit, and the out field to send output events.

Link the XPath unit to the Tick unit.

Finally, we need to specify the destination to receive the XML containing only information on the years. Add an Email Sink unit and in the To Recipients tab add the email destination. Link the XPath unit to this Email Sink.

After saving and starting the Flow, you must click the red button of the Tick unit to begin sending events.

Download this example

You can try this flow by downloading the following JSON file and uploading it to your domain using the Import option:

Â