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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:33:00+00:00 2026-06-04T23:33:00+00:00

I am using XSLT for converting a XML derived from web and convert the

  • 0

I am using XSLT for converting a XML derived from web and convert the same on the fly into the target xml file denoted as output .
I am still unable to do so even after trying a lot , Can anyone please help me out with this conversion .

Source XML

<allocelement>
    <hd1>12</hd1>
    <hd2>14</hd2>
    <hd3>87</hd3>
        <alc>1</alc>
    <amount>4587</amount>
    <code>1111</code>
</allocelement>
<alloclement>
        <hd1>12</hd1>
    <hd2>14</hd2>
    <hd3>87</hd3>
    <alc>2</alc>
    <amount>80000</amount>
    <code>1111</code>
</alloclement>
<alloclement>
    <hd1>875</hd1>
    <hd2>455</hd2>
    <hd3>455</hd3>
    <alc>2</alc>
    <amount>80000</amount>
    <code>1112</code>
 </alloclement>

Output Desired

<allocelement>
    <Codeheader>
    <code>1111</code>
        <hd1>12</hd1>
        <hd2>14</hd2>
        <hd3>87</hd3>
                <alc>1</alc>
                    <amount>4587</amount>
                <alc>2</alc>
                    <amount>80000</amount>
    </codeHeader>
        <CodeHeader>
    <code>1112</code>
        <hd1>875</hd1>
        <hd2>455</hd2>
        <hd3>455</hd3>
            <alc>2</alc>
              <amount>80000</amount>
    </CodeHeader>
</allocelement>

The Grouping is on the basis of Code,[hd1,hd2,hd3] such that the different elements within which have the same Code and [hd1,hd2,hd3] will be merged and only show fields which are different viz. the and .
Also I am using xslt 1.0 .

  • 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-06-04T23:33:01+00:00Added an answer on June 4, 2026 at 11:33 pm

    A significantly shorter and simpler XSLT 1.0 solution:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:key name="kCodeByVal" match="code" use="."/>
    
     <xsl:template match="/*">
      <allocelement>
       <xsl:apply-templates/>
      </allocelement>
     </xsl:template>
    
    <xsl:template match="allocelement"/>
    
     <xsl:template match=
      "allocelement
        [generate-id(code) = generate-id(key('kCodeByVal', code)[1])]">
      <code><xsl:value-of select="code"/>
        <xsl:copy-of select=
        "node()[not(self::code)]
        | key('kCodeByVal', code)/../*[self::alc or self::amount]"/>
      </code>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the following XML document (a single top element wrapping the provided XML fragment):

    <t>
        <allocelement>
            <hd1>12</hd1>
            <hd2>14</hd2>
            <hd3>87</hd3>
            <alc>1</alc>
            <amount>4587</amount>
            <code>1111</code>
        </allocelement>
        <allocelement>
            <hd1>12</hd1>
            <hd2>14</hd2>
            <hd3>87</hd3>
            <alc>2</alc>
            <amount>80000</amount>
            <code>1111</code>
        </allocelement>
        <allocelement>
            <hd1>875</hd1>
            <hd2>455</hd2>
            <hd3>455</hd3>
            <alc>2</alc>
            <amount>80000</amount>
            <code>1112</code>
        </allocelement>
    </t>
    

    the wanted, correct result is produced:

    <allocelement>
       <code>1111<hd1>12</hd1>
          <hd2>14</hd2>
          <hd3>87</hd3>
          <alc>1</alc>
          <amount>4587</amount>
          <alc>2</alc>
          <amount>80000</amount>
       </code>
       <code>1112<hd1>875</hd1>
          <hd2>455</hd2>
          <hd3>455</hd3>
          <alc>2</alc>
          <amount>80000</amount>
       </code>
    </allocelement>
    

    Explanation: Proper use of the Muenchian grouping method.


    II. XSLT 2.0 solution — again shorter, and much more importantly — syntactically and semantically correct:

    <xsl:stylesheet version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
      <xsl:template match="/*">
      <allocelement>
       <xsl:for-each-group select="*" group-by="code">
         <code><xsl:value-of select="code"/>
            <xsl:sequence select=
            "node()[not(self::code)]
            | current-group()/*[self::alc or self::amount]"/>
      </code>
       </xsl:for-each-group>
      </allocelement>
     </xsl:template>
    </xsl:stylesheet>
    

    UPDATE: The OP has changed the requirements for the outpu.

    Here is the corresponding modified XSLT 1.0 solution:

    <xsl:stylesheet version="1.0"
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
         <xsl:output omit-xml-declaration="yes" indent="yes"/>
         <xsl:strip-space elements="*"/>
    
         <xsl:key name="kCodeByVal" match="code" use="."/>
    
         <xsl:template match="/*">
          <allocelement>
           <xsl:apply-templates/>
          </allocelement>
         </xsl:template>
    
        <xsl:template match="allocelement"/>
    
         <xsl:template match=
          "allocelement
            [generate-id(code) = generate-id(key('kCodeByVal', code)[1])]">
          <codeHeader>
           <code><xsl:value-of select="code"/></code>
             <xsl:copy-of select=
             "node()[not(self::code)]
             | key('kCodeByVal', code)/../*[self::alc or self::amount]"/>
          </codeHeader>
         </xsl:template>
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using XSLT to convert a very large XML document into (X)HTML. For
I'm using XSLT to transform from one format of XML into another but I
I am using XSLT to transform a XML into a html/php file. In this
I am converting a XML to HTML using XSLT. The output of should have
I am converting a xml using xslt. Original XML is <Content> <book> <customData> <CustomDataElement>
I am have trouble converting my xml using XSLT back to xml in the
I'm using XSLT 1.0 an want to transform an XML file. It works fine
I'm using XSLT to manipulate the data inside of an XML file (I'm taking
I'm using XSLT 1.0 and need to convert a date format from dd/mm/yyyy to
I have an assignment on converting the input xml using xslt. My input xml

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.