Open source command line tool for Linux to diff XML files ignoring element order

后端 未结 6 1551
走了就别回头了
走了就别回头了 2021-02-04 09:57

Is there an open source command-line tool (for Linux) to diff XML files which ignores the element order?

Example input file a.xml:



        
6条回答
  •  时光取名叫无心
    2021-02-04 10:41

    First your XML examples are not valid, because they lack a root element. I added a root element. This is a.xml:

    
    
        
            
            
            
        
        
            
            
        
        
            
            
        
    
    

    And this is b.xml:

    
    
        
            
            
            
        
        
            
            
        
        
            
            
        
    
    

    You can create a canonical form for the comparison by merging the siblings with the same name attribute and sorting by the tag name and the value.

    In order to merge the sibling elements with the same name you have to ignore the elements which name is the same like a preceding sibling and take the remaining. This can be done on the second element level by the following Xpath:

    *[not(@name = preceding-sibling::*/@name)]
    

    You have to take the name of those elements in order to select all the child elements which have a parent with this name. After that you have to sort by name and value. This makes it possible to transform both files into this canonical form:

    
    
        
            
            
            
        
        
            
            
            
            
        
    
    

    This will do the transformation:

    
    
        
        
        
            
                    
                    
                        
                        
                            
                            
                                
                                
                                
                                    
                                
                            
                        
                    
            
        
    
    

提交回复
热议问题