Vous êtes sur la page 1sur 13

Beginner’s Guide to XSLT

Mapping in SAP PI
MAY 15, 2010 BY RIYAZ

In this article we will learn the basics of XSLT mapping. SAP PI


supports four types of mappings like plain graphical mapping, Java
mapping, ABAP mapping and XSLT mapping. We will understand
when to use XSLT mapping and learn the pros and cons of using
XSLT in SAP PI.

XSLT stands for Extensible Stylesheet Language Transformation. It


is an XML based language for transforming XML documents into
any other formats suitable for browser to display, on the basis of set
of well-defined rules. Using XSLT we can transform XML to XML,
Text, HTML/XHTML, PDF etc.

Basic understanding of XML and XPATH is all you need to write an


XSLT mapping. XPATH is nothing but an expression or path that
points to a particular node or element within an XML document.
Consider the following XML document:

<?xml version=”1.0” encoding=”UTF-8”?>


<root>
<node1>
<e11>element11</e11>
<e12>element12</e12>
</node1>
<node2>
<e21>element21</e21>
<e22>element22</e22>
</node2>
</root>

 XPATH Expression root would select all child nodes of


node root
 /root represents absolute path and would select the node root
 root/node1 would select all node1 elements that are children
of root
 //node1 would select all node1 elements irrespective of where
they are in the document
 root//node1 would select all node1 elements that are
descendent of root
 /root/node1[1] would select first node1 element that is child
of root
 . would select the current node
 .. would select the parent of current node, while
 @ would select attributes

To understand XPATH in further detail, read XPATH Tutorials.

When to use XSLT mapping?


Sometimes it is difficult to produce desired output using graphical
mapping. For example text/html output, sorting or grouping of
records etc. A few situations where XSLT mapping fits well are:

 When the required output is other than XML like text, html or
xhtml
 When data is to be filtered based on certain fields
 When data is to be sorted based on certain field
 When data is to be grouped based on certain field

An example could be – say you are receiving an xml consisting of


list of PO items and you need to sort and group these items as per
the PO numbers, at the same time separating header and item level
information.

Advantages of using XSLT mapping


 XSLT program itself defines its own target structure
 XSLT provides use of number of standard XPath functions that
can be used just like Java UDFs used in graphical mapping
 You can also define your own XSL/XPATH functions
 File content conversion at receiver side can be avoided in case
of text or html output.
 Multiple occurrences of node within tree (source XML) can be
handled easily
 You can easily segregate and group source data from flat
structure to deeply nested structure and vice versa. Achieving
this using graphical mapping is possible, however would take
more development effort.
 XSLT can be used in combination with graphical mapping
 Multi-mapping is also possible using XSLT
 XSLT can be used with ABAP and JAVA Extensions

Disadvantages of using XSLT mapping


 Resultant XML payload can not be viewed in SXMB_MONI if not
in XML format (for service packs < SP14).
 Interface mapping testing does not show proper error
description. Errors in XSLT programs are difficult to trace in PI
but can be easily identified outside PI using browser.
 XSLT mapping requires more memory than mapping classes
generated in Java. So should be avoided when parsing very
large source XML documents.
 XSLT program become lengthier as source structure fields
grows

XSLT Editing and Mapping Tools


To be frank, you can write XSLT code in any text editor like
Notepad and test it using a browser say, Internet Explorer. There
are a couple of tools that will simplify your job –

 Altova XMLSpy – can be used to edit XSLT files


 Altova MapForce – provides graphical interface with drag and
drop tools to create XSLT mapping (just like graphical mapping
editor within IR/ESR)
 Stylus Studio – Provides similar functionality as Altova
MapForce

I will explain the manual method of testing an XSLT program: Open


your source XML file and insert the following line immediately after
the first line so that it looks like below. Change the path of XSL file
accordingly.
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="path-to-xslt-
file.xsl"?>

Now open source xml file in browser. This will show you
transformed output and not the actual source xml file. This method
however will not display the XML tags. You will only see the content
of the XML elements. To see XML tags you will need one of the
software listed above or similar ones.

Basics of XSLT
XSLT is used to transform an XML document into another XML or
other type of document that is recognized by a browser, like HTML
and XHTML. With XSLT you can add/remove elements and
attributes to or from the output file. You can also rearrange and sort
elements, perform tests and make decisions about which elements
to hide and display, and a lot more. Lets understand some
commonly used XSLT elements –

XSLT Transform
The root element that declares the document to be an XSL style
sheet is <xsl: stylesheet> or <xsl: transform>. The correct way to
declare an XSL style sheet according to the W3C XSLT
recommendation is –

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

or

<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

Note: <xsl: stylesheet> and <xsl: transform> are completely


synonymous and either can be used.

XSLT <template>
An XSL style sheet consists of one or more set of rules that are
called templates. Each template contains rules to apply when a
specified node is matched. The <xsl: template> element is used to
build templates.

The match attribute is used to associate a template with an XML


element. The match attribute 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).

XSLT <value-of>
The <xsl:value-of> element can be used to extract the value of an
XML element and add it to the output stream of the transformation.

XSLT <for-each>
The <xsl:for-each> element allows you to do looping in XSLT. The
XSL <xsl:for-each> element can be used to select every XML
element of a specified node-set.

XSLT <sort>
The <xsl:sort> element is used to sort the output. To sort the output,
simply add an <xsl:sort> element inside the <xsl:for-each> element
in the XSL file.

XSLT <if>
The <xsl:if> element is used to put a conditional test against the
content of the XML file. To put a conditional if test against the
content of the XML file, add an <xsl:if> element to the XSL
document.

<xsl:if test="expression">
... ...
some output if the expression is true
... ...
</xsl:if>

XSLT <choose>
The <xsl:choose> element is used in conjunction with <xsl:when>
and <xsl:otherwise> to express multiple conditional tests.
<xsl:choose>
<xsl:when test="expression">
... some output ...
</xsl:when>
<xsl:otherwise>
... some output ....
</xsl:otherwise>
</xsl:choose>

Other XSLT Elements


There are a number of XSLT elements. Please go through this
article for details.

XSLT Examples
W3Schools website has good examples to understand each of
these elements. Study these XSLT Examples to gain better
understanding.

Steps for implementing XSLT mapping in SAP


PI
Define source and target data
types. Design an XSLT mapping
for source to target type using
the various XSLT elements
available. In case it is not
possible to design the XSLT
mapping on our own, use XSLT mapping tool like Stylus Studio to
create the desired XSL file.

Compress the XSL document created above into a .ZIP or .JAR


Archive file. The file name (including path) of the archive must not
be longer than 180 characters. Import the above generated ZIP or
JAR Archive into IR/ESR under Imported Archives.

Specify the name and namespace and other


information. Use the Import Archive option and import
the ZIP/JAR file created previously.

Name of the XSL file would be visible under the Archive Program
section. Save and activate the changes.

Now define the Interface


Mapping. Specify source and
target message interfaces and
click on Read Interfaces option.
Under the Mapping Program
section, select the Type as XSL.

Use the Search Help to specify the XSLT mapping program.


Choose the XSL file imported previously and click OK. Save and
activate the changes.

Following articles discuss some examples of XSLT mapping in SAP


PI –

 SAP PI – Segregation and Regrouping of Data using XSLT


Mapping
 XSLT Mapping – A Simple Example

 Share Now:



XSLT Mapping – A Simple
Example
MAY 16, 2010 BY RIYAZ

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.

Steps required for developing XSLT Mapping


This document gives you clear understanding of XSLT Mapping, which shall convert source
structure to specific target structure.

Below are different ways of achieving this conversion in XI,

1. Message Mapping (Graphical Mapping using Mapping Editor in XI )


2. Java Mapping
3. ABAP Mapping
4. XSLT Mapping
The rest of the document gives you pre-requisites and steps that are necessary for making use
of XSLT Mapping.

Pre-requisites
 XSL – Extensible Style sheet Language
 XSLT – XSL Transformations, the purpose of this is for transformation of XML documents
into other formats like XML, XHTML and etc.
 XPATH – XSLT uses XPATH to find information in an XML document. XPATH is used to
navigate through elements and attributes in XML documents.
Steps required for developing XSLT Mapping
 Create a source data type and a target data type
 Create Message types for the source and target data types.
 Create Message Interfaces includes Inbound Message interface and Outbound Message
interface.
 XSLT Mapping does not require creation of Message mapping, so don’t create any Message
mapping.
 Create an .XSL file which converts source data type into target data type.
 Zip that .xsl file and import it into Integration Repository under Imported Archives.
 In Interface Mapping choose mapping program as XSL and specify this zip program.
(Through search help you will get XSL Mapping programs that you imported under Imported
Archives, select your corresponding XSL Program)
 Test this mapping program by navigating to Test tab.
By having look at above steps you can easily find out that this mapping is no where different from
other mapping programs, here the challenging lies in creating an XSLT file. If you spend couple
of minutes in studying XPATH tutorial you would be in ideal position to create an XSL
Transformation (.xsl extension).

If you still find difficulties in generating XSL Transformation, then you can make use of a tool
“Altova MapForce” which will create XSL file for you.

Steps for creating XSL file using this tool:


1. Open the Alto MapForce, import the source .xml and .xsd file in it
2. Similarly import the target .xml and .xsd in MapForce.
3. These two data files should match with source and target data types in Integration
Repository.
4. Complete the graphical mapping using extensive list of XSLT functions available there.
5. Save the mapping file.
6. Click the XSLT tab. You will have the entire xslt logic there.
7. Copy that content and save it as .xsl file.
8. Zip above .xsl file and import the same into IR under Imported Archives.
Scenario demonstrating usage of XSLT Mapping
Take a business scenario where in business (Source System) sends an outbound delivery to
their Logistics partner for transporting the same. But here the source business is running on SAP
R/3 and corresponding Logistics partner is not on SAP, hence the interpretation of
data/communication is different. Because source system sends delivery information in an IDoc
(which will be handled by message control for delivery (VL01N/02N) and the target Logistics
partner is not aware of these IDoc structures, so in order to provide a communication between
these two we need some interface mechanism which takes IDoc structure and converts it into the
format understandable by the target Logistics provider and vice versa.

Here the target system expects the xml files in CIDX format, so XI system takes IDoc as an input
and converts it to an XML file adhering to CIDX naming standards.
SAP PI Interview Questions and SAP PI Tutorials

SAP PI Interview Questions and SAP PI Tutorials


Important steps to make this scenario:

– Import needed IDoc structure into IR, in our scenario import IDocDESADV.DELVRY03
– Import corresponding XSD structure under External Definitions in IR. Here the
corresponding CIDX message is Load Tender Motor.
– Import ZIP file corresponding to XSLT under Imported Archives in IR, which can
transform source message to target structure.

Objects needed to be developed in IR


 Inbound Message Interface: Create Inbound Message Interface.
 Import XSL ZIP file.
 Provide path to corresponding zip file. Save it.
 Interface Mapping: Provide source interface as DESADV.DELVRY03 and Target interface as
MI_IB_LoadTenderMotor.
 Now specify Mapping Program specific to XSL Type.
 Choose corresponding Archive program from the search help provided under “Name”.
Save it.

 Now activate all the objects that are developed in IR.


Objects that need to be developed in ID
Business System: Create a business system-pointing source SAP R/3 system.
Business Service: Create a business service corresponding to target file system and don’t
forget to assign outbound and inbound interfaces. Communication Channel: Create a
communication channel of type File Adapter under Business service (w.r.t target system) to send
a file to FTP server of target system. Define the target FTP server details as shown in the
following screen:
Sender Agreement: No need as Source system is sending IDOC which is native to XI (but need
to perform necessary steps through transactions IDX1 and IDX2).
Receiver Determination: Configure Receivers for corresponding Sender Service and Outbound
Interface combination.
Interface Determination: Specify corresponding Inbound Interface and Interface Mapping for
combination of Sender Service and Outbound Interface w.r.t specified Receiver in Receiver
Determination.
Receiver Agreement: Create this corresponding to Receiver Business service and inbound
interface combination and specify communication channel, which places file on the target
system’s FTP server.
Altova MapForce to create XSLT
 Open Altova MapForce tool, before this activity, have XSD’s of both Source and Target
structures on local machine.
 Create a new Mapping file
 Now import XSD’s of both Source and Target messages (which are Outbound and Inbound
Messages in XI).
To do this go to Menu Item, Insert-> XML Schema/File
 Give path to Source message XSD (in this scenario xsd is corresponding to IDoc type
DESADV.DELVRY03), after specifying the xsd it will look like as shown below
 In the same way repeat necessary steps for Target structure (in this scenario xsd is
corresponding Load Tender Motor CIDX message)
 Now make use of functions that are available on left side and complete required mapping.
 Once you are done with mapping save it and then go to XSLT tab which is shown below of
the mapping.
 There is required XSLT code, copy that and create an XSL file (extension .xsl) by simply
pasting the above code. Now zip this XSL file and import the same in IR under Imported
Archives.

Vous aimerez peut-être aussi