Wiki source code of Xpath Expert

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

Hide last authors
Erik Bakker 2.1 1 {{container}}{{container layoutStyle="columns"}}(((
Carlijn Kokkeler 17.1 2 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.
eMagiz 1.1 3
Erik Bakker 2.1 4 Should you have any questions, please get in touch with [[academy@emagiz.com>>mailto:academy@emagiz.com]].
eMagiz 1.1 5
6 == 1. Prerequisites ==
7
Erik Bakker 6.1 8 * Expert knowledge of the eMagiz platform
Erik Bakker 9.1 9 * [[XPath Basic>>doc:Main.eMagiz Academy.Microlearnings.Crash Course.Crash Course Platform.crashcourse-platform-create-transformation-xpath-basic||target="blank"]]
10 * [[XPath Intermediate>>doc:Main.eMagiz Academy.Microlearnings.Intermediate Level.Create your transformations.intermediate-create-your-transformations-xpath-intermediate||target="blank"]]
11 * [[XPath Advanced>>doc:Main.eMagiz Academy.Microlearnings.Advanced Level.Create your transformations.advanced-create-your-transformations-xpath-advanced||target="blank"]]
eMagiz 1.1 12
13
14 == 2. Key concepts ==
15
Erik Bakker 7.1 16 This microlearning focuses on very complex XPath operations.
eMagiz 1.1 17
Erik Bakker 7.1 18 With XPath Expert, we mean learning that XPath options are sometimes very complex but could benefit you in specific cases in your daily work.
eMagiz 1.1 19
Erik Bakker 7.1 20 Some of the very complex XPath options are:
eMagiz 1.1 21
Erik Bakker 7.1 22 * matches
23 * replace
24 * tokenize
eMagiz 1.1 25
Erik Bakker 7.1 26 == 3. XPath Expert ==
eMagiz 1.1 27
Erik Bakker 10.1 28 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>>doc:Main.eMagiz Academy.Microlearnings.Crash Course.Crash Course Platform.crashcourse-platform-create-transformation-xpath-basic||target="blank"]]. In the intermediate microlearning on this subject, we built upon that knowledge. Please check out this [[microlearning>>doc:Main.eMagiz Academy.Microlearnings.Intermediate Level.Create your transformations.intermediate-create-your-transformations-xpath-intermediate||target="blank"]] if you need a refresher on that. In the [[microlearning>>doc:Main.eMagiz Academy.Microlearnings.Advanced Level.Create your transformations.advanced-create-your-transformations-xpath-advanced||target="blank"]] 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.
eMagiz 1.1 29
Erik Bakker 7.1 30 Some of the very complex XPath options are:
eMagiz 1.1 31
Eva Torken 12.1 32 * Matches
33 * Replace
34 * Tokenize
eMagiz 1.1 35
Eva Torken 12.1 36 === 3.1 Matches ===
eMagiz 1.1 37
Erik Bakker 7.1 38 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.
eMagiz 1.1 39
Erik Bakker 7.1 40 matches(OrderID,'^\d{7}$')
eMagiz 1.1 41
Erik Bakker 7.1 42 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.
eMagiz 1.1 43
Eva Torken 12.1 44 === 3.2 Replace ===
eMagiz 1.1 45
Erik Bakker 7.1 46 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.
eMagiz 1.1 47
Erik Bakker 7.1 48 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.
eMagiz 1.1 49
Erik Bakker 7.1 50 replace(OrderID,'\D','')
eMagiz 1.1 51
Erik Bakker 7.1 52 With a given input string of 12C34A567, the returned result will be 1234567.
eMagiz 1.1 53
Eva Torken 12.1 54 === 3.3 Tokenize ===
eMagiz 1.1 55
Erik Bakker 7.1 56 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.
eMagiz 1.1 57
Erik Bakker 7.1 58 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.
eMagiz 1.1 59
Erik Bakker 7.1 60 tokenize(AddressID,',')
eMagiz 1.1 61
Erik Bakker 7.1 62 The given input detailed above will result in the following output: street housenumber housenumberaddition.
eMagiz 1.1 63
Eva Torken 12.1 64 == 4. Key takeaways ==
eMagiz 1.1 65
Erik Bakker 7.1 66 Some of the very complex XPath options are:
eMagiz 1.1 67
Erik Bakker 7.1 68 * matches
69 * replace
70 * tokenize
eMagiz 1.1 71
Eva Torken 12.1 72 == 5. Suggested Additional Readings ==
eMagiz 1.1 73
Carlijn Kokkeler 13.1 74 * [[Crash Courses (Menu)>>doc:Main.eMagiz Academy.Microlearnings.Crash Course.WebHome||target="blank"]]
75 ** [[Crash Course Platform (Navigation)>>doc:Main.eMagiz Academy.Microlearnings.Crash Course.Crash Course Platform.WebHome||target="blank"]]
76 *** [[Transformation - XPath Basic (Explanation)>>doc:Main.eMagiz Academy.Microlearnings.Crash Course.Crash Course Platform.crashcourse-platform-create-transformation-xpath-basic||target="blank"]]
Carlijn Kokkeler 14.1 77 *** [[Splitting messages (Explanation)>>doc:Main.eMagiz Academy.Microlearnings.Crash Course.Crash Course Platform.crashcourse-platform-create-splitting-messages||target="blank"]]
Carlijn Kokkeler 15.1 78 * [[Intermediate (Menu)>>doc:Main.eMagiz Academy.Microlearnings.Intermediate Level.WebHome||target="blank"]]
79 ** [[Create your transformations (Navigation)>>doc:Main.eMagiz Academy.Microlearnings.Intermediate Level.Create your transformations.WebHome||target="blank"]]
80 *** [[Transformation - XPath Intermediate (Explanation)>>doc:Main.eMagiz Academy.Microlearnings.Intermediate Level.Create your transformations.intermediate-create-your-transformations-xpath-intermediate||target="blank"]]
81 * [[Advanced (Menu)>>doc:Main.eMagiz Academy.Microlearnings.Advanced Level.WebHome||target="blank"]]
82 ** [[Create your transformations (Navigation)>>doc:Main.eMagiz Academy.Microlearnings.Advanced Level.Create your transformations.WebHome||target="blank"]]
83 *** [[XPath Advanced (Explanation)>>doc:Main.eMagiz Academy.Microlearnings.Advanced Level.Create your transformations.advanced-create-your-transformations-xpath-advanced||target="blank"]]
84 * [[Matches (External)>>http://www.xsltfunctions.com/xsl/fn_matches.html||target="blank"]]
85 * [[Replace (External)>>http://www.xsltfunctions.com/xsl/fn_replace.html||target="blank"]]
86 * [[Tokenize (External)>>http://www.xsltfunctions.com/xsl/fn_tokenize.html||target="blank"]]
Carlijn Kokkeler 16.1 87 * [[XPath (Search Result)>>url:https://docs.emagiz.com/bin/view/Main/Search?sort=score&sortOrder=desc&highlight=true&facet=true&r=1&f_space_facet=0%2FMain.&f_type=DOCUMENT&f_locale=en&f_locale=&f_locale=en&text=xpath||target="blank"]]
Eva Torken 12.1 88 )))((({{toc/}}))){{/container}}{{/container}}