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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:23:34+00:00 2026-06-13T09:23:34+00:00

Input: <root><element> <small>a</small> <Large>B</Large> <Time>301</Time></element><element> <small>a</small> <Large>B</Large> <Time>322</Time></element><element> <small>b</small> <Large>A</Large> <Time>274</Time></element><element> <small>c</small> <Large>B</Large> <Time>325</Time></element><element>

  • 0

Input:

<root><element>
  <small>a</small>
  <Large>B</Large>
  <Time>301</Time></element><element>
  <small>a</small>
  <Large>B</Large>
  <Time>322</Time></element><element>
  <small>b</small>
  <Large>A</Large>
  <Time>274</Time></element><element>
  <small>c</small>
  <Large>B</Large>
  <Time>325</Time></element><element>
  <small>b</small>
  <Large>A</Large>
  <Time>301</Time></element></root>

Need to write a xslt see how many times the small and Large elements comes in pair and list the count in smallnum tag and also add the time of that many iterations to totsmalltime tag.

Output:

<root><element>
  <small>a</small>
  <Large>B</Large>
  <smallnum>2</smallnum>
  <totsmalltime>623</totsmalltime></element><element>
  <small>b</small>
  <Large>A</Large>
  <smallnum>2</smallnum>
  <totsmalltime>575</totsmalltime></element><element>
  <small>c</small>
  <Large>B</Large>
  <smallnum>1</smallnum>
  <totsmalltime>325</totsmalltime></element></root>
  • 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-13T09:23:36+00:00Added an answer on June 13, 2026 at 9:23 am

    I. XSLT 1.0 solution:

    Here is a shorter solution that is completely “push style” 🙂

    <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="kElem" match="element"
      use="concat(small, '+', Large)"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match=
     "element[generate-id()=generate-id(key('kElem',concat(small, '+', Large))[1])]">
      <xsl:variable name="vGroup" select="key('kElem',concat(small, '+', Large))"/>
    
      <element>
       <xsl:apply-templates/>
       <smallnum><xsl:value-of select="count($vGroup)"/></smallnum>
       <totsmalltime><xsl:value-of select="sum($vGroup/Time)"/></totsmalltime>
      </element>
     </xsl:template>
     <xsl:template match="element|Time"/>
    </xsl:stylesheet>
    

    When this transformation is applied on the provided XML document:

    <root>
        <element>
            <small>a</small>
            <Large>B</Large>
            <Time>301</Time>
        </element>
        <element>
            <small>a</small>
            <Large>B</Large>
            <Time>322</Time>
        </element>
        <element>
            <small>b</small>
            <Large>A</Large>
            <Time>274</Time>
        </element>
        <element>
            <small>c</small>
            <Large>B</Large>
            <Time>325</Time>
        </element>
        <element>
            <small>b</small>
            <Large>A</Large>
            <Time>301</Time>
        </element>
    </root>
    

    the wanted, correct result is produced:

    <root>
       <element>
          <small>a</small>
          <Large>B</Large>
          <smallnum>2</smallnum>
          <totsmalltime>623</totsmalltime>
       </element>
       <element>
          <small>b</small>
          <Large>A</Large>
          <smallnum>2</smallnum>
          <totsmalltime>575</totsmalltime>
       </element>
       <element>
          <small>c</small>
          <Large>B</Large>
          <smallnum>1</smallnum>
          <totsmalltime>325</totsmalltime>
       </element>
    </root>
    

    Explanation:

    1. Proper use and overriding of the identity rule.

    2. Proper use of the Muenchian Grouping Method.


    II. XSLT 2.0 Solution:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="/*">
      <root>
       <xsl:for-each-group select="*" group-by="concat(small, '+', Large)">
        <element>
         <xsl:sequence select="small, Large"/>
         <smallnum>
           <xsl:sequence select="count(current-group())"/>
         </smallnum>
         <totsmalltime>
          <xsl:sequence select="sum(current-group()/Time)"/>
         </totsmalltime>
        </element>
       </xsl:for-each-group>
      </root>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the same XML document (above), the same correct result is produced:

    <root>
       <element>
          <small>a</small>
          <Large>B</Large>
          <smallnum>2</smallnum>
          <totsmalltime>623</totsmalltime>
       </element>
       <element>
          <small>b</small>
          <Large>A</Large>
          <smallnum>2</smallnum>
          <totsmalltime>575</totsmalltime>
       </element>
       <element>
          <small>c</small>
          <Large>B</Large>
          <smallnum>1</smallnum>
          <totsmalltime>325</totsmalltime>
       </element>
    </root>
    

    Explanation:

    1. Proper use of <xsl:for-each-group> with group-by attribute.

    2. Proper use of the current-group() function.

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

Sidebar

Related Questions

I encountered a peculiar difference in xslt behavior when the root element has a
My input XML consists of the following, <root> <entry> <type>U</type> <value>111</value> </entry> <entry> <type>X</type>
My sample input XML is: <root> <a> <b>item</b> <b>item1</b> <b>item2</b> <b>item3</b> <b>item4</b> </a> </root>
This is my input xml: <root> <node1/> <node2/> <node3/> <node4/> <othertags/> </root> The output
I have the following input XML: <root age=1> <description>some text</description> <section> <item name=a> <uuid>1</uuid>
How to copy xml document skipping some top level nodes. For example: Input: <root>
this is an example of my input file. <root> <!-- [...] --> <bbb>Foo 1</bbb>
I'm creating a new XDocument and inserting a root element profiles in it, then
import xml.etree.ElementTree as ET doc = ET.parse(users.xml) root = doc.getroot() #Returns the root element
Trying to use exportDoc.Root.Elements(string).Where(node => !(node.Element(product).HasElements) || node.Element(product).Element(type).Value != product).Remove(); to remove the nodes

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.