Yes I liked your solution, which I found earlier via web search. Definitely cleaner than mine. It doesn't however, handle the truly pathological case of a malformed DOCTYPE decl as in:
<!DOCTYPE TEI PUBLIC "name removed to protect my client">
I guess a url following the name is required in this case.
Perhaps it's a *bad idea*, but I am trying to allow this by ignoring it. At least the angle brackets match. Sigh.