Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7677947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:32:07+00:00 2026-05-31T17:32:07+00:00

I tried to transform an xml input file that contains multiple identical elements into

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:32:08+00:00Added an answer on May 31, 2026 at 5:32 pm
                    <xsl:with-param name="currentPartLine" 
                    select="DataArea/ShipmentSchedule/ShipmentScheduleLine"/>
                    <xsl:with-param name="nextPartLine" 
                    select="DataArea/ShipmentSchedule/ShipmentScheduleLine/
                                       following-sibling::ShipmentScheduleLine"/>
    

    These two expressions select the same node set. DataArea/ShipmentSchedule/ShipmentScheduleLine selects all the ShipmentScheduleLine elements not just the first, and so adding following-sibling::ShipmentScheduleLine does 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:

    <xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>
    
    <xsl:key name="n" match="ItemID" use="ID"/>
    
    <xsl:template match="/">
     <Output_Data>
      <ShipmentSchedule>
       <xsl:for-each select="//ManufacturingItem/ItemID[generate-id()=
                 generate-id(key('n',.))]">
        <Part_Detail>
         <part_no value="{.}"/>
        </Part_Detail>
       </xsl:for-each>
      </ShipmentSchedule>
     </Output_Data>
    </xsl:template>
    
    </xsl:stylesheet>
    

    which produces:

    $ saxon man.xml man.xsl
    <?xml version="1.0" encoding="utf-8"?>
    <Output_Data>
       <ShipmentSchedule>
          <Part_Detail>
             <part_no value="P313503"/>
          </Part_Detail>
          <Part_Detail>
             <part_no value="P313504"/>
          </Part_Detail>
       </ShipmentSchedule>
    </Output_Data>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking to make an XSLT that will transform the input XML below:
I'm using an XSL to transform one XML into another. My problem is that
This is similar question with few differences : Split XML file into multiple files
I have an interesting problem with XSL. We have an XML Input file that
I need to extract data from a .dbf file and transform it into xml.
I'm trying to transform an XML document into another XML document. I've tried various
I would like to use xslt to transform an xml file into an almost
I've tried this, but that doesn't work: CATransform3D currentTransform = self.layer.transform; CATransform3D identityTransform =
I need to transform a piece of XML, so that the value of every
I am having a xml file as input to the xsl file. When i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.