Hi Tod,
Described scenario is achievable with Scriptella, but be prepared to use Java (or JavaScript) for implementing complex parts of business logic. For complex migration scenarios I recommend a hybrid approach, when conceptual, workflow level tasks are defined in XML, while some specific transformations or business logic details are done in Java classes. So in your example you can start with something like this:
<connection id="db"/>
<connection id="text" driver="text"/> <!-- Use pure text driver to output XML - no validation or escaping. -->
<query connection-id="db"> <!-- start with main table1 -->
select * from table1
<script connection-id="text">
<main-record id="${id}">
</script>
<query> <!-- find all records from table2 with the same id -->
select * from table2 where id=?id
<script connection-id="text"> <!-- output them -->
<child-record>....</child-record>
</script>
<query connection-id="db">
<!-- For each records in table2, find child rows in Table4 using keys from Table2.
Assume Table4keysColumn contains comma separated list of IDs.
If it's not the case, consider writing helper Java method+use Janino or play with ${"a,b,c".replace(";", ",")} -->
select * from table4 where ID in ($Table4keysColumn);
<script connection-id="text">
<child-record4>....</child-record4>
</script>
</query>
</query>
<script connection-id="text">
</main-record>
</script>
</query>
Regards,
Fyodor