One other thought about the format.
In thinking about this, it occurred to me that there might be reasons to have different tags for different sorts of insets. E.g.:
<text> <command> <math>This would save the need to do some long switch on the "type" before we can figure out where to go next. All the command insets are going to be processed in the same way, more or less, so knowing immediately that we had one would be good.
Richard