Look in packages/cms/www/modules/items/export-xml.tcl. This is used to export a content revision, but it could be used as model for creating what you want. It will only work for oracle, as postgresql doesn't include xml utilities. Probably, these routines should be rewritten to use ns_xml, so both the oracle and postgresql versions can use the same code. The corresponding sql routine is content_revision.export_xml, and it is located in packages/acs-content-repository/sql/oracle/content-revision.sql.