I've now finished the second stage of this work, with nest_data.xsl complete and working; all mortgages, lenders and associated documents are now organized into a structured XML document which can be used to generate SQL. In the process, I had to manually fix some of the column headers in unwrapped_fods.xml, because they were inconsistent in terms of spelling and case; that means the original unwrap_fods.xsl can't be run again to re-run the whole process without doing the same manual intervention. However, I don't think that will be necessary, since the data in unwrapped_fods.xml is complete. So now we have unwrapped_fods_nested.xml, and I can start generating SQL from that.