correctly assemble xml document tree