Removing duplicates in xml with xslt

前端 未结 4 774
有刺的猬
有刺的猬 2021-01-06 01:08

I need to remove duplicates in the following xml:


          


        
4条回答
  •  走了就别回头了
    2021-01-06 01:30

    If you interested in how this is achieved using Muenchian Grouping, which is a common technique in XSLT, you first need to define a 'key' to identify duplicate books within a row.

    
    

    In this I am achieving this using a concatenated key of RowID, BookType and BookName. The key will contain a list of books all with that particular value of key. Do note the use of the # character as the joining character. If there is any chance of # appearing in your XML, you will need to pick another character (or string).

    Now when you are matching on book elements, you can check for duplicates like so

    
    
    

    In other words, is this book element the first element in our key.

    Here is the full XSLT

    
    
       
       
    
       
          
          
             
                
             
          
       
    
       
          
             
          
       
    
    

    Also note the use of the identity transform so that other nodes can be copied without having to explicitly reference them. When this XSLT is applied to your input, the following output is generated:

    
       ADOA-XssK
       
          
             Brand
             jon
          
       
    
    

    EDIT: I have amended the XSLT to remove an unnecessary template match.

提交回复
热议问题