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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:32:17+00:00 2026-06-14T19:32:17+00:00

this question follows on from previous thread: XSLT: Sorting based on sum of values

  • 0

this question follows on from previous thread: XSLT: Sorting based on sum of values from other nodes

I can now get my data summed from the other nodes by using keys.
What I can’t seem to get is the syntax or method needed to now apply conditions to select the data I’m after while using these keys.

Here is a simplified xml I’m using (modified from last one to highlight issues):

<Horses>
    <Horse>
        <ID>1</ID>
        <Name>hrsA</Name>
        <SireID>101</SireID>
        <Age>3</Age>
        <Pace>
            <Stakes>20</Stakes>
            <Wins>0</Wins>
        </Pace>
    </Horse>
    <Horse>
        <ID>2</ID>
        <Name>hrsB</Name>
        <SireID>101</SireID>
        <Age>6</Age>
        <Pace>
            <Stakes>1600</Stakes>
            <Wins>9</Wins>
        </Pace>
    </Horse>
    <Horse>
        <ID>3</ID>
        <Name>hrsC</Name>
        <SireID>101</SireID>
        <Age>3</Age>
        <Trot>
            <Stakes>200</Stakes>
            <Wins>2</Wins>
        </Trot>
    </Horse>
    <Horse>
        <ID>4</ID>
        <Name>hrsD</Name>
        <SireID>101</SireID>
        <Age>4</Age>
        <Pace>
            <Stakes>50</Stakes>
            <Wins>0</Wins>
        </Pace>
        <Trot>
            <Stakes>100</Stakes>
            <Wins>1</Wins>
        </Trot>
    </Horse>
    <Horse>
        <ID>5</ID>
        <Name>hrsE</Name>
        <SireID>101</SireID>
        <Age>3</Age>
        <Pace>
            <Stakes>100</Stakes>
            <Wins>1</Wins>
        </Pace>
        <Trot>
            <Stakes>300</Stakes>
            <Wins>1</Wins>
        </Trot>
    </Horse>
</Horses>
<Sires>
    <Sire>
        <ID>101</ID>
        <Name>srA</Name>
        <LiveFoalsALL>117</LiveFoalsALL>
    </Sire>
</Sires>

In one scenario I need to get the offspring (Horses) belonging to one Sire that are of a certain age.

With no age specified I use a key that sorts on stakes won like so (thanks to Dimitre):

<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">
    <!-- get the data I want -->
 </xsl:template>
</xsl:stylesheet>

Now, in the ‘get the data I want’ I’m trying to only get 3 year old horses (Age=3)
For instance, to only get srA’s 3 year old winning offspring (answer = 2) I need something like this I’m thinking:

<xsl:value-of select="count(key('kOffspring', ID)[Age=3]/*/Wins)"/>

but that does not work… Or do I need to create a new key for each age I want to use with some conditional syntax(?) like:

<xsl:key name="kOffspring" match="Horse[/Age=3]" use="SireID"/>

As you can see I don’t really know what I’m doing or if it is even possible 🙂

.

The other scenario in a similar vein is needing to count how many horses were winners – answer=4 (not the number of wins, just whether they won at pace or trot … a third scenario requires either just at pace or just at trot).

I tried this in the Sire template with:

<xsl:value-of select="count(key('kOffspring', ID)/*/Wins &gt; 0)"/>

but that only ever returns a count of one.

If any experts here could help get me going I would appreciate that. I am finding xslt syntax a little confusing and hard to remember compared to other programming languages – I hope I get the swing of it soon as it is very powerful.

Regards,
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-14T19:32:18+00:00Added an answer on June 14, 2026 at 7:32 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)"/>
         3 year old winning offspring: <xsl:value-of
         select="count(key('kOffspring', ID)[Age = 3 and */Wins > 0])"/>
         Offspring that ever were a winner: <xsl:value-of
          select="count(key('kOffspring', ID)[*/Wins > 0])"/>
     </xsl:template>
    </xsl:stylesheet>
    

    When applied on the provided XML (the provided fragment, enclosed in a single top element to make it a well-formed XML document):

    <t>
        <Horses>
            <Horse>
                <ID>1</ID>
                <Name>hrsA</Name>
                <SireID>101</SireID>
                <Age>3</Age>
                <Pace>
                    <Stakes>20</Stakes>
                    <Wins>0</Wins>
                </Pace>
            </Horse>
            <Horse>
                <ID>2</ID>
                <Name>hrsB</Name>
                <SireID>101</SireID>
                <Age>6</Age>
                <Pace>
                    <Stakes>1600</Stakes>
                    <Wins>9</Wins>
                </Pace>
            </Horse>
            <Horse>
                <ID>3</ID>
                <Name>hrsC</Name>
                <SireID>101</SireID>
                <Age>3</Age>
                <Trot>
                    <Stakes>200</Stakes>
                    <Wins>2</Wins>
                </Trot>
            </Horse>
            <Horse>
                <ID>4</ID>
                <Name>hrsD</Name>
                <SireID>101</SireID>
                <Age>4</Age>
                <Pace>
                    <Stakes>50</Stakes>
                    <Wins>0</Wins>
                </Pace>
                <Trot>
                    <Stakes>100</Stakes>
                    <Wins>1</Wins>
                </Trot>
            </Horse>
            <Horse>
                <ID>5</ID>
                <Name>hrsE</Name>
                <SireID>101</SireID>
                <Age>3</Age>
                <Pace>
                    <Stakes>100</Stakes>
                    <Wins>1</Wins>
                </Pace>
                <Trot>
                    <Stakes>300</Stakes>
                    <Wins>1</Wins>
                </Trot>
            </Horse>
        </Horses>
        <Sires>
            <Sire>
                <ID>101</ID>
                <Name>srA</Name>
                <LiveFoalsALL>117</LiveFoalsALL>
            </Sire>
        </Sires>
    </t>
    

    produces the wanted, correct result:

     Sire 101 (srA) Stakes: 2370
     3 year old winning offspring: 2
     Offspring that ever were a winner: 4
    

    Alternatively, a probably more efficient solution is to use another key that is composite and its two parts are the SireID and the Age:

    <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:key name="kOffspringBySireIdAndAge" match="Horse"
      use="concat(SireID, '+', Age)"/>
    
     <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)"/>
         3 year old winning offspring: <xsl:value-of
         select="count(key('kOffspringBySireIdAndAge', concat(ID, '+3'))
                           [*/Wins > 0]
                      )"/>
         Offspring that ever were a winner: <xsl:value-of
          select="count(key('kOffspring', ID)[*/Wins > 0])"/>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the same XML document (above), the same correct result is produced:

     Sire 101 (srA) Stakes: 2370
     3 year old winning offspring: 2
     Offspring that ever were a winner: 4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question follows on from a previous question which can be found here: Need
This question follows on from a previous question, that has raised a further issue.
This question follows on from a previous question. However stackoverflow presents me from commenting
This problem follows on from a previous question . When I run the following
This question follows directly from my previous question here in SO . I think
This follows on from my previous question about Moose structured types. I apologise for
This question follows on from a previous question... How do I create the current
So this question follows on from my previous question Under what conditions does a
This follows on from a previous question here where I asked how to develop
OK, this kind of follows on from my previous question . What I would

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.