Grouping xml nodes by value of a child in Xsl

前端 未结 3 1067
攒了一身酷
攒了一身酷 2021-02-04 06:36


1
first



2
second         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-04 06:58

    I. Here is a complete and very short XSLT 1.0 solution:

    
     
    
     
     
     
     
      
       
      
     
    
     
    
      
       
      
     
     
     
    
    
    

    when this transformation is applied on the provided XML document:

    
        
            1
            first
        
        
            2
            second
        
        
            3
            first
        
    
    

    the wanted, correct result is produced:

    
        
            1
            first
        
            3
            first
        
        
            2
            second
        
    
    

    Do note:

    1. The use of the Muenchian method for grouping. This is the most efficient grouping method in XSLT 1.0.

    2. The use of AVT (Attribute Value Template) to specify a literal result element and its variable - value attribute as one whole. Using AVTs simplifies coding and yields shorter and more understandable code.

    II. An even shorter XSLT 2.0 solution:

    
        
    
     
         
          
           
            
           
          
         
     
    
    

    when this transformation is applied on the same XML document (above), the same correct result is again produced.

    Do note:

    .1. The use of the XSLT 2.0 instruction.

    .2. The use of the standard XSLT 2.0 functions current-group() and current-grouping-key()

提交回复
热议问题