The answer to this question seems to be more elusive than it really should be.
I have the following ruby, encoding an XML string to its xml entity form: