XSLT: Sorting based on sum of values from other nodes

后端 未结 2 652
抹茶落季
抹茶落季 2021-01-20 03:27

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 sectio

2条回答
  •  隐瞒了意图╮
    2021-01-20 03:48

    This transformation:

    
     
     
    
     
      
       
      
     
    
     
         Sire 
       
     
     
    
    

    when applied on the provided XML document:

    
        
            
                1
                hrsA
                101
                
                    4800
                
            
            
                2
                hrsB
                102
                
                    3600
                
            
            
                3
                hrsC
                102
                
                    2800
                
            
            
                4
                hrsD
                101
                
                    56
                
            
            
                5
                hrsE
                100
                
                    20000
                
            
            
                6
                hrsF
                101
                
                    20000
                
            
            
                7
                hrsG
                101
                
                    559
                
            
            
                8
                hrsH
                102
                
                    386
                
                
                    10000
                
            
        
        
            
                100
                srA
                117
            
            
                101
                srB
                774
            
            
                102
                srC
                43
            
        
    
    

    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.

提交回复
热议问题