[xml mode] Rewrite state machine to be simpler and faster

Tangentially related to issue #2023
[xml mode] Reset state when giving up on a bogus tag

Issue #2023