Couldn't you do two passes against the file? First create a twig with the start_tag_handler to get whatever it is you need. Then use that information to create a second twig for the actual processing. Would that work or am I missing something? As mirod mentions some sample code and data might help clear things up.