crashcourse-platform-create-transformation-xpath-basic
Version 16.1 by Erik Bakker on 2022/06/10 08:38
Transformation * XPath Basic
Sometimes the transformation tooling does not provide you with the exact correct transformation option to get the desired result in your output. For those cases, you can use a custom (handwritten) XPath expression to achieve the desired result.In this microlearning, we will educate you on the basics of XPath and see how we can use it within the context of transforming messages. In other microlearnings, we will discuss the use of XPath outside the context of transforming messages.Should you have any questions, please contact academy@emagiz.com.- Last update: November 8th, 2021
- Required reading time: 9 minutes
1. Prerequisites
- Basic knowledge of the eMagiz platform
2. Key concepts
This microlearning focuses on XPath Basic in the context of transformations.With XPath Basic, we mean: Understanding on a fundamental level what XPath is, how it is used and how you can use it within the transformation tooling of eMagiz3. Transformation * XPath Basic
Sometimes the transformation tooling does not provide you with the exact correct transformation option to get the desired result in your output. For those cases, you can use a custom (handwritten) XPath expression to achieve the desired result.3.1 What is XPath
Before we delve into the use of XPath within the eMagiz tooling let us first discuss XPath itself. XPath stands for XML Path Language. As the name suggests it can be used to write down a certain "path" to identify and navigate nodes in an XML message. By following this "path" you can access all elements and attributes within your input XML message.XPath is a widely used standard with a lot of built-in functions and is a W3C recommendation3.2 Reading and Writing XPath
The simplest XPath is /. This simply means access the root of the input message. So to access the root of your input message you write down one forward-slash (/). If you want to access an element below the root directly you can use two forward slashes () to start your XPath expression.For example, take a look at the following input message:- /Projects/Project/ID
- Project/ID
- ID
3.3 Absolute vs Relative XPath
As discussed in the previous segment there is a choice to be made between using an Absolute XPath (which starts at the root level) and the Relative XPath (which can start anywhere in the structure)Below you find a summary of the main differences between both options.Absolute XPath- It is the direct way to find the element
- Disadvantage of the absolute XPath is that if there are any changes made in the path of the element then that XPath gets failed.
- Starts with the single forward-slash(/), which means you can select the element from the root node.
- Finds the element(s) in the whole message, not considering the structure.
- Starts with the double forward-slash (), which means it can search the element anywhere at the message.
3.4 Namespaces
To complete things a little bit more we are now going to discuss namespaces. A namespace is a set of symbols that are used to organize objects of various kinds, so that these objects may be referred to by name.An example of comparative nature would be: There are in the Netherlands two cities named Hengelo. Via namespaces can we split and recognize them. The namespace that can be used to split them is using the namespace Province (Gelderland and Overijssel). Gelderland:Hengelo & Overijssel:HengeloTo handle namespaces while reading and writing XPath you have two options:- Prefix
- Wildcard
3.4.1 Prefix
By defining the prefix of the namespace (i.e. sys, cdm, ns) you can refer to this prefix while reading and writing your XPath. Let's return to our original example, only this time the input message has a namespace:- /sys:Projects
- /sys:Projects/sys:Project
3.4.2 Wildcard
By using the wildcard notation, an asterisk (\*), you specify that regardless of the chosen prefix by the party for delivering the input message you will accept it. Using the prefix makes it clearer to others in which namespaces the XPath is written. Using the wildcard is easier as you don't have to check for every XPath you write what the prefix is and whether there is a namespace. Therefore we see a lot of use of the wildcard when writing a custom XPath in eMagiz.Using the wildcard will result in the following valid XPath options:3.5 Custom XPath in Transformation
Now that we have a basic conceptual understanding of XPath let us turn our attention towards relating this information to eMagiz. Specifically how you can use it while transforming your messages with the help of the transformation functionality in Create.As you saw in the previous microlearning a lot of options are already available out of the box and don't require you to write your custom XPath. However, sometimes it is necessary to write a custom XPath.Let us look at a example in eMagiz:- The DateTime should be filled with the DateTime value related to the first OrderLine
- The Description should be the value for ID and the value for DateTime merged with the help of a dash (-) icon.
- OrderLine/DateTime
- [1] = the first iteration of OrderLine
- [2] = the second iteration of OrderLine
- etc.
- DateTime|ID
Practice
4. Assignment
Open a flow that makes use of the transformation tooling and enters two custom XPath expression to achieve the following:- A element on the root level should be filled with a value related to the first iteration of a list in your input message
- The desired output for a field called Description should be the input values of two other elements merged via a hashtag sign This assignment can be completed within the (Academy) project that you have created/used in the previous assignment.
5. Key takeaways
- XPath gives you the option to navigate through an XML document in a "path" like manner
- There are various ways of setting up your XPath (Absolute vs Relative)
- Consider the namespace
- Within the transformation, the starting point of each XPath depends on where the line on entity level was drawn from
6. Suggested Additional Readings
If you are interested in this topic and want more information on it please read the help text provided by eMagiz and/or read more information on the following link:- https://www.w3schools.com/xml/xpath_intro.asp
- https://www.tutorialspoint.com/xpath/index.htm
- https://www.softwaretestinghelp.com/xml-path-language-xpath-tutorial/
- [XPath - Intermediate](intermediate-create-your-transformations-xpath-intermediate.md)
- [XPath - Advanced](advanced-create-your-transformations-xpath-advanced.md)