XSLT Mapping in SAP PI 7.1 PDF
XSLT Mapping in SAP PI 7.1 PDF
Applies to:
SAP NetWeaver Process Integration 7.1 (SAP PI 7.1)
Summary
This document explains about using XSLT mapping in SAP Process Integration for converting a simple input
to a relatively complex output. It explains the complete process of preparing a .xsl file and then importing it to
PI.
Authors: Amit Srivastava, Anshul Chowdhary
Company: Infosys Technologies Limited
Created on: 13 August 2010
Author Bio
Amit Srivastava is working as a Senior Software Engineer on SAP XI/PI. He began his career
on Nov-2007 and since then he has been working on SAP XI/PI. His area of expertise is SAP
XI/PI.
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 1
XSLT Mapping in SAP PI 7.1
Table of Contents
A Basic Overview on XSLT................................................................................................................................. 3
Basic XSLT Tags ............................................................................................................................................ 5
XPATH Functions in XSLT Mapping: .............................................................................................................. 9
How to Use an XSLT Mapping in PI ................................................................................................................. 10
Example 1 ..................................................................................................................................................... 11
Example 2 ..................................................................................................................................................... 15
References........................................................................................................................................................ 20
Disclaimer and Liability Notice .......................................................................................................................... 21
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 2
XSLT Mapping in SAP PI 7.1
Now as we have the source and the target with us we can develop an XSLT mapping between them using
any of the XML editors or even a note pad.
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 3
XSLT Mapping in SAP PI 7.1
ns0:MT_XSLT_Source/Person/LastName)"/>
</Name>
<Street>
<xsl:value-of
select="concat(concat(ns0:Mt_XSLT_Source/Person/Address/Houseno
,' '),
ns0:Mt_XSLT_Source/Pers
on/Address/Street)"/>
</Street>
<City>
<xsl:value-of
select="ns0:Mt_XSLT_Source/Person/Address/City"/>
</City>
</ns1:MT_XSLT_Target>
</xsl:template>
</xsl:stylesheet>
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 4
XSLT Mapping in SAP PI 7.1
1) <xsl:stylesheet> or <xsl:transform> :
<xsl:stylesheet> or <xsl:transform> are the root elements that declare the document to be an XSL style
sheet. Either of the two elements can be used as root elements as they are synonymous.
EG: <xsl:stylesheet version=”1.0” xmlns:xsl=http://www.w3.org/1999/XSL/Transform>
The xmlns:xsl="http://www.w3.org/1999/XSL/Transform" points to the official W3C XSLT namespace. If you
use this namespace, you must also include the attribute version="1.0".
2) <xsl:template> :
An XSL style sheet contains one or more set of rules that are called templates.A template contains rules to
apply when a specified node is matched.The “match” attribute is used to associate a template with an XML
element or it can also be used to define a template for the entire XML document. The value of the match
attribute is an XPath expression (i.e. match="/" defines the whole document).
3) <xsl:value-of> :
The <xsl:value-of> element is used to extract the value of a selected node.The value of the select attribute is
an XPath expression. An XPath is used for defining parts of an XML document. An XPath expression works
like navigating a file system where a forward slash (/) selects subdirectories.
4) <xsl:for-each> :
The <xsl:for-each> element is used to loop in XSLT. The value of the select attribute is an XPath expression.
For example in our above example if we had multiple person data at the source then we could have used
<xsl:for-each element> as shown below:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://XYZ.com/Gen"
Xmlns:ns1=”http://XYZ.com/Test”>
<xsl:template match="/">
<ns1:MT_XSLT_Target>
<xsl:for-each select=”ns0:MT_XSLT_Source/Person”>
<Title>
<xsl:value-of select="Gender"/>
</Title>
<Name>
<xsl:value-of select="concat(concat(FirstName,' '),
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 5
XSLT Mapping in SAP PI 7.1
LastName)"/>
</Name>
<Street>
<xsl:value-of select="concat(concat(Address/Houseno,'
'),
Address/Street)"/>
</Street>
<City>
<xsl:value-of select="Address/City"/>
</City>
</xsl:for-each>
</ns1:MT_XSLT_Target>
</xsl:template>
</xsl:stylesheet>
We can also filter the output from the XML file by adding a criterion to the select attribute of <xsl:for-each>
element.
EG: <xsl:for-each select="ns0:MT_XSLT_Source/Person[FirstName='Anshul']">
5)<xsl:sort> :
The <xsl:sort> element is used to sort the output.
Example:
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 6
XSLT Mapping in SAP PI 7.1
LastName)"/>
</Name>
<Street>
<xsl:value-of
select="concat(concat(Address/Houseno,' '),
Address/Street)"/>
</Street>
<City>
<xsl:value-of select="Address/City"/>
</City>
</xsl:sort>
</xsl:for-each>
</ns1:MT_XSLT_Target>
</xsl:template>
</xsl:stylesheet>
The select attribute indicates what XML element to sort on. In the above example it will display the output
based upon sorting the “Names” .
6) <xsl:if> :
The <xsl:if> element is used to put a conditional test against the content of the XML file. The value of the
required test attribute contains the expression to be evaluated.
Syntax: < xsl: if test=”expression”>
</xsl:if>
Example:
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 7
XSLT Mapping in SAP PI 7.1
select="concat(concat(Address/Houseno,' '),
Address/Street)"/>
</Street>
<City>
<xsl:value-of select="Address/City"/>
</City>
</xsl:if>
</xsl:for-each>
</ns1:MT_XSLT_Target>
</xsl:template>
</xsl:stylesheet>
The above code will only output those person details which have “male” as gender .
7) <xsl:choose> :
The <xsl:choose> element is used to handle condition based tests. Multiple conditions are expressed with
the help of <xsl:when> and <xsl:otherwise> elements.
Syntax/EG:
<xsl:choose>
<xsl:when test=" Gender =Male ">
... some processing logic inside ...
</xsl:when>
<xsl:otherwise>
... some processing logic inside....
</xsl:otherwise>
</xsl:choose>
Choose condition will come just above the element in the XSL where the condition needs to be implied.
8) <xsl:apply-templates> :
The <xsl:apply-templates> element applies a template to the current element or to the current element's child
nodes. If we add a select attribute to the <xsl:apply-templates> element it will process only the child element
that matches the value of the attribute. We can use the select attribute to specify the order in which the child
nodes are processed.
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 8
XSLT Mapping in SAP PI 7.1
2) translate()
The translate function takes the input string in the value argument of the syntax as shown below and
substitutes all occurrences of a string specified in the string1 argument with that mentioned in string2
argument.
Syntax: translate("Anshul chowdhary","abcdefghijklmnopqrstuvwxyz","
ABCDEFGHIJKLMNOPQRSTUVWXYZ")
Output:”ANSHUL CHOWDHARY”
3) string()
The string function converts the input to a string.
Syntax: string("Anshul chowdhary")
4) concat()
The concat function takes all input arguments individually and concatenates them together in the specified
order.
Syntax: concat("anshul","chowdhary")
Output:”anshulchowdhary”
5) sum()
The sum function converts PCDATA text to a numeric value
Syntax: sum(p2:marks/score)
6) count()
This function is used to count the nodes
Syntax: count(p2:marks/subjects) .
The use of the above XPATH Functions is explained in an example below, but before going into that we
should know how to use XSLT Mapping in PI.
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 9
XSLT Mapping in SAP PI 7.1
Basic Steps- There are some basics steps required for using XSLT mapping in PI. Those steps are -
STEP 4: XSLT Mapping does not require creation of Message mapping as the .XSL file is directly imported
to the Operations Mapping.
STEP 5: Create a .XSL file which contains the logic for converting source data type to target data type.
STEP 6: Zip the developed .xsl file and import it into Enterprise Services Builder under Imported Archives.
STEP 7: In Operation Mapping choose mapping program as XSL and specify this zip program. (When one
chooses the Type as XSL, in search help all XSL Mapping programs that are imported under Imported
Archives of the particular namespace gets listed for selection)
STEP 8: Test the mapping program imported by moving to the Test tab.
Based upon the above mentioned steps, a few scenarios have been configured in PI as shown below. While
explaining the examples it has been assumed that the user has basic knowledge of interface creation in PI
7.1.
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 10
XSLT Mapping in SAP PI 7.1
Example 1
Creating flat structure from a complex message.
The source structure is as shown in the outbound datatype below.
The inbound datatype is as mentioned below. This is the desired target structure.
For the above two data types, prepare the message types, service interfaces etc. Message Mapping will not
exist for interfaces which use XSLT mapping. The XSLT mapping is required to be specified in Operation
Mapping. To achieve this, .XSL file is transported to Imported archives in the form of a ZIP file as shown
below.
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 11
XSLT Mapping in SAP PI 7.1
In the ESR go to the desired namespace and right click on Imported Archive. Select New as shown below.
The below screen pops up when the name of the xsl file is entered after clicking on New. The file is imported
by clicking in the Import Archive button as shown below.
When Import Archive button is pressed, a browser window open up from where the desired xsl mapping is
chosen.
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 12
XSLT Mapping in SAP PI 7.1
</Gender>
<Name>
<xsl:value-of select="concat(concat(FirstName,'
'),LastName)" />
</Name>
<Street>
<xsl:value-of select="Address/Street" />
</Street>
<HouseAddress>
<xsl:value-of
select="concat(concat(Address/Houseno,','),Address/City)" />
</HouseAddress>
</Details>
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 13
XSLT Mapping in SAP PI 7.1
</xsl:for-each>
</ns1:MT_XSLT_Inbound>
</xsl:template>
</xsl:stylesheet>
In Operation Mapping select XSL as type under Mapping Program and press the search button in the Name
section. The entire XSL mapping imported gets listed from where the desired mapping is chosen.
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 14
XSLT Mapping in SAP PI 7.1
Example 2
There is one common problem in JDBC scenarios that whenever SP name changes at target side , we need
to redo our graphical message mapping . So, in order to get rid of that we can use XSLT mapping. You just
need to change the SP name in the XSL sheet . The scenario is as mentioned below:
The below given are the outbound and the inbound datatypes.
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 15
XSLT Mapping in SAP PI 7.1
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 16
XSLT Mapping in SAP PI 7.1
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 17
XSLT Mapping in SAP PI 7.1
Then the mapping is used in Operation Mapping to get the desired result.
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 18
XSLT Mapping in SAP PI 7.1
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 19
XSLT Mapping in SAP PI 7.1
References
XSLT Tutorial - w3schools.com
Blog: xpath functions in xslt mapping
XSL Transformations (XSLT) Version 2.0 - w3.org
XSLT Mapping for SAP PI 7.1 on help.sap.com
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 20
XSLT Mapping in SAP PI 7.1
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 21