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

  • Home
  • SEARCH
  • 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 4584444
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:21:21+00:00 2026-05-21T21:21:21+00:00

Hi I have the following xml <?xml version=1.0 encoding=UTF-8?> <root> <item> <name>john</name> <year>2010</year> </item>

  • 0

Hi I have the following xml

<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>
<name>john</name>
<year>2010</year>
</item>
<item>
<name>sam</name>
<year>2000</year>
</item>
<item>
<name>jack</name>
<year>2007</year>
</item>
<item>
<name>smith</name>
<year>2010</year>
</item>
</root>

I use the following xslt to group by year

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<xsl:template match="/">
<xsl:for-each-group select="r//*[(name=*)]" group-by="year">
<xsl:sort select="year" order="descending"/>
<xsl:variable name="total" select="count(/r//*[(name=*)]) + 1" />
    <xsl:value-of select="year"/><br />
    <xsl:for-each select="current-group()/name">
        <xsl:variable name="i" select="position()"/>    
        <xsl:call-template name="row">
            <xsl:with-param name="name" select="."/>
            <xsl:with-param name="number" select="$total - $i"/>
        </xsl:call-template>
    </xsl:for-each>
    <br />
</xsl:for-each-group>
</xsl:template>

<xsl:template name="row">
<xsl:param name="name"/>
<xsl:param name="number"/>
        <xsl:value-of select="concat($number, '. ')"/>
        <xsl:value-of select="concat($name, ' ')"/><br />
</xsl:template>
</xsl:stylesheet>

This is outputing, it’s quite close to the output i want.

2010 
4. john 
3. smith 

2007 
4. jack 

2000
4. sam

What i want is simply numbering all names (descending order from total number of names to 1) e.g

2010
4. john 
3. smith 

2007
2. jack 

2000
1. sam

It would be simple if we can reassign a varible to a new value, but i thinks it’s not possible, so i have to find another solution. Can anyone help me to find out how to resolve this problem.

thanks

  • 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-21T21:21:22+00:00Added an answer on May 21, 2026 at 9:21 pm

    Here is an XSLT-1.0 solution:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output type="text" omit-xml-declaration="yes"/>
        <xsl:key name="byYear" match="item" use="year"/>
        <xsl:template match="/">
            <!-- process first item for each distinct year number (ordered) -->
            <xsl:apply-templates select="//item[count(.|key('byYear',year)[1])=1]">
                <xsl:sort select="year" order="descending"/>
            </xsl:apply-templates>
        </xsl:template>
        <xsl:template match="item">
            <!-- output year number, surrounded by newlines -->
            <xsl:text>
    </xsl:text>
            <xsl:value-of select="year"/>
            <xsl:text>
    </xsl:text>
            <!-- now process all items for the current year number -->
            <xsl:for-each select="key('byYear',year)">
                <!-- output reversed index of current item for current year number
                     plus total items for lower year numbers -->
                <xsl:number value="count(//item[year &lt; current()/year])+last()-position()+1"
                     format="1. "/>
                <!-- and finally also the name of the current item and again a newline -->
                <xsl:value-of select="name"/>
                <xsl:text>
    </xsl:text>
            </xsl:for-each>
        </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 have the following xml file: <?xml version=1.0 encoding=utf-8?> <root> <phrase id=test><![CDATA[test]]></phrase> <phrase id=test0><![CDATA[test0]]></phrase>
I have the following XML structure: <?xml version=1.0 encoding=utf-8 standalone=yes?> <Root xmlns:xsi=My Program> <NotRoot
Consider we have the following xml document <?xml version=1.0 encoding=utf-8?> <Root> <Child /> </Root>
I have the following XML file: <?xml version=1.0 encoding=utf-8 ?> <strategies> <strategy name=God Class>
I have an xml document: <?xml version=1.0 encoding=utf-8?> <Root> <Child name=MyType compareMode=EQ></Child> </Root> And
I have following type of file contains in xml format <?xml version=1.0 encoding=UTF-8?> <root>
Say I have the following web.config: <?xml version=1.0 encoding=utf-8?> <configuration> <system.web> <authentication mode=Windows></authentication> </system.web>
I have the following XML: <?xml version=1.0 encoding=UTF-8?> <gnm:Workbook xmlns:gnm=http://www.gnumeric.org/v10.dtd xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://www.gnumeric.org/v9.xsd> <office:document-meta xmlns:office=urn:oasis:names:tc:opendocument:xmlns:office:1.0
I have the following XML document: <?xml version=1.0 encoding=UTF-8?> <Offices id=0 enabled=false> <office />
I have the following sample XML: <?xml version=1.0 encoding=utf-8 ?> <queryableData> <table displayName=Shipments dbName=Quotes>

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.