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 5840259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:42:58+00:00 2026-05-22T11:42:58+00:00

I want to merge items with same id into single item. The resultant item

  • 0

I want to merge items with same id into single item. The resultant item needs to have first non-empty element from the item group. If there is no non-empty element in the item group then the element itself ignored from the item group and should not present in the resultant item.

Sample XML Input:

<?xml version="1.0" encoding="UTF-8" ?>
<shiporder orderid="orderid106">
   <orderperson id="123">orderperson107</orderperson>
   <shipto>
      <name>name108</name>
      <address>address109</address>
      <city>city110</city>
      <country>country111</country>
   </shipto>

   <!--Item Group 100-->
   <item>
      <id>100</id>
      <title>
         <first>
            <a></a>
         </first>
         <last>item100_lastTitle1</last>
      </title>
      <note></note>
      <quantity></quantity>
   </item>
   <item>
      <id>100</id>
      <title>
         <first>
            <a>a_100_2</a>
         </first>
         <last>item100_lastTitle2</last>
      </title>
      <note>note100_2</note>
      <quantity></quantity>
   </item>
   <item>
      <id>100</id>
      <title>
         <first>
            <a att1="abc" att2='cde'>a_100_3</a>
         </first>
         <last id="1" attr="2">item100_lastTitle3</last>
      </title>
      <note>note100_3</note>
      <quantity>1</quantity>
   </item>

   <!--Item Group 101-->
   <item>
      <id>101</id>
      <title>
         <first>
            <a>a_101_1</a>
         </first>
         <last></last>
      </title>
      <note>note101_1</note>
      <quantity>10</quantity>
   </item>
   <item>
      <id>101</id>
      <title>
         <first>
            <a>a_101_2</a>
         </first>
         <last>item101_lastTitle2</last>
      </title>
      <note>note101_2</note>
      <quantity>5</quantity>
   </item>

   <!--Item Group 103-->
   <item>
      <id>103</id>
      <title>
         <first>
            <a>a_103_2</a>
         </first>
         <last>item103_lastTitle2</last>
      </title>
      <note>note103_1</note>
      <quantity></quantity>
   </item>
</shiporder>

Sample XML Output:

<?xml version = '1.0' encoding = 'UTF-8'?>
<shiporder orderid="orderid106">
  <item>
    <id>100</id>
    <title>
      <first>
        <a>a_100_2</a>
      </first>
      <last>item100_lastTitle1</last>
    </title>
    <note>note100_2</note>
    <quantity>6</quantity>
  </item>

  <item>
    <id>101</id>
    <title>
      <first>
        <a>a_101_1</a>
      </first>
      <last>item101_lastTitle2</last>
    </title>
    <note>note101_1</note>
    <quantity>10</quantity>
  </item>

  <item>
    <id>103</id>
    <title>
      <first>
        <a>a_103_2</a>
      </first>
      <last>item103_lastTitle2</last>
    </title>
    <note>note103_1</note>
  </item>
</shiporder>

I tried with the following XSL code to get the above output:

<?xml version="1.0" encoding="windows-1252" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="/shiporder">
      <shiporder>
         <xsl:if test="@orderid">
            <xsl:attribute name="orderid">
               <xsl:value-of select="@orderid"/>
            </xsl:attribute>
         </xsl:if>

         <!-- Grouping each item based on its ID element value-->
         <xsl:for-each-group select="item" group-by="id">
            <item-group>
               <!-- For Each group search for first non empty child element-->
               <xsl:for-each select="current-group()[id/text()][1]">
                  <xsl:copy-of select="id"/>
               </xsl:for-each>
               <title>
                  <first>
                     <!-- for 'a' element -->
                     <xsl:variable name="temp1">
                        <xsl:for-each select="current-group()/title/first[a/text()][1]">
                           <elm>
                              <xsl:copy-of select="a"/>
                           </elm>
                        </xsl:for-each>
                     </xsl:variable>
                     <xsl:copy-of select="$temp1/elm[1]/a"/>
                  </first>

                  <!-- for 'last' element -->
                  <xsl:variable name="temp2">
                     <xsl:for-each select="current-group()/title[last/text()][1]">
                        <elm>
                           <xsl:copy-of select="last"/>
                        </elm>
                     </xsl:for-each>
                  </xsl:variable>
                  <xsl:copy-of select="$temp2/elm[1]/last"/>
               </title>

               <!-- for 'note' element -->
               <xsl:for-each select="current-group()[note/text()][1]">
                  <xsl:copy-of select="note"/>
               </xsl:for-each>

               <!-- for 'quantity' element -->
               <xsl:for-each select="current-group()[quantity/text()][1]">
                  <xsl:copy-of select="quantity"/>
               </xsl:for-each>

            </item-group>
         </xsl:for-each-group>
      </shiporder>
   </xsl:template>
</xsl:stylesheet>

The code works fine But the worst part is, I couldn’t make it generalize. For each element under the item I need to write a xsl code specific to the xpath of particular element. I couldn’t even write code in terms of user-defined-templates.

I found already answered question in the title of “Recursively combine identical sibling elements in XSLT“. But I’m not able to customize it according to my requirements.

I’m having hundreds of such child or decedent element under each item and the depth of each one is arbitrary. It is very ridiculous to write xsl code corresponds to each of those child or descendants. Can any of the experts guide me to write generic xsl code (irrespective of number of elements and also its depth) or optimize the existing code?

Thanks in advance,

Kathir

  • 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-22T11:42:58+00:00Added an answer on May 22, 2026 at 11:42 am

    This is actually an XSLT 1.0 solution (I don’t have an XSLT2 processor to hand), but it uses a generic ‘combine’ named template that should do what you need.

    <?xml version="1.0" encoding="windows-1252" ?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:key name="item" use="id" match="item" />
    
      <xsl:template match="item[generate-id() = generate-id(key('item',id)[1])]">
        <xsl:call-template name="combine">
          <xsl:with-param name="list" select="../*[id = current()/id]" />
        </xsl:call-template>
      </xsl:template>
    
      <xsl:template match="item" />
    
      <xsl:template name="combine">
        <xsl:param name="list" />
        <xsl:element name="{name($list[1])}">
          <xsl:for-each select="*[not(preceding-sibling::*[name() = name(current())])]" >
            <xsl:call-template name="combine">
              <xsl:with-param name="list" select="$list/*[name() = name(current())]" />
            </xsl:call-template>
          </xsl:for-each>
          <xsl:value-of select="$list[text()][1]/text()" />
        </xsl:element>
      </xsl:template>
    
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:template>
    </xsl:stylesheet>
    

    The combine template takes a list of nodes, and combines them into one recursively, taking the first available value for each node. This template does NOT combine attributes as well, but as you don’t have any in the item XML you provided, this should be OK. The <xsl:for-each in the middle of the template uses an XSLT1 technique for picking out unique element names; it only selects those that don’t have a previous sibling with the same name. There’s probably a way of doing this using for-each-group in XSLT2, but I can’t check that here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a tuple, INSTALLED_APPS, and I want to merge extra items into it
I have 2 images and I want to merge them into one on my
If I have two arrays both with 10000 items, now I want to merge
I want to merge this queries into only one, to return 2 values (scount_atp
I want to merge two branches that have been separated for a while and
I have to array and I want to merge this two array with a
I have two objects and I want to merge them: public class Foo {
I have a plist with two string arrays. I want to merge these two
If I have the following, $this->Session->write('ScoreCardCriteria', 'test'); And want to add another item to
I have an IEnumerable<T> and an IEnumerable<U> that I want merged into an IEnumerable<KeyValuePair<T,U>>

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.