Skip to content
Advertisement

How to use the XML Tokenize language as a predicate in a Content based Router?

Can we use body().xtokenize as predicate of choice() in Camel pipeline for routing when XML type matches the path we want?

I tried it but doesn’t filter in accordance with the predicate :

This is the code snippet :

 Namespaces ns = new Namespaces("ns1", "http://standards.iso.org/iso/15143/-3");
.choice().when(body().xtokenize("/ns1:Links", 'i', ns)) 

These are the type of XML content that I would like to route according to the name of the root element:

<Links xmlns="http://standards.iso.org/iso/15143/-3">
    <rel>last</rel>
    <href>https://[source domain name]/public/api/aemp/v2/15143/-3/Fleet/Equipment/ID/[equipment
        id]/Locations/2021-01-01T00:00/2022-09-29T12:52:12.519982300/1
    </href>
</Links> 

Or

<Location datetime="2022-04-05T09:52:53Z">
    <Latitude>43.290143</Latitude>
    <Longitude>5.491987</Longitude>
    <Altitude>102.375</Altitude>
    <AltitudeUnits>metre</AltitudeUnits>
</Location>

Thank you for your help.

Advertisement

Answer

The XML Tokenize language is not really meant to be used as a predicate, you should rather use another language like the XPath language for example.

Your Content Based Router could test the local name of the root element to route your messages as next:

.choice()
    .when().xpath("/*[local-name()='Links']")
        .log("Link detected ${body}")
    .when().xpath("/*[local-name()='Location']")
        .log("Location detected ${body}")
    .otherwise()
        .log("Unknown ${body}");
Advertisement