Some tags in XML files are not getting merged

Hi


I am using mail merge with MustacheTemplate and syntax.
I give xml as a input and use foreach in the template to do the mail merge operation.For some tags the mail merge is not working properly , instead of xml data a mail merge field is getting created , the data in the xml file is not present.

I have attached the source code , xml file and the output document .

This is my sample code

public class MustacheTemplateSyntax
{

public static void main(String[] args) throws Exception
{
// The path to the documents directory.
String dataDir = Utils.getDataDir(MustacheTemplateSyntax.class);

// Use DocumentBuilder from the javax.xml.parsers package and Document class from the org.w3c.dom package to read
// the XML data file and store it in memory.
javax.xml.parsers.DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();

// Parse the XML data.
org.w3c.dom.Document xmlData = db.parse(dataDir + “text1.xml”);

// Open a template document.
Document doc = new Document(dataDir + “VendorTemplate.doc”);

doc.getMailMerge().setUseNonMergeFields(true);

// Note that this class also works with a single repeatable region (and any nested regions).
// To merge multiple regions at the same time from a single XML data source, use the XmlMailMergeDataSet class.
// e.g doc.getMailMerge().executeWithRegions(new XmlMailMergeDataSet(xmlData));
doc.getMailMerge().executeWithRegions(new XmlMailMergeDataSet(xmlData));

// Save the output document.
doc.save(dataDir + “Output.docx”);

System.out.println(“Mail merge performed successfully.”);
}


}
Hi Spencer,

Thanks for your inquiry. Please use MailMerge.PreserveUnusedTags property to get or set a value indicating whether the unused "mustache" tags should be preserved.

doc.getMailMerge().setPreserveUnusedTags(true);
doc.getMailMerge().executeWithRegions(new XmlMailMergeDataSet(xmlData));

You may use MailMerge.CleanupOptions to remove unused fields from the document. Hope this helps you.

<pre style=“font-family: “Courier New”; font-size: 9pt; background-color: rgb(255, 255, 255);”>doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS);
<span style=“font-family: “Courier New”; font-size: 9pt; background-color: rgb(255, 255, 255);”>doc.getMailMerge().<span style=“font-family: “Courier New”; font-size: 9pt; background-color: rgb(228, 228, 255);”>executeWithRegions<span style=“font-family: “Courier New”; font-size: 9pt; background-color: rgb(255, 255, 255);”>(<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 0, 128); font-weight: bold;”>new <span style=“font-family: “Courier New”; font-size: 9pt; background-color: rgb(255, 255, 255);”>XmlMailMergeDataSet(xmlData));

I dont want to clean up the tags…

I have the value for the the used mustache tag in the xml file. But Instead of printing the value in the xml file to the output document the mustache tag is getting printed to the final output.
Please have a look at the attached document and the code snippet .


Thanks
spencer
Hi Spencer,

Thanks for your inquiry. Aspose.Words merges certification and spencerIssued fields correctly. The imgUrl is not merged because it is not in XML file.

If you are facing different issue, please share some more detail about that along with expected output. We will then provide you more information about your query.