SAP PI supports four types of mappings like plain graphical mapping, Java mapping, ABAP mapping and XSLT mapping. In the previous article Beginner’s Guide to XSLT Mapping in SAP PI, we learnt the basics of XSLT mapping. We also discussed the advantages and disadvantages of using XSLT in SAP PI. We briefly touched the situations where XSLT mapping can prove to be helpful and appropriate option.
In this article, we will implement a simple XSLT mapping program to understand it better.
Lets say we have a source XML as follows:
and the desired target structure is as follows:
Thus all we need to do is to concatenate first and last name of the person to create a full name along with the birth date at the target. Although this could be easily achieved using plain graphical mapping, I am using this example just to keep it simple enough to understand.
Now let us look at the XSLT mapping program:
<xsl:stylesheet> tag indicates that this is an XSL document
<xsl:template> tag is used to match “/” i.e. entire source document. So the rules within <xsl:template> and </xsl:template> will be used produce the target XML.
<xsl:for-each> tag is used to loop through all occurrences of Person node. And the code within <xsl:for-each> and </xsl:for-each> is executed once for every occurrence of Person node occurring under Persons node.
<xsl:variable> tags are used to store the values of Name and Surname fields from source document.
<xsl:value-of> tags are used to move individual XML field values to the target structure. The function concat() is used to concatenate current values of variables $fname and $lname separated by a space.
Testing the XSLT Mapping
You can test the mapping by compressing the XSL file into a ZIP archive and importing it into IR/ESR. You need to create appropriate IR objects and use the XSLT Program in your interface mapping. I have explained detailed steps of doing this in the article – Beginner’s Guide to XSLT Mapping in SAP PI.
There is another simple way of verifying the functionality of our XSLT program. Open the source XML file and add the following line immediately after the first line.
<?xml-stylesheet type="text/xsl" href="Persons_to_Persons.xsl"?>
Make sure the name of XSL file is correct. Save the file and open it in a browser. If everything goes well you should see the correctly transformed values in the browser. However, you will not be able to see the XML tags in the browser output. To see the XML tags you will need to test the program using one of the XSLT development tools like Altova XMLSpy, Altova MapForce, Stylus Studio etc or you can directly test the program in IR/ESR as explained earlier.
You can download the source XML file and XSLT Mapping program for testing. To download, right-click the download links and choose ‘Save target as…’ or ‘Save link as…’ option.
Hi Riyaz,
Nice article on the basics of XSLT. I would like to understand (and this would be really useful on this blog) how the “context changes” in graphical mapping look like when the same is done using XSLT. This way we can get a deeper understanding if one is familiar with either this in graphical mapping or XSLT.
This along with “split by value” or “remove context”…these functions in XSLT just to understand teh equivelent would be excellent in understanding in the way of comparision.
Please can you add this to your blog or provide examples?
Thanks a lot.
RanJ
RanJ,
Context and Queues generally apply to graphical mapping. If you want to apply split-by-value or similar context based functions in the mapping program, its best to use either graphical or Java mapping.
However, i can think of an analogy between XSLT and context handling – XSL templates can be considered as different contexts. say <xsl:template match=”/”> can be though of as a queue, while other templates say <xsl:template match=”/node1/node2″> may be considered a specific context. You can define the necessary rules within such templates.
If you have had any specific situations where you found implementing these functions through XSLT was a better option, please feel free to share your thoughts as everyone could benefit from it.
RanJ,
Check the new article on XSLT mapping – https://www.riyaz.net/blog/sap-pi-segregation-and-regrouping-of-data-using-xslt-mapping/technology/sap/812/
May be it addresses some aspects of your question.
Hi Riyaz,
In my xslt mapping, one of the XLS file is referring a XML file. Based on the condtion in the XLS file it picks a value from the XML file.
The interface is working fine. But sometimes it is not picking the value from the XML file. But when I test in Message mapping, Interface mapping and in the Test Congiguration I do get the correct result. But when i test in RWB(Runtime Workbench) i’m not getting the correct result.
Many times it happens. Did all the cache refresh, but not use.
Do i have to do something else? This XLST mapping was given my 3rd party.
how xslt and text file can be edited from ir
Open your Imported Archive, switch to change mode. Then select the XSL file you wish to edit and choose Change Program option. This would help you edit the XSL from within IR. However, for major changes, I would recommend editing it using a full-fledged XML editor tool like Altova XMLSpy etc.