Skip to Content

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 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.

To report this post you need to login first.


You must be Logged on to comment or reply to a post.

  1. Jithin R

    My 2 cents :

    1. Use the return as XML option to send the complete XML string as an input to the UDF
    2. Inside the UDF create an XML document based on the input string and store in container so that we reuse it for later calls
    3. Appy the input  XPath on the XML document and return the result back

    I guess no workarounds are required for this and will be supported by SAP because we use the standard “return as XML” option of graphical mapping.

      1. Jithin R

        🙂 100% agree with you , this option was not there in 2006. Thanks for your blog though, actually I started thinking about using XPath in Graphical mapping after reading this blog..


Leave a Reply