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 noderoot
-
/root
represents absolute path and would select the noderoot
-
root/node1
would select allnode1
elements that are children ofroot
-
//node1
would select allnode1
elements irrespective of where they are in the document -
root//node1
would select allnode1
elements that are descendent ofroot
-
/root/node1[1]
would select firstnode1
element that is child ofroot
-
.
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 –