open-xml-templating/docxtemplater

Ability to Insert IDs into XML Tags

Closed this issue · 1 comments

After using docxtemplater to replace placeholders with data in DOCX files, I then generate an HTML preview of the processed documents. I would like to be able to trace back from the HTML elements to the original XML tags replaced by docxtemplater.

Is there a feature or a recommended method to insert unique IDs into the XML tags surrounding replaced placeholders? This would help link the HTML elements directly to their original XML tags.

Hello,

I remember someone doing something very similar a few years back. (Maybe it was you ?). I could'n t find whether it was on a github issue or per email.

I think the way he did it at the time was, before passing the data to docxtemplater, he would open the word/document.xml, and add unique ids to each <w:t> tag.

Then you could generate your docx from that same zip file.

Something like this (untested code, but likely should work) :

const zip = new JSZip(buf);
const text = zip.file("word/document.xml").asText();
const { DOMParser, XMLSerializer } = require('@xmldom/xmldom')
const document = new DOMParser().parseFromString(text, "text/xml");
const textTags = document.getElementsByTagName("w:t");
let trackedId = 1;
for (let i = 0, len = textTags.length; i < len; i++) {
	const textTag = textTags[i];
	textTag.setAttribute("_trackedId", trackedId++)
}
const newText = new XMLSerializer().serializeToString(document);
zip.file("word/document.xml", newText);
const doc = new Docxtemplater(zip, { paragraphLoop: true, linebreaks: true });
doc.render({
     first_name: "John",
});

I'm not exactly sure how you then convert your XML to HTML and keep the tags, if you have more insights, please post them here so that future users can also benefit from this !