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.
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
This first template is only applied to the object with id 40000000. This object was chosen as the root of the object tree.
xsl:copy instructs the XSLT to copy this xml node and all attributes to the output XML.
xsl:apply-templates on node instructs the XSLT to apply the node template on all fields of this node. (see template below)
xsl:apply-templates on key instructs the XSLT to nest include all the nodes that have their parent field have the same value as the “externalCode” field of the root object.
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.
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.
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.
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.