Distinct elements and grouping

前端 未结 3 896
一生所求
一生所求 2020-12-10 15:20

Given the following xml fragment:


  
    file1
    desc1
  &l         


        
3条回答
  •  北海茫月
    2020-12-10 16:17

    This solution is a little bit simpler, more efficient and at the same time more general than the one presented by Richard:

    This transformation:

    
    
     
    
     
    
        
         
          
          
            

    when applied to the provided XML document:

    
        
            file1
            desc1
        
        
            file1
            desc2
        
        
            file2
            desc1
        
    
    

    Produces the wanted result:

    
       
          

    file1

    desc1

    desc2

    file2

    desc1

    Do note the simple match pattern of the first and how, using a second , we locate all "Description" elements that are siblings of a "File" element that has a given value.

    We could have used more templates instead of pull-processing, however this is a quite simple case and the solution really benefits from shorter, more compact and more readable code.

    Also note, that in XSLT 2.0 one will typically use the instruction instead of the Muenchian method.

提交回复
热议问题