• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

riyaz.net

Tech Tips and Tutorials for SAP Professionals and Bloggers

  • Home
  • WordPress
  • Tips & Tricks
  • Internet
  • SAP
    • SAP PI
    • SAP ABAP
  • Personal Finance
  • Health & Fitness
  • Travel & Leisure
You are here: Home / SAP / XSLT Mapping in SAP PI – Segregation and Regrouping of Data

XSLT Mapping in SAP PI – Segregation and Regrouping of Data

May 16, 2010 by Riyaz

xslt-mapping

In this article, I describe how to segregate and regroup source XML data using XSLT mapping. Let us understand this using a small example. Lets say we have a flat XML structure consisting of Purchase Order (PO) Items of a couple of Purchase Orders (POs). And our intention is to transform this XML into another XML such that the PO Items are grouped according to their PO numbers and header level and item level data is segregated. Header level data should occur only once and item level data should occur as many times as many number of items exist in a particular PO.

Source XML Structure

xslt mapping

Desired Target Structure

xslt mapping

XSLT Mapping Program

Now let us understand the XSLT Mapping Program. You can download the source code of XSLT mapping program here: POExtract_to_POData.xsl.

xslt mapping

<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.

We store the current PO Number in a variable called currentPONumber using <xsl:variable> tag. <xsl:for-each> checks whether the current and previous PO numbers differ. Only if the numbers differ, the elements will be produced at the output. Thus, the Header data will be produced at the output only once for each PO number.

We have another <xsl:for-each> loop within the existing <xsl:for-each>. This is used to produce item level fields at the output. The <xsl:for-each> tag compares the PO number with the PO number stored in the variable currentPONumber. Only if the values match, the underlying elements will be produced at the output. Thus, item level details will be produced under the current PO row node only if they belong to the current PO.

<xsl:value-of> tags are used to move individual XML field values to the target structure.

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="POExtract_to_POData.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.

Filed Under: SAP Tagged With: SAP PI

About Riyaz

Riyaz Sayyad is the founder of Riz Labs. Learn more about him here.

Primary Sidebar

Popular Guides

  • Dropbox Tutorial
  • CDN Setup Guide
  • Blog Design Tips
  • Optimize RSS Feed
  • Create Twitter App
  • Short URLs
  • Password Protect Folders
  • Time Management
  • ALE IDocs Tutorial
  • SAP PI Starter Kit
  • SAP PI Tips
  • Ergonomics

Popular Topics

  • Home
  • WordPress
  • Tips & Tricks
  • Internet
  • SAP
    • SAP PI
    • SAP ABAP
  • Personal Finance
  • Health & Fitness
  • Travel & Leisure

About Riyaz

riyaz.net is a popular technology site with how-to guides, tips and tutorials on personal technology, blogging, social media, web apps, personal finance and SAP.

riyaz.net was launched way back in 2005 by web designer, blogger and SAP Consultant Riyaz Sayyad from Pune, India. Over the years the site has grown into a full featured online community with thousands of visitors daily from around the world. Read more.

Copyright © 2025 · riyaz.net