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.