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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:26:19+00:00 2026-05-20T09:26:19+00:00

I need to solve a groping problem in my xsl; I have to group

  • 0

I need to solve a groping problem in my xsl; I have to group the input xml based on , and . If any of these data is not same then create new tag and copy the respective inside that. If you check my xslt i am able to do the grouping by but not sure how to incorporate the same logic for and . I am not sure if i explained the problem very well but if u see the “needed output” section it would be more easy to understand what I want. Thanks in advance for your help.

Input:

<?xml version="1.0" encoding="UTF-8"?>
<BC>
    <SO>
        <plantCode>xyz</plantCode>
        <airportofloading>US</airportofloading>
        <airportofunloading>UK</airportofunloading>
                  <package>1</package>
                  ....
    </SO>
    <SO>
        <plantCode>xyz</plantCode>
        <airportofloading>US</airportofloading>
        <airportofunloading>UK</airportofunloading>
                  <package>2</package>
                  ...
    </SO>
    <SO>
        <plantCode>abc</plantCode>
        <airportofloading>US</airportofloading>
        <airportofunloading>UK</airportofunloading>
                  <package>5</package>
                  ....
    </SO>
    <SO>
        <plantCode>abc</plantCode>
        <airportofloading>US</airportofloading>
        <airportofunloading>AB</airportofunloading>
                  <package>1</package>
                  ....
    </SO>
</BC>

Needed Output:

<?xml version="1.0" encoding="UTF-8"?>
<BC>
    <plant name="xyz">
        <SO>
            <plantCode>xyz</plantCode>
            <airportofloading>US</airportofloading>
            <airportofunloading>UK</airportofunloading>
                            <package>1</package>
                            ....
        </SO>
        <SO>
            <plantCode>xyz</plantCode>
            <airportofloading>US</airportofloading>
            <airportofunloading>UK</airportofunloading>
                            <package>2</package>
                            ....
        </SO>
    </plant>
    <plant name="abc">
        <SO>
            <plantCode>abc</plantCode>
            <airportofloading>US</airportofloading>
            <airportofunloading>UK</airportofunloading>
                            <package>5</package>
                            ....

        </SO>
    </plant>
    <plant name="abc">
        <SO>
            <plantCode>abc</plantCode>
            <airportofloading>US</airportofloading>
            <airportofunloading>AB</airportofunloading>
                            <package>1</package>
                            ....

        </SO>
    </plant>
    </BC>

My XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"     xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:output method="xml"/>
    <xsl:key name="Code" match="SO" use="plantCode"/>
    <xsl:template match="BC">
        <xsl:element name="BC">
            <xsl:apply-templates select="SO[generate-id(.) = generate-id(key    ('Code', plantCode)[1])]"/>
            </xsl:element>
</xsl:template>
<xsl:template match="SO">
    <xsl:element name="plant">
        <xsl:attribute name="name"><xsl:value-of     select="plantCode"/></xsl:attribute>
            <xsl:for-each select="key('Code', plantCode)">
                <xsl:copy-of select="."/>
            </xsl:for-each>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>
  • 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-20T09:26:19+00:00Added an answer on May 20, 2026 at 9:26 am

    Edit: Sorry, I see now I misunderstood your question. I belive the following is more or less what you are looking for:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:key name="code" match="SO" use="plantCode" />
      <xsl:key name="airports" match="SO" use="concat(airportofloading,' ',airportofunloading)" />
      <xsl:output indent="yes"/>
      <xsl:template match="/">
            <xsl:apply-templates />
      </xsl:template>
      <xsl:template match="BC">
            <BC>
                  <xsl:for-each select="SO[generate-id(.)=generate-id(key('code',plantCode))]">
                        <xsl:variable name="thisCode" select="plantCode"/>
                        <xsl:for-each select="../SO[generate-id() = generate-id(key('airports', concat(airportofloading,' ',airportofunloading))[plantCode = $thisCode][1])]">
                              <xsl:element name="plant">
                                    <xsl:attribute name="name">
                                          <xsl:value-of select="plantCode"/>
                                    </xsl:attribute>
                                    <xsl:for-each select="key('airports', concat(airportofloading,' ',airportofunloading))[plantCode = $thisCode]">
                                          <xsl:copy-of select="."/>
                                    </xsl:for-each>
                              </xsl:element>
                        </xsl:for-each>
                  </xsl:for-each>
            </BC>
      </xsl:template>
    

    That should create one <plant> element for every triple of {plantcode, airportofloading, airportofunloading}. Said <plant> element containing all the <SO> elements for that triple. I believe that is what you wanted, or is very close to it, so you should be able to make any needed adjustments.

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

Sidebar

Related Questions

I have a problem I need to solve, but I can't think of any
I need to solve a gender translation problem, and Django doesn't seem to have
I have a problem that I believe I need to use grouping using xsl:key
I need to solve a problem. I have 5 devices. They all have 4
I need to solve the following problem. //pseudo algorithm you have four elements: elm1,
I have a problem I need to solve regarding my Javascript code. When instantiating
I need to solve the following problem: I have multiple rectangles of sizes: width
I need to solve the following problem for one of my clients. They have
I need to solve a problem like below. I'm not sure, can you suggest
For a CS class I need to solve an assigned problem using three data

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.