in reply to XML-Twig: Tree building and printing
I have never used it this way, but I think you could use flush_up_to at the end of the inner loop to flush the part of the tree before the current element.