Distinct elements and grouping

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

Given the following xml fragment:


  
    file1
    desc1
  &l         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-10 15:58

    Here's how I'd do it, using the Muenchean method. Google 'xslt muenchean' for more info from smarter people. There might be a clever way, but I'll leave that to others.

    One note, I avoid using capitals at the start of xml element names, eg 'File', but that's up to you.

    
    
        
        
        
            
                
                    
                
            
        
        
            
                
                

    The idea is, key each File element using it's text value. Then only display the file values if they are the same element as the keyed one. To check if they're the same, use generate-id. There is a similar approach where you compare the first element that matches. I can't tell you which is more efficient.

    I've tested the code here using Marrowsoft Xselerator, my favorite xslt tool, although no longer available, afaik. The result I got is:

    
    
    

    file1

    desc1

    desc2

    file2

    desc1

    This is using msxml4.

    I have sorted the output by File. I'm not sure if you wanted that.

    I hope this helps.

提交回复
热议问题