Grouping in XSLT
Need to create a XSLT for below Request XML.
XSLT has to do below:
- Group the Coverage codes based on vehicle category.
- Sum up premium per coverage code
- Sum up premium per coverage code and vehicle category
- Get the Vehicle category based on vehicle sequence number.
- calculate split percent based on values derived from 2 and 3 points.
InputXML:
<Root>
<Request>
<Vehicles>
<Vehicle>
<VehicleSequenceNo>1</VehicleSequenceNo>
<VehicleCategory>Tractor</VehicleCategory>
</Vehicle>
<Vehicle>
<VehicleSequenceNo>2</VehicleSequenceNo>
<VehicleCategory>Tractor</VehicleCategory>
</Vehicle>
<Vehicle>
<VehicleSequenceNo>3</VehicleSequenceNo>
<VehicleCategory>Trailer</VehicleCategory>
</Vehicle>
</Vehicles>
<Policies>
<Policy>
<PrimaryAuto>
<Liability>
<Exposures>
<Vehicle>
<VehicleSequenceNo>1</VehicleSequenceNo>
<CoverageProvided>
<CoverageCode>L1</CoverageCode>
<Premium>100</Premium>
</CoverageProvided>
</Vehicle>
<Vehicle>
<VehicleSequenceNo>2</VehicleSequenceNo>
<CoverageProvided>
<CoverageCode>L1</CoverageCode>
<Premium>200</Premium>
</CoverageProvided>
</Vehicle>
<Vehicle>
<VehicleSequenceNo>3</VehicleSequenceNo>
<CoverageProvided>
<CoverageCode>L1</CoverageCode>
<Premium>150</Premium>
</CoverageProvided>
<CoverageProvided>
<CoverageCode>UI</CoverageCode>
<Premium>140</Premium>
</CoverageProvided>
</Vehicle>
</Exposures>
</Liability>
</PrimaryAuto>
</Policy>
</Policies>
</Request>
</Root>
Expected Output:
<PremiumSplit>
<Liability>
<Vehicle>
<CoverageSection>Liability</CoverageSection>
<CoverageCode>L1</CoverageCode>
<!-- 100 + 200 + 150 = 450 (sum of all L1 code premium) ,
100 + 200 = 300 (sum of Tractor L1 premium) ,
450 / 300 = 1.5 -->
<CoveragePercent>1.5</CoveragePercent>
<VehicleCategory>Tractor</VehicleCategory>
</Vehicle>
<Vehicle>
<CoverageSection>Liability</CoverageSection>
<CoverageCode>L1</CoverageCode>
<!-- 100 + 200 + 150 = 450 (sum of all L1 code premium) ,
150 (Trailer L1 premium) ,
450 / 150 = 3 -->
<CoveragePercent>3</CoveragePercent>
<VehicleCategory>Trailer</VehicleCategory>
</Vehicle>
<Vehicle>
<CoverageSection>Liability</CoverageSection>
<CoverageCode>UI</CoverageCode>
<!-- 140 (Trailer UI premium) / 140 (Trailer UI premium) = 1 -->
<CoveragePercent>1</CoveragePercent>
<VehicleCategory>Trailer</VehicleCategory>
</Vehicle>
</Liability>
</PremiumSplit>
I tried foreach on Root/Request/Policies/Policy/PrimaryAuto/Liability/Exposures/Vehicle/CoverageProvided and extracted CoverageCode and VehicleSequenceNumber.
Now I need to fetch Vehicle category for the extracted VehicleSequenceNumber from Root/Request/Vehicles/Vehicle
also need to find duplicate code and do premium addition and percentage calculation
The following XSLT 1.0 solution uses keys (with self-explanatory names) to determine the coverages per code and category and compares the
generate-id()
values to select only the first coverage for a given code or code-category combination.It may no longer work as desired if the input XML contains more than one
<Exposures>
element. For a full solution, a full specification of the meaning of the elements in the input XML would be needed. But the given solution can serve as a starting point.More elegant solutions are possible with XSLT 2.0 or 3.0, if you are able to use that.