Acorel
Gratis demo

Using XSLT in SAP CPI

to make like a tree and nest
SAP CPI is about transforming messages and XSLT is one way to do that.

When describing a hierarchical structure such as an organisation, unpeeling data can become quite complex.

Sometimes you might want to subselect the information of a whole department, or you might want to select ‘the manager of the manager’, or you want to know the number of nodes (employees) a manager is responsible for. In such complex hierarchical structures, it can be tough to perform a proper mapping in SAP Cloud Platform Integration (CPI). You might try some groovy scripting, but in the case of multiple hierarchical levels, this might turn into spaghetti real quick.

Let’s see if we can use data from a flat list with parent information into a nested tree structure, to find for instance all people that report to a manager, directly or indirectly.

If the structure supplied is in XML one of the options to make it into this nested structure is using XSLT.

XSL (eXtensible Stylesheet Language) is a styling language for XML. XSLT stands for XSL Transformations.

Today we will have a look at one example of XSLT and go line by line to explain it.

XSLT

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

the XSL:stylesheet tag signifies that this xml file is a transformation. Lets have a look inside.

  <xsl:key name="nodeChildren" match="/FODepartment/FODepartment" use="parent"/>

Lets make a variable key with the name “nodeChildren”. The match part specifies the Xpath to the objects. The use part tells us that the subfield named “parent” is the one we will be looking at.

The next part is the most interesting bit, the template matching part. we have 4 templates defined.

  <xsl:template match="/FODepartment">
		<xsl:apply-templates select="/FODepartment/FODepartment[externalCode='40000000']"/>
  </xsl:template> 

This part will be the root node, the object that nests everything. Inside it, it triggers the next template, on the object with id 40000000.
Because the next two templates are very similar, only difference being that the first is only applied to the root object, the second template is applied to all other object nodes in the xml file

 <xsl:template match="/FODepartment/FODepartment[externalCode='40000000']">
		<xsl:copy>
			<xsl:apply-templates select="node()|@*"/>
			<xsl:apply-templates select="key('nodeChildren',./externalCode)"/>
		</xsl:copy>
  </xsl:template>
  <xsl:template match="FODepartment/FODepartment">
		<xsl:copy>
			<xsl:apply-templates select="node()|@*"/>
			<xsl:apply-templates select="key('nodeChildren',./externalCode)"/>
		</xsl:copy>
  </xsl:template>

This first template is only applied to the object with id 40000000. This object was chosen as the root of the object tree.

the apply-templates tells XSLT to apply matching templates to the structures referenced, in this case the children of the object that are being copied into the nested structure. Using the second template that matches all object nodes, this will nest the children of those children in the same way, recursively.

  	<xsl:template match="node()|@*">
		<xsl:copy>
			<xsl:apply-templates select="node()|@*"/>
		</xsl:copy>
	</xsl:template>

This final template is just to copy all fields and all attributes that are in the object. The special keyword “node()” tells the XSLT to apply a template to each field. Attributes are specified with the “@” “at” “apestaartje” symbool. the special combination “@*” means all attributes. This template applies itself to any DOM children of the fields that are being copied for each node, making a full-depth copy of the node.

</xsl:stylesheet>

With this closing tag we close the XSL.

To protect customer data I can not share the actual xml this was used with, but knowing you are interested in the genealogy of the Greek gods, here is an example of a nested tree I found online that could have been the output of a similar but different XSLT transformation.

greek gods

If you are struggling with data transformation in SAP CPI, or just need some help getting those sweet clean dataflows, feel free to contact us or comment below.

David van Deijk

Read all my blogs

Receive our weekly blog by email?
Subscribe here: