[WIP] Added support for declarative document types.

This isn't ready for merge at all but it shows a direction and code. Feedback wanted.