sarbjit
sarbjit

Reputation: 3894

Comparing two xml using python ignoring particular attribute

I am looking for a python based solution to compare two xml's ignoring a particular attribute value. For example, the below xml's should be treated as identical though the Ref and ID values are different as these would be different in each xml. One of the solution could be to substitue these with empty strings first and then compare xml's. Is there any library available in python which can do this while comparing the xmls.

#XML1:

<Objects>
   <Object Name="Object1" Ref="12345">
        <Item Name="Item1" value="Value1"/>
    </Object>
</Objects>

<RefTable>
    <Refitem ID="12345" Name="Item1"/>
</RefTable>


#XML2:

<Objects>
   <Object Name="Object1" Ref="54321">
        <Item Name="Item1" value="Value1"/>
    </Object>
</Objects>

<RefTable>
    <Refitem ID="54321" Name="Item1"/>
</RefTable>

Upvotes: 0

Views: 166

Answers (1)

user2618501
user2618501

Reputation:

Something like this could work:

root1 = etree.fromstring(xml1)
root2 = etree.fromstring(xml2)
for node1, node2 in zip(root1.iter(), root2.iter()):

   if node1.tag == node2.tag:
       a1 = node2.attrib
       a2 = node2.attrib

       if node1.tail != node2.tail:
           raise ValueError('XML differs')

       for ignored in ('ID',):

           try:
              del a1[ignored]
           except AttributeError:
              pass

           try:
              del a2[ignored]
           except AttributeError:
              pass

        if a1 != a2:
           raise ValueError('XML differs')
    else:
        raise ValueError('XML differs')

Instead of izip() you might need to use itertools.izip_longest()

Upvotes: 1

Related Questions