Xpath Expert

Last modified by Carlijn Kokkeler on 2024/09/03 12:25

In this microlearning, we delve into some of the more intricate XPath functions that can enhance your daily work with XML data. Building on the foundational concepts covered earlier, we'll explore advanced XPath options, specifically focusing on the matches, replace, and tokenize functions. These powerful tools can help you handle complex scenarios and improve your data manipulation skills in eMagiz. Let's dive into how each function works and how you can apply them effectively in your projects.

Should you have any questions, please get in touch with academy@emagiz.com.

1. Prerequisites

2. Key concepts

This microlearning focuses on very complex XPath operations.

With XPath Expert, we mean learning that XPath options are sometimes very complex but could benefit you in specific cases in your daily work.

Some of the very complex XPath options are:

  • matches
  • replace
  • tokenize

3. XPath Expert

Within the crash course, we already explained XPath conceptually. In that same microlearning, we looked at some more uncomplicated cases of using XPath within your transformation. If you need to brush up on that knowledge, please check out this microlearning. In the intermediate microlearning on this subject, we built upon that knowledge. Please check out this microlearning if you need a refresher on that. In the microlearning that followed, we built upon that knowledge and looked at some concrete, practical examples that could be useful in your project. In this microlearning, we will wrap the concept of XPath by looking at three complex XPath alternatives that are sometimes needed when dealing with messages in eMagiz.

Some of the very complex XPath options are:

  • Matches
  • Replace
  • Tokenize

3.1 Matches

Sometimes, you want to determine whether a specific value within your payload matches a pattern. In those cases, you can use the XPath function called matches. The function will return true if the supplied string matches a given regular expression. So, for example, if you want to check whether your OrderID contains exactly seven digits and nothing else, the following XPath expression will work for your use case.

matches(OrderID,'^\d{7}$')

As a result, you will get a true or false back which you can then use as a filter or within an if-then-else construction.

3.2 Replace

The replace function has many similarities with the matches function. It builds on the premise of the matches function, but instead of returning a true or false, you can state with what you want to replace the matched string. In other words, this function returns a string produced from the input string by replacing any substrings that match a given regular expression with a supplied replacement string.

When we apply this to our earlier example, we can state that when the OrderID contains any non-digit, we will replace this value with nothing. This will lead to the following XPath expression.

replace(OrderID,'\D','')

With a given input string of 12C34A567, the returned result will be 1234567.

3.3 Tokenize

The tokenize function can split a string into multiple entries for you. This is particularly useful when you want to match an input string to a list of possible values that are valid for that string. For example, the function returns a sequence of strings constructed by splitting the input wherever a separator is found; the separator is any substring that matches a given regular expression.

So, for example, when the input string for AddressID is "street,housenumber,housenumberaddition" and you want to tokenize this with the help of the separator, you could use, in this example, the comma between the values as the separator. This will lead to the following XPath expression.

tokenize(AddressID,',')

The given input detailed above will result in the following output: street housenumber housenumberaddition.

4. Key takeaways

Some of the very complex XPath options are:

  • matches
  • replace
  • tokenize

5. Suggested Additional Readings