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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:33:34+00:00 2026-06-14T16:33:34+00:00

I’m rather new to coding xslt and have got rather stuck trying to do

  • 0

I’m rather new to coding xslt and have got rather stuck trying to do the following.

I have an xml file that has breeding info for horses broken into two main sections. 1. Horses node has the performance details of individual horses as well as an id to who their sire was. 2. Sires node is list of the Sires also holding breeding specific statistics.

I need to sort the list of sires based on the sum of the ‘stake’ money won by their foals (i.e. in the horses node).

So a cut down xml file looks like this:

<Horses>
    <Horse>
        <ID>1</ID>
        <Name>hrsA</Name>
        <SireID>101</SireID>
        <Pace>
            <Stakes>4800</Stakes>
        </Pace>
    </Horse>
    <Horse>
        <ID>2</ID>
        <Name>hrsB</Name>
        <SireID>102</SireID>
        <Pace>
            <Stakes>3600</Stakes>
        </Pace>
    </Horse>
    <Horse>
        <ID>3</ID>
        <Name>hrsC</Name>
        <SireID>102</SireID>
        <Pace>
            <Stakes>2800</Stakes>
        </Pace>
    </Horse>
    <Horse>
        <ID>4</ID>
        <Name>hrsD</Name>
        <SireID>101</SireID>
        <Pace>
            <Stakes>56</Stakes>
        </Pace>
    </Horse>
    <Horse>
        <ID>5</ID>
        <Name>hrsE</Name>
        <SireID>100</SireID>
        <Pace>
            <Stakes>20000</Stakes>
        </Pace>
    </Horse>
    <Horse>
        <ID>6</ID>
        <Name>hrsF</Name>
        <SireID>101</SireID>
        <Trot>
            <Stakes>20000</Stakes>
        </Trot>
    </Horse>
    <Horse>
        <ID>7</ID>
        <Name>hrsG</Name>
        <SireID>101</SireID>
        <Trot>
            <Stakes>559</Stakes>
        </Trot>
    </Horse>
    <Horse>
        <ID>8</ID>
        <Name>hrsH</Name>
        <SireID>102</SireID>
        <Pace>
            <Stakes>386</Stakes>
        </Pace>
        <Trot>
            <Stakes>10000</Stakes>
        </Trot>
    </Horse>
</Horses>
<Sires>
    <Sire>
        <ID>100</ID>
        <Name>srA</Name>
        <LiveFoalsALL>117</LiveFoalsALL>
    </Sire>
    <Sire>
        <ID>101</ID>
        <Name>srB</Name>
        <LiveFoalsALL>774</LiveFoalsALL>
    </Sire>
    <Sire>
        <ID>102</ID>
        <Name>srC</Name>
        <LiveFoalsALL>43</LiveFoalsALL>
    </Sire>
</Sires>

So summing the various stakes the output would have this ordering:

Sire 101 (srB) Stakes: $25415
Sire 100 (srA) Stakes: $20000
Sire 103 (srC) Stakes: $16768.

When I was just summing and ordering the individual horse stakes for other web pages I was able to use:

<xsl:apply-templates select="Horse">
    <xsl:sort select="sum(descendant::Stakes)" data-type="number"
        order="descending"/>
</xsl:apply-templates>

I just can’t figure out how to do the same for the sires referencing the horse node for summing to get the right ordering… possibly something like this where I try to say the Sire/ID is equal to the Horse/SireID:

<xsl:apply-templates select="Sire">
    <xsl:sort select="sum(//Horses/Horse[@SireID=ID]/descendant::Stakes)" 
        data-type="number" order="descending"/>
</xsl:apply-templates>

But that doesn’t work, the debugger jumps straight out of the current template when it hits the sort line so my syntax must be invalid.
I’ve been trying variations on this theme without success.

Can anyone please give me a pointer of how to call my Sire template and get the correct ordering?

Thanks,

Bryce Stenberg

  • 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-14T16:33:36+00:00Added an answer on June 14, 2026 at 4:33 pm

    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:key name="kOffspring" match="Horse" use="SireID"/>
    
     <xsl:template match="/*">
      <xsl:apply-templates select="Sires/Sire">
       <xsl:sort select="sum(key('kOffspring', ID)/*/Stakes)"
                 data-type="number" order="descending"/>
      </xsl:apply-templates>
     </xsl:template>
    
     <xsl:template match="Sire">
         Sire <xsl:value-of select="concat(ID,' (', Name, ') Stakes: ')"/>
       <xsl:value-of select="sum(key('kOffspring', ID)/*/Stakes)"/>
     </xsl:template>
     <xsl:template match="text()"/>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <t>
        <Horses>
            <Horse>
                <ID>1</ID>
                <Name>hrsA</Name>
                <SireID>101</SireID>
                <Pace>
                    <Stakes>4800</Stakes>
                </Pace>
            </Horse>
            <Horse>
                <ID>2</ID>
                <Name>hrsB</Name>
                <SireID>102</SireID>
                <Pace>
                    <Stakes>3600</Stakes>
                </Pace>
            </Horse>
            <Horse>
                <ID>3</ID>
                <Name>hrsC</Name>
                <SireID>102</SireID>
                <Pace>
                    <Stakes>2800</Stakes>
                </Pace>
            </Horse>
            <Horse>
                <ID>4</ID>
                <Name>hrsD</Name>
                <SireID>101</SireID>
                <Pace>
                    <Stakes>56</Stakes>
                </Pace>
            </Horse>
            <Horse>
                <ID>5</ID>
                <Name>hrsE</Name>
                <SireID>100</SireID>
                <Pace>
                    <Stakes>20000</Stakes>
                </Pace>
            </Horse>
            <Horse>
                <ID>6</ID>
                <Name>hrsF</Name>
                <SireID>101</SireID>
                <Trot>
                    <Stakes>20000</Stakes>
                </Trot>
            </Horse>
            <Horse>
                <ID>7</ID>
                <Name>hrsG</Name>
                <SireID>101</SireID>
                <Trot>
                    <Stakes>559</Stakes>
                </Trot>
            </Horse>
            <Horse>
                <ID>8</ID>
                <Name>hrsH</Name>
                <SireID>102</SireID>
                <Pace>
                    <Stakes>386</Stakes>
                </Pace>
                <Trot>
                    <Stakes>10000</Stakes>
                </Trot>
            </Horse>
        </Horses>
        <Sires>
            <Sire>
                <ID>100</ID>
                <Name>srA</Name>
                <LiveFoalsALL>117</LiveFoalsALL>
            </Sire>
            <Sire>
                <ID>101</ID>
                <Name>srB</Name>
                <LiveFoalsALL>774</LiveFoalsALL>
            </Sire>
            <Sire>
                <ID>102</ID>
                <Name>srC</Name>
                <LiveFoalsALL>43</LiveFoalsALL>
            </Sire>
        </Sires>
    </t>
    

    produces the wanted, correct result:

     Sire 101 (srB) Stakes: 25415
     Sire 100 (srA) Stakes: 20000
     Sire 102 (srC) Stakes: 16786
    

    Explanation:

    1. We define a key that specifies a Horse as a function of its SireID. This is useful in selecting all offspring of a given Sire just by providing its ID in a call to the standard XSLT key() function — like this: key('kOffspring', ID).

    2. Similarly, the sum of Stakes of all offspring of a given Sire is: sum(key('kOffspring', ID)/*/Stakes) .

    3. We apply templates to all Sire elements in the XML document and sort these by the decreasing values of the sums of the Stakes of their offsprings.

    4. For each Sire we output its ID, Name and the sum of Stakes of its offspring.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm parsing an XML file, the creators of it stuck in a bunch social
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I

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.