This blog narrates how XSLT mapping can be effectively used to filter records. Although filter operations can be accomplished using Groovy and CPI pallet filter options, XSLT mapping can be immensely helpful in some of the scenarios.
Case 1: Remove Parent node if a particular child node is not present:
Input XML:
This is the sample input XML. <Parent> node specified in the second position does not contain <Id>. We will write an XSLT mapping to remove that specific <Parent> where <Id> is not present.
XSLT Mapping to remove parent node (<Parent>) if child node (<Id>) is not present:
<!--Copies the whole input-->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!--Keeps only those records where Id is present-->
<xsl:template match="Parent[not(Id)]"/>
Output:
This is the output after XSLT mapping, which shows that the <Parent> in the second position is removed as child node <Id> is not present.
Case 2: Filter record based on externalized property value using XSLT
- How to use externalized properties in XSLT mapping?
We can use values specified in externalized property in XSLT mapping.
Example:
Suppose we have externalized a property named ‘Id’ and we have specified the values in the property as 1,2.
Screenshot of Externalized Parameter:
We want to keep those records where Id is 1 or 2.
We need to use the below mentioned XSLT code to use eternalized property ‘Id’ in XSLT Mapping:
<xsl:param name="Id" />
- XSLT mapping to Filter records on the basis of values present in externalized properties
Input XML:
This is the input XML. We need to display those <Parent> where <Id> is 1 or 2.
XSLT Mapping:
<!--We need to declare a param named Id(name of the externalized parameter)-->
<xsl:param name="Id" />
<!--Since the input in the externalized parameter is a comma separated value (1,2), we need to split the values and put it in a list (named tokenizedList). We have used XSLT function "tokenize" to accomplish that-->
<xsl:variable name="tokenizedList" select="tokenize($Id, ',')"/>
<!--Copies the whole input-->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!--Keeps records where Id is equal to the value present in the Externalized parameter -->
<xsl:template match="Parent[not(Id = $tokenizedList)]"/>
Output:
This is the output XML, which contains only <Parent> nodes having ‘Id’ value as 1,2.
Case 3: Remove Parent node if a particular child node has a specific value
Input XML
This is the sample input XML. We will write an XSLT mapping that will remove the parent node if the value of the child node <Id> is 3.
XSLT Mapping to remove parent node (<Parent>) if the value of <Id> = 3
<!--Copies the whole input-->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!—Removes parent node where <Id> = 3 -->
<xsl:template match="Parent[(Id = ‘3’)]"/>
I tried to cover some of the filter scenarios using XSLT, which can help in CPI implementations. Please share your valuable feedback, and please let me know if you need any additional details on this topic.
Reference :
https://help.sap.com/viewer/368c481cd6954bdfa5d0435479fd4eaf/Cloud/en-US/f917c39942284e0e971faed3521...