better handling for misplaced elements in XML