The code that failed (minimal set as we resolved down to the not working stmt) added to the document.bplt :
1) escape all as CDATA section
Initially we had isolated that it was the "for" loop that was failing. The first workaround we coded used a recursive call to do the looping - that did work.
But after feedback from Engineering it made sense what the problem was - with hindsight the answer is obvious.