I have a functioning solution in PHP. This one will query a database and replace placeholders within IDML templates. It also combines multiple IDML files. I may publish when I have added error handling.
Placeholders are wrapped in percent signs. %PlaceholderName%
1. For each result in the database query, PHP class unzips the selected template to a work folder with a unique name.
2. The Story_XXX.xml files that include placeholder names are selected.
3. Find/Replace is performed on the array of placeholders and the database results.
4. Once all of the placeholders have been handled, the story and spread Ids are made unique. This includes the filename and references within spreads. An array keeps track of these changes.
5. The first template folder is designated the combined workspace. All of the other template stories and spreads are dumped into the appropriate holders.
6. The combined workspace’s designmap.xml is opened as a DOMDocument and the new spreads and stories are added.
7. The combined document is zipped (The mimetype from the template is incorporated.)