Free Support Forum - aspose.com

Looking for advise on having a field in the output only if it is present in the input xml


#1

To describe the problem with an example,
if my input xml is

<Root>
    <Item>
      <Name>_name_1</Name>
      <Description>_desc1</Description>
    </Item>
    <Item>
      <Name>_name_2</Name>
    </Item>
</Root>

Note Description is optional in ‘Item’.

and template doc looks like

|
{{#foreach Item}}
Name:
{{Name}}
Desc:
{{Description}}

{{/foreach Item}}
|

Want to exclude
Desc:

{{Description}}

from the output if ‘Item’ does not have ‘Description’ element.

Assuming achievable using the ‘IF’
Looking for an example that will do the job. Thanks.


#2

@hyerrabapu,

Do you want to use LINQ Reporting Engine or the regular Mail Merge and Reporting feature of Aspose.Words for .NET?

In case of regular Mail Merge, you can get idea from the if-else ‘Mustache’ template syntax example, please see the sample input, output documents and screenshot in this archive (Mustache-Template-Syntax.zip (38.5 KB)) and try running the following code:

Document doc = new Document("E:\\Mustache-Template-Syntax\\input.docx");
doc.MailMerge.UseNonMergeFields = (true);
doc.MailMerge.Execute(new String[] { "GENDER" }, new Object[] { "MALE" });
doc.Save("E:\\Mustache-Template-Syntax\\awjava-18.12.docx");

Hope, this helps in achieving what you are looking for.


#3

Managing adding a field and having a ‘IF’ for it. Does the job. Thanks


#4

@hyerrabapu,

It is great that you were able to find what you were looking for. Please let us know any time you have any further queries.