Hack: Using XPATH in XI Message Mappings
The Message Mapping tool has some limitations when dealing with complex context selections. Often it is possible to perform a selection based on an XPath expression rather then user defined function. This blog describes how it is possible to add xpath expressions to the mapping.
My colleague Thorsten Søbirk did make a set of UDF to the message mapping, which enabled the use of XPath. The idea in this was to create an internal XML document, which could be used for queries. It was then possible to use the document to perform the XPaths on. Thorsten’s solution required the developer, to create a new XML document with the content from selected queues.
After I saw what Thorsten did, I thought that it must be easier to make it possible to use XPaths. I got to think about a way to grab the original XML document, which would be easier to implement. I looked at the code for the message mapping and found that it extended the AMappingProgram class.
I AMappingProgram there is a method, which is called when a mapping is executed this is: execute(InputStream in, OutputStream out). If found that this could be overwritten it was possible to get the original inputstream. I then had to copy the inputstream; I used one parsing the XML document for XPath evaluation, while the other copy is used for the original execute method.
I added the following to the global variable content area to overwrite the execute method.
Remember to add the imports section like the following.
Then I added the following context user defined function, where it was possible to get access to use the xpath. This generates an entry each time it is executed and pr node.
To use this function supply the function with xpaths expressions for each line. This can be performed with a replace operation, where nnn is replaced with PosNR field in the expression /XPATH_demo/Line[PosNR=’nnn’]/Quantity[Units=’g’]/Value. An example on how this works is the following result.
It should also be possible to use an xpath like /XPATH_demo/Line/Quantity[Units=’g’]/Value. This will result in the following, where there are multi results from a query. Unfortantly this is not correct, since the line two is missing a quantity.
I have used Thorsten’s UDF for background for parsing a document and evaluation of xpaths. Based on those functions changed them, so they could be used with the current setup. I would not go into much into details on how the function works.
The main class has the following content.
To handle exceptions, I have added an exception class.
To evaluate xpath Jaxen was used. Jaxen can be downloaded at http://jaxen.codehaus.org/releases.html. I have not been able to find any documentation on how to use SAP’s XPath evaluation, if it exist please share.
Please note that this is a hack, and it not indented for productive use. This method has two drawbacks; it requires extra memory for storing the document two times extra and there are parsing an extra time for the document. Besides the memory and performance, the function is not supported by SAP and it could mean that the mappings will break eventually.