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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:41:23+00:00 2026-06-10T00:41:23+00:00

Assuming that I have this: <nodes> <node> <number>1</number> <name>ABC</name> <comment>blah</comment> </node> <node> <number>2</number> <name>DEF</name>

  • 0

Assuming that I have this:

<nodes>
<node>
    <number>1</number>
    <name>"ABC"</name>
    <comment>"blah"</comment>
</node>

<node>
    <number>2</number>
    <name>"DEF"</name>
    <comment>"blah blah"</comment>
</node>

<node>
    <number>3</number>
    <name>"XYZ"</name>
    <comment>"blah blah blah"</comment>
</node>
</nodes>

I would like to end up with this:

<nodes>
<node>
    <number>3</number>
    <name>"XYZ"</name>
    <comment>"blah blah blah"</comment>
</node>

<node>
    <number>2</number>
    <name>"DEF"</name>
    <comment>"blah blah"</comment>
</node>

<node>
    <number>1</number>
    <name>"ABC"</name>
    <comment>"blah"</comment>
</node>
</nodes>
  • 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-10T00:41:24+00:00Added an answer on June 10, 2026 at 12:41 am

    I. In your case the solution is as simple as this:

    <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:template match="node()|@*">
         <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
         </xsl:copy>
     </xsl:template>
    
     <xsl:template match="/*">
      <nodes>
       <xsl:apply-templates select="node">
         <xsl:sort select="position()" data-type="number" order="descending"/>
       </xsl:apply-templates>
      </nodes>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the provided XML document:

    <nodes>
        <node>
            <number>1</number>
            <name>"ABC"</name>
            <comment>"blah"</comment>
        </node>
        <node>
            <number>2</number>
            <name>"DEF"</name>
            <comment>"blah blah"</comment>
        </node>
        <node>
            <number>3</number>
            <name>"XYZ"</name>
            <comment>"blah blah blah"</comment>
        </node>
    </nodes>
    

    the wanted, correct result is produced:

    <nodes>
       <node>
          <number>3</number>
          <name>"XYZ"</name>
          <comment>"blah blah blah"</comment>
       </node>
       <node>
          <number>2</number>
          <name>"DEF"</name>
          <comment>"blah blah"</comment>
       </node>
       <node>
          <number>1</number>
          <name>"ABC"</name>
          <comment>"blah"</comment>
       </node>
    </nodes>
    

    II. Reversing a node-set inplace, whose nodes all belong to the same document:

    Things become much more tricky if we want to reverse the nodes of a node-set and leave all other nodes in the document “as-is”.

    Let’s nave this XML document:

    <nums>
      <num>01</num>
      <num>02</num>
      <num>03</num>
      <num>04</num>
      <num>05</num>
      <num>06</num>
      <num>07</num>
      <num>08</num>
      <num>09</num>
      <num>10</num>
    </nums>
    

    We want to produce from this a document where the num elements with odd value are in the same order, but the num elements with even value come in reverse order. The result must be:

    <nums>
       <num>01</num>
       <num>10</num>
       <num>03</num>
       <num>08</num>
       <num>05</num>
       <num>06</num>
       <num>07</num>
       <num>04</num>
       <num>09</num>
       <num>02</num>
    </nums>
    

    Here is the transformation that produces the wanted result:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:variable name="vNodes" select="/*/*[. mod 2 = 0]"/>
    
     <xsl:variable name="vrtfReverse">
       <xsl:for-each select="$vNodes">
         <xsl:sort select="position()" data-type="number" order="descending"/>
         <xsl:copy-of select="."/>
       </xsl:for-each>
     </xsl:variable>
    
     <xsl:variable name="vReverse" select="ext:node-set($vrtfReverse)/*"/>
    
     <xsl:template match="node()|@*" name="identity">
         <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
         </xsl:copy>
     </xsl:template>
    
     <xsl:template match="*/*">
      <xsl:choose>
       <xsl:when test="count(. | $vNodes) > count($vNodes)">
         <xsl:call-template name="identity"/>
       </xsl:when>
       <xsl:otherwise>
         <xsl:variable name="vPositionInNodeSet" select=
         "count($vNodes
                 [count(.
                        | current()/preceding::node()
                        | current()/ancestor::node()
                        )
                 =
                  count(  current()/preceding::node()
                        | current()/ancestor::node()
                        )
                 ]
                ) +1"/>
    
         <xsl:for-each select="$vReverse[position() = $vPositionInNodeSet]">
           <xsl:call-template name="identity"/>
         </xsl:for-each>
       </xsl:otherwise>
      </xsl:choose>
     </xsl:template>
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming that I have this: enum { A = 0x2E, B = 0x23, C
Assuming that I have a CronTriggerBean similar to <bean id=midMonthCronTrigger class=org.springframework.scheduling.quartz.CronTriggerBean> <property name=jobDetail ref=reminderJobDetail
In Android, assuming that I have files in /data/data/package.name/, without knowing the names or
How can I get the name of an app, assuming that I have the
Assuming you have a DOM tree with nested tags, I would like to clean
Alright I have an xml document that looks something like this: <xml> <list> <partner>
I have an xml file like this: <uploads> <upload> <name>asd</name> <type>123</name> </upload> <upload> <name>qwe</name>
I have an XML document that looks like this: <kmsg xmlns=http://url1 xmlns:env=url1 xmlns:xsi=http://www.w3.org/2001/XMLSchemainstance xsi:schemaLocation=http://location
Assuming that I have a typedef declared in my .h file as such: typedef
Assuming that we have: http.HandleFunc(/smth, smthPage) http.HandleFunc(/, homePage) User sees a plain 404 page

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.