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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:59:12+00:00 2026-05-19T04:59:12+00:00

UPDATE – New code at the bottom I’m trying to figure out how to

  • 0

UPDATE – New code at the bottom

I’m trying to figure out how to use the sort function to pull the most recent record from some XML data. I’m very new to using XSLT and am running into a bunch of problems. Here’s an example of my data…

<content date="1/13/2011 1:21:00 PM">
    <collection vo="promotion">
        <data vo="promotion" promotionid="64526" code="101P031" startdate="1/7/2011 12:00:00 AM"/>
        <data vo="promotion" promotionid="64646" code="101P046" startdate="1/9/2011 12:00:00 AM"/>
    </collection>
</content>

What I want to do is sort the data by promotionid in decsending order and then ONLY ouput via HTML the promotionid that is greatest. Here is along the lines of what I was trying

UPDATE – This is the latest version of the code that is still experiencing problems.

<html><body>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"  encoding="UTF-8" />
    <xsl:template match="content/collection/data">
        <xsl:apply-templates>
            <xsl:sort select="promotionid" order="descending" data-type="number" />
        </xsl:apply-templates>
    </xsl:template>
    <xsl:template match="content/collection">
        <xsl:value-of select="data/@promotionid" />
    </xsl:template> 
</xsl:stylesheet>
</body></html>

While this does return results what I am getting back is ‘64526’ and NOT ‘64646’.

Can anyone help? Also I’ve seen examples online where you can sort by multiple fields. It may be worth noting now, rather then asking later, that we may want to end up sorting by startdate rather than promotionid. I have managed to come up with code to break out the date by YYYY, MM, and DD, but have no idea how I would even begin to use that aside from using those as my select paramater of the sort, but I don’t know if that actually works or not.

Year
<xsl:value-of select="substring(substring-after(substring-after(data/@startdate,'/'),'/'),1,4)" />

Month
<xsl:value-of select="substring-before(data/@startdate,'/')" />

Day
<xsl:value-of select="substring-before(substring-after(data/@startdate,'/'),'/')" />

Thank in advance and I appologize my less than novice XSLT skills.

——————————————————

After some help here the code has changed, but is still not working as intended. Here is the code…

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"  encoding="UTF-8" />
    <xsl:template match="content/collection/">
            <xsl:apply-templates>
                <xsl:sort select="@promotionid" order="descending" data-type="number" />
            </xsl:apply-templates>
        </xsl:template>

 <xsl:template match="content/collection/data">
        <xsl:if test="position()=1">
                   <xsl:value-of select="@promotionid"/>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

And I am still seeing the lesser value ouput rather than the greater. Perhaps there is another way to do this with out sorting? As I am open to that possibility as well.

1/14/11 10:37 Update
*——————————————————————-*
Okay using this code now does indeed sort the data and output the highest promotionid number. Thanks a Ton!

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:template match="node()|@*">
      <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="collection">
      <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:apply-templates select="data">
         <xsl:sort select="@promotionid" data-type="number" order="descending"/>
        </xsl:apply-templates>
      </xsl:copy>
    </xsl:template>
<xsl:template match="content/collection/data">
        <xsl:if test="position()=1">
                   <xsl:value-of select="@promotionid"/>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

Ignoring the promtionid now can you show me how I would sort, descending, by JUST the date? I tried removing Unfortunately I know the dates should have a static length, but we have no control of the data we receive 🙁

Also can you recommend a book to start with to really can some better understanding of all this? You’ve been a tremendous help!

  • 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-19T04:59:12+00:00Added an answer on May 19, 2026 at 4:59 am
    <xsl:sort select="promotionid" order="descending" data-type="number"
    

    />

    There is an obvious error here: promotionid is an attribute, not an element.

    Solution:

    select="@promotionid" order="descending" data-type="number" />
    

    Another error:

    <xsl:template match="content/collection/data">
        <xsl:apply-templates>
            <xsl:sort select="promotionid" order="descending" data-type="number" />
        </xsl:apply-templates>
    </xsl:template>
    

    The <xsl:apply-templates> and sorting is performed too-late.

    You want:

    <xsl:template match="content/collection/">
                <xsl:apply-templates>
                    <xsl:sort select="@promotionid" order="descending" data-type="number" />
                </xsl:apply-templates>
            </xsl:template>
    

    and

        <xsl:template match="content/collection/data">
            <xsl:if test="position()=1">
                       <xsl:value-of select="@promotionid"/>
            </xsl:if>
        </xsl:template>
    

    As for your expanded question:

    This transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
        <xsl:template match="node()|@*">
          <xsl:copy>
             <xsl:apply-templates select="node()|@*"/>
          </xsl:copy>
        </xsl:template>
    
        <xsl:template match="collection">
          <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates select="data">
             <xsl:sort select="@promotionid" data-type="number" order="descending"/>
             <xsl:sort select=
              "concat(
                 substring-after(substring-after(substring-before(@startdate,' ')
                                                 ,'/'
                                                 ),
                                   '/'
                                   ),
                 substring-before(substring-after(substring-before(@startdate,' ')
                                                 ,'/'
                                                 ),
                                  '/'
                                ),
                 substring-before(substring-after(substring-before(@startdate,' ')
                                                 ,'/'
                                                 ),
                                   '/'
                                   )
                    )"/>
            </xsl:apply-templates>
          </xsl:copy>
        </xsl:template>
    </xsl:stylesheet>
    

    when applied to the provided XML document:

    <content date="1/13/2011 1:21:00 PM">
        <collection vo="promotion">
            <data vo="promotion" promotionid="64526" code="101P031" startdate="1/7/2011 12:00:00 AM"/>
            <data vo="promotion" promotionid="64646" code="101P046" startdate="1/9/2011 12:00:00 AM"/>
        </collection>
    </content>
    

    produces the wanted result:

    <content date="1/13/2011 1:21:00 PM">
    
       <collection vo="promotion">
          <data vo="promotion" promotionid="64646" code="101P046" startdate="1/9/2011 12:00:00 AM"/>
          <data vo="promotion" promotionid="64526" code="101P031" startdate="1/7/2011 12:00:00 AM"/>
       </collection>
    
    </content>
    

    However, note that this well not handle the variable length for the date components. It is better to use fixed length format: mm/dd/yyyy

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

Sidebar

Related Questions

UPDATE: Turns out my code works. Browser was caching previous failed response. Thanks for
Update: Solved, with code I got it working, see my answer below for the
Update: Check out this follow-up question: Gem Update on Windows - is it broken?
Update: Now that it's 2016 I'd use PowerShell for this unless there's a really
Update : this is more-or-less a dupe , and it turns out to be
UPDATE : I got my O3D situation straightened out, but I have yet to
Update: As someone pointed out I was missing an s in my Route registration.
[UPDATE] Thanks guys,final code: var EUR_share_cost = 0; var USD_share_cost = 0; var GBP_share_cost
update Since one effect of these functions is to provide a way to use
UPDATE : Answer at the bottom. Hi Guys, How to initialize an 'array of

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.