#NEWLINE# def compare_xml( want , got ) : #NEWLINE# #INDENT# "Tries to do a 'xml-comparison' of want and got. Plain string\n    comparison doesn't always work because, for example, attribute\n    ordering should not be important. Comment nodes are not considered in the\n    comparison.\n\n    Based on http://codespeak.net/svn/lxml/trunk/src/lxml/doctestcompare.py\n    " #NEWLINE# _norm_whitespace_re = re . compile ( '[ \\t\\n][ \\t\\n]+' ) #NEWLINE# def norm_whitespace( v ) : #NEWLINE# #INDENT# return _norm_whitespace_re . sub ( ' ' , v ) #NEWLINE# #UNINDENT# def child_text( element ) : #NEWLINE# #INDENT# return '' . join ( [ c . data for c in element . childNodes if ( c . nodeType == Node . TEXT_NODE ) ] ) #NEWLINE# #UNINDENT# def children( element ) : #NEWLINE# #INDENT# return [ c for c in element . childNodes if ( c . nodeType == Node . ELEMENT_NODE ) ] #NEWLINE# #UNINDENT# def norm_child_text( element ) : #NEWLINE# #INDENT# return norm_whitespace ( child_text ( element ) ) #NEWLINE# #UNINDENT# def attrs_dict( element ) : #NEWLINE# #INDENT# return dict ( element . attributes . items ( ) ) #NEWLINE# #UNINDENT# def check_element( want_element , got_element ) : #NEWLINE# #INDENT# if ( want_element . tagName != got_element . tagName ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# if ( norm_child_text ( want_element ) != norm_child_text ( got_element ) ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# if ( attrs_dict ( want_element ) != attrs_dict ( got_element ) ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# want_children = children ( want_element ) #NEWLINE# got_children = children ( got_element ) #NEWLINE# if ( len ( want_children ) != len ( got_children ) ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# for ( want , got ) in zip ( want_children , got_children ) : #NEWLINE# #INDENT# if ( not check_element ( want , got ) ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# #UNINDENT# return True #NEWLINE# #UNINDENT# def first_node( document ) : #NEWLINE# #INDENT# for node in document . childNodes : #NEWLINE# #INDENT# if ( node . nodeType != Node . COMMENT_NODE ) : #NEWLINE# #INDENT# return node #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# ( want , got ) = strip_quotes ( want , first_node ) #NEWLINE# want = want . replace ( '\\n' , '\n' ) #NEWLINE# got = got . replace ( '\\n' , '\n' ) #NEWLINE# if ( not want . startswith ( '<?xml' ) ) : #NEWLINE# #INDENT# wrapper = '<root>%s</root>' #NEWLINE# want = ( wrapper % want ) #NEWLINE# got = ( wrapper % got ) #NEWLINE# #UNINDENT# want_root = first_node ( parseString ( want ) ) #NEWLINE# got_root = first_node ( parseString ( got ) ) #NEWLINE# return check_element ( want_root , got_root )