0

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>
1
  • If all you want to do is compare things, use difflib Commented Sep 9, 2013 at 5:04

1 Answer 1

1

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()

Sign up to request clarification or add additional context in comments.

1 Comment

Except that you would want to compare text and tail in addition to tags before checking the attributes, and go through nodes recursively.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.