使用OpenTBS生成docx时,文本框内block标签消失引发XML解析错误
I've run into this exact quirky issue before—Word's text box XML structure behaves differently from regular document content, and it can throw off OpenTBS's ability to parse block tags correctly. Let's break down the problem and fix it:
1. Why This Happens
Word stores text box content in nested, fragmented XML nodes (usually under <w:textbox> in word/document.xml or related drawing files). When you place a block=tbs:row tag directly inside a cell's text, Word's formatting often splits the tag across multiple <w:t> (text) elements. OpenTBS needs the full block tag to be a single, uninterrupted string to process it, so this splitting causes the start tag to vanish entirely, leading to XML parse errors.
2. The Quick Fix: Move the Block Tag to the Table Row
Instead of embedding the block tag in the cell text, attach it directly to the table row itself. This avoids the text node splitting issue because the row's XML node is a single, distinct element that OpenTBS can reliably process.
Template Adjustment Steps:
- In your Word template, target the table row that needs repeating.
- Replace your cell content:
With this setup:[domain.id1;block=tbs:row] : [domain.id2]- Add the block logic to the table row (you can edit this via OpenTBS's template tools, or manually tweak the XML):
<w:tr [onload;block=tbs:row;when [domain.id1]]> - Then in the cell, keep only the data placeholders:
[domain.id1] : [domain.id2]
- Add the block logic to the table row (you can edit this via OpenTBS's template tools, or manually tweak the XML):
3. Update OpenTBS to the Latest Version
Older versions of OpenTBS had limited support for block tags in complex Word elements like text boxes. Many of these parsing bugs have been fixed in recent releases, so grabbing the latest version of OpenTBS might resolve the issue immediately without template changes.
4. Manual XML Check (If Issues Persist)
If the above steps don't work, verify the XML structure to ensure the tag isn't split:
- Rename your
.docxfile to.zipand extract its contents. - Open
word/document.xml(or the relevant drawing file if the text box is stored separately) in a text editor. - Search for your block tag—confirm
[domain.id1;block=tbs:row]lives entirely within a single<w:t>element, not split across multiple nodes. - If it's split, edit the XML to combine the fragmented text nodes into one, rezip the file, and test again.
Your PHP code looks solid—your $domains array is structured correctly, and you're loading the template with the proper encoding flag. The issue is purely with how the block tag is positioned in the Word template's XML structure.
内容的提问来源于stack exchange,提问作者Matt

