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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:19:40+00:00 2026-05-28T16:19:40+00:00

Sorry for my English. I need sort nodes of this XML data <root> <section

  • 0

Sorry for my English.
I need sort nodes of this XML data

<root>
  <section id="1">
    <news-item id="1" pub-date="2012-01-03" />
    <news-item id="2" pub-date="2012-01-04" />
    <news-item id="3" pub-date="2011-12-21" />
  </section>

  <section id="2">
    <news-item id="4" pub-date="2012-01-05" />
    <news-item id="5" pub-date="2012-01-06" />
    <news-item id="6" pub-date="2012-01-07" />
  </section>

  <section id="3">
    <news-item id="7" pub-date="2012-02-10" />
    <news-item id="8" pub-date="2012-02-11" />
    <news-item id="9" pub-date="2012-02-12" />
  </section>
</root>

to this

<root>
  <section id="3">
    <news-item id="9" pub-date="2012-02-12" />
    <news-item id="8" pub-date="2012-02-11" />
    <news-item id="7" pub-date="2012-02-10" />
  </section>

  <section id="2">
    <news-item id="6" pub-date="2012-01-07" />
    <news-item id="5" pub-date="2012-01-06" />
    <news-item id="4" pub-date="2012-01-05" />
  </section>

  <section id="1">
    <news-item id="2" pub-date="2012-01-04" />
    <news-item id="1" pub-date="2012-01-03" />
    <news-item id="3" pub-date="2011-12-21" />
  </section>
</root>

i.e. I need first sort news-item elements by pub-date in section, and then sort section element by max pub-date in news-item. (Section with lastes news must be on top).

Many 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-28T16:19:42+00:00Added an answer on May 28, 2026 at 4:19 pm

    This is your pastebin with the names fixed and a different sort for the sections:

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="section">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates select="news-item">
                <xsl:sort select="@pub-date" data-type="text" order="descending" />
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="root">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:variable name="sectionOrder">
                <xsl:text>|</xsl:text>
                <xsl:for-each select="section/news-item">
                    <xsl:sort select="@pub-date" data-type="text" order="descending" />
                    <xsl:value-of select="generate-id(..)"/>
                    <xsl:text>|</xsl:text>
                </xsl:for-each>
            </xsl:variable>
            <xsl:apply-templates select="section">
                <xsl:sort select="string-length(substring-before($sectionOrder, concat('|',generate-id(),'|')))" data-type="number" />
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
    

    The names in your pastebin don’t match (newsitem vs. news-item, pubdate vs. pub-date), so I fixed that. The output I get seems to be right (also with the test case you added in your comment):

    <root>
        <section id="2">
            <news-item id="7" pub-date="2222-12-22" />
            <news-item id="6" pub-date="2012-01-07" />
            <news-item id="5" pub-date="2012-01-06" />
            <news-item id="4" pub-date="2012-01-05" />
        </section>
        <section id="3">
            <news-item id="10" pub-date="2012-02-12" />
            <news-item id="9" pub-date="2012-02-11" />
            <news-item id="8" pub-date="2012-02-10" />
        </section>
        <section id="1">
            <news-item id="2" pub-date="2012-01-04" />
            <news-item id="1" pub-date="2012-01-03" />
            <news-item id="3" pub-date="2011-12-21" />
        </section>
    </root>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for bad English :( Suppose i can preliminary organize recipes and ingredients data
Firstly, sorry for my English (I'm from China). By reading this article( how to
Hi (sorry for my ugly english) I wonder if this is possible to retrieve
First of all, sorry for my English... I have something like this: public class
newbie here, sorry if this is an obvious question, and sorry for my English.
(Sorry for my english) First: I have an application created using C++ (this is
Sorry for my English. This problem is complicated, and my English isn't thats good
Alright, sorry for my english in advance, here what I need: I got a
first of all sorry for my english , not my main lang. I need
name|num|num|num|num name|num|num|num|num name|num|num|num|num How i can sort this list on need me field (2,3,4,5)

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.