I tried to transform an xml input file that contains multiple identical elements into a new xml file that will combine all identical elements into one. The input file is like is
<?xml version="1.0"?>
<InputShipmentSchedule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<DataArea>
<ShipmentSchedule>
<ShipmentScheduleLine>
<ManufacturingItem>
<ItemID>
<ID>P313503</ID>
</ItemID>
</ManufacturingItem>
</ShipmentScheduleLine>
<ShipmentScheduleLine>
<ManufacturingItem>
<ItemID>
<ID>P313503</ID>
</ItemID>
</ManufacturingItem>
</ShipmentScheduleLine>
<ShipmentScheduleLine>
<ManufacturingItem>
<ItemID>
<ID>P313504</ID>
</ItemID>
</ManufacturingItem>
</ShipmentScheduleLine>
<ShipmentScheduleLine>
<ManufacturingItem>
<ItemID>
<ID>P313504</ID>
</ItemID>
</ManufacturingItem>
</ShipmentScheduleLine>
</ShipmentSchedule>
</DataArea>
</InputShipmentSchedule>
I made the following xsl transformer file:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" omit-xml-declaration="no" indent="yes"/>
<xsl:preserve-space elements="*"/>
<xsl:template match="InputShipmentSchedule">
<xsl:call-template name="CreateShipmentScheduleXmlns"/>
</xsl:template>
<xsl:template name="CreateShipmentScheduleXmlns">
<xsl:element name="Output_Data">
<xsl:element name="ShipmentSchedule">
<xsl:call-template name="part_detail_template">
<xsl:with-param name="currentPartLine" select="DataArea/ShipmentSchedule/ShipmentScheduleLine"/>
<xsl:with-param name="nextPartLine" select="DataArea/ShipmentSchedule/ShipmentScheduleLine/following-sibling::ShipmentScheduleLine"/>
</xsl:call-template>
</xsl:element> <!-- ShipmentSchedule tag end -->
</xsl:element> <!-- Output_Data tag end -->
</xsl:template> <!-- CreateShipmentScheduleXmlns template end -->
<xsl:template name="part_detail_template">
<xsl:param name="currentPartLine"/>
<xsl:param name="nextPartLine"/>
<xsl:element name="Part_Detail"> <!-- Part_Detail tag start -->
<xsl:variable name="part_no" select="$currentPartLine/ManufacturingItem/ItemID/ID"/>
<xsl:element name="part_no">
<xsl:attribute name="value">
<xsl:value-of select="$part_no"/>
</xsl:attribute>
</xsl:element>
</xsl:element> <!-- Part_Detail tag end -->
<xsl:variable name="currentItem" select="$currentPartLine/ManufacturingItem/ItemID/ID"/>
<xsl:variable name="nextItem" select="$nextPartLine/ManufacturingItem/ItemID/ID"/>
<xsl:choose>
<xsl:when test="$nextPartLine and $nextItem != $currentItem">
<xsl:call-template name="part_detail_template">
<xsl:with-param name="currentPartLine" select="$nextPartLine"/>
<xsl:with-param name="nextPartLine" select="$nextPartLine/following-sibling::ShipmentScheduleLine"/>
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template> <!-- part_detail_template tag end -->
</xsl:stylesheet>
But the output xml file still contains redundant P313503 as follows:
<?xml version="1.0" encoding="UTF-8"?>
<Output_Data>
<ShipmentSchedule>
<Part_Detail>
<part_no value="P313503"/>
</Part_Detail>
<Part_Detail>
<part_no value="P313503"/>
</Part_Detail>
<Part_Detail>
<part_no value="P313504"/>
</Part_Detail>
</ShipmentSchedule>
</Output_Data>
I don’t know why the “part_no” element (“P313503”) would appear twice. It is supposed to contain non-redundant “part_no” element in the output xml file. What did I do wrong in the above xsl file? Any comment or suggestion would be greatly appreciated.
Thanks in advance.
These two expressions select the same node set.
DataArea/ShipmentSchedule/ShipmentScheduleLineselects all theShipmentScheduleLineelements not just the first, and so addingfollowing-sibling::ShipmentScheduleLinedoes not select any different nodes.Grouping problems are a lot easier in XSLT2 than XSLT1 but assuming you are stuck at 1 for some reason. google for “muenchian grouping” which will lead you to this solution:
which produces: