Splitting an array of repeated elements into sub groups based on index in XSL
Sometimes we may require splitting the array of repeated elements into sub groups based on index in XSL; this blog will explain the approach to do the same.
The below XSLT will select the first two order line items from the list of line items.
The same can be used to select the elements by passing startIndex and endIndex values.
<xsl:stylesheet version="1.0"
xmlns:xsdLocal="http://www.reuters.com/ns/2007/01/25/GCAP/EAI/OrderManagement"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="startIndex">1</xsl:variable>
<xsl:variable name="endIndex">2</xsl:variable>
<xsl:template match="/">
<xsdLocal:orderRequest>
<xsl:for-each select="/xsdLocal:UserIdRequest/xsdLocal:orderLineItem[(position() >= $startIndex) and (position() <= $endIndex)]">
<xsdLocal:orderLineItem>
<xsdLocal:CRMOLIId>
<xsl:value-of select="xsdLocal:CRMOLIId"/>
</xsdLocal:CRMOLIId>
<xsdLocal:parentOrderItemId>
<xsl:value-of select="xsdLocal:parentOrderItemId"/>
</xsdLocal:parentOrderItemId>
<xsdLocal:rootItemId>
<xsl:value-of select="xsdLocal:rootItemId"/>
</xsdLocal:rootItemId>
</xsdLocal:orderLineItem>
</xsl:for-each>
</xsdLocal:orderRequest>
</xsl:template>
</xsl:stylesheet>
Input XML:
<?xml version="1.0" encoding="UTF-8" ?>
<UserIdRequest xmlns="http://www.reuters.com/ns/2007/01/25/GCAP/EAI/OrderManagement">
<orderLineItem>
<CRMOLIId>CRMOLIId529</CRMOLIId>
<parentOrderItemId>parentOrderItemId530</parentOrderItemId>
<rootItemId>rootItemId531</rootItemId>
</orderLineItem>
<orderLineItem>
<CRMOLIId>CRMOLIId529</CRMOLIId>
<parentOrderItemId>parentOrderItemId530</parentOrderItemId>
<rootItemId>rootItemId531</rootItemId>
</orderLineItem>
<orderLineItem>
<CRMOLIId>CRMOLIId529</CRMOLIId>
<parentOrderItemId>parentOrderItemId530</parentOrderItemId>
<rootItemId>rootItemId531</rootItemId>
</orderLineItem>
<orderLineItem>
<CRMOLIId>CRMOLIId529</CRMOLIId>
<parentOrderItemId>parentOrderItemId530</parentOrderItemId>
<rootItemId>rootItemId531</rootItemId>
</orderLineItem>
<orderLineItem>
<CRMOLIId>CRMOLIId529</CRMOLIId>
<parentOrderItemId>parentOrderItemId530</parentOrderItemId>
<rootItemId>rootItemId531</rootItemId>
</orderLineItem>
<orderLineItem>
<CRMOLIId>CRMOLIId529</CRMOLIId>
<parentOrderItemId>parentOrderItemId530</parentOrderItemId>
<rootItemId>rootItemId531</rootItemId>
</orderLineItem>
</UserIdRequest>
Output XML:
<?xml version = '1.0' encoding = 'UTF-8'?>
<xsdLocal:orderRequest xmlns:xsdLocal="http://www.reuters.com/ns/2007/01/25/GCAP/EAI/OrderManagement" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.reuters.com/ns/2007/01/25/GCAP/EAI/OrderManagement file:/D:/Albin/DevCodeBase/2012_R5/CRM_EAI_code_deploy/code/CRM61OM/CPFGOM_OrderSplitter/EAI.MSG.OM.125_OrderRequest.xsd">
<xsdLocal:orderLineItem>
<xsdLocal:CRMOLIId>CRMOLIId529</xsdLocal:CRMOLIId>
<xsdLocal:parentOrderItemId>parentOrderItemId530</xsdLocal:parentOrderItemId>
<xsdLocal:rootItemId>rootItemId531</xsdLocal:rootItemId>
</xsdLocal:orderLineItem>
<xsdLocal:orderLineItem>
<xsdLocal:CRMOLIId>CRMOLIId529</xsdLocal:CRMOLIId>
<xsdLocal:parentOrderItemId>parentOrderItemId530</xsdLocal:parentOrderItemId>
<xsdLocal:rootItemId>rootItemId531</xsdLocal:rootItemId>
</xsdLocal:orderLineItem>
</xsdLocal:orderRequest>