Hi,
When iterating an element array from the datasource the report is coming as blank for the below input xml and the template.
Input Xml
<data>
<testData>
<persons>
<department>IT</department>
<count>3</count>
<person>
<name>John Doe</name>
<age>30</age>
<birth>1989-04-01 4:00:00 pm</birth>
</person>
<person>
<name>Jane Doe</name>
<age>27</age>
<birth>1992-01-31 07:00:00 am</birth>
</person>
<person>
<name>John Smith</name>
<age>51</age>
<birth>1968-03-08 1:00:00 pm</birth>
</person>
</persons>
<additonalData>
<data>
<persons>
<person>1</person>
</persons>
<person>2</person>
</data>
</additonalData>
<subData>
<person>3</person>
</subData>
</testData>
</data>
Word Template Content
<<var [ppls = data.testData.persons.person]>>
<<foreach [in ppls]>>
<<[name]>>
<<[age]>>
<</foreach>>
But when removing person tags under additionalData and subData tag, data gets populated in the generated file.
Here is the report engine code,
XmlDataLoadOptions loadOptions = new XmlDataLoadOptions();
loadOptions.setAlwaysGenerateRootObject(true);
XmlDataSource xmlDataSource = new XmlDataSource(inputXmlBytes, loadOptions);
ReportingEngine engine = new ReportingEngine();
engine.getKnownTypes().add(ControlChar.class);
engine.setOptions(ReportBuildOptions.ALLOW_MISSING_MEMBERS | ReportBuildOptions.REMOVE_EMPTY_PARAGRAPHS | ReportBuildOptions.INLINE_ERROR_MESSAGES);
engine.buildReport(document, xmlDataSource, "data");
Aspose words version - 25.4
Please help on this issue. Thanks
@vasanth18
The blank output is caused by the duplicate <person> element name that appears in three different parts of the XML (testData.persons.person, additonalData.data.person and subData.person).
In the Reporting Engine the dot‑notation (data.testData.persons.person) builds a single collection based on the element name only. When the same name is also present elsewhere the engine treats the collection as heterogeneous (some items have child nodes, some are simple values) and, by design, skips rendering the fields that are not present on every item – resulting in an empty report.
Typical ways to resolve
-
Make the element names unique for the collection you want to iterate, e.g.:
<persons>
<personItem>
<name>John Doe</name>
<age>30</age>
<birth>1989-04-01 4:00:00 pm</birth>
</personItem>
…
</persons>
Then use <<var [ppls = data.testData.persons.personItem]>>.
-
Scope the selection with an explicit filter (available from v23.9 onward) so that only the intended nodes are taken:
<<var [ppls = data.testData.persons.person[../@type='main']]>> // example using an attribute
If you cannot add an attribute, you can filter by the presence of child elements:
<<var [ppls = data.testData.persons.person[./name]]>>
-
Upgrade to the latest Aspose.Words for Java (the current release at the time of writing is 24.12). The newer versions contain a fix that makes the engine ignore unrelated nodes with the same name when a filter is applied.
Next steps for verification
- Provide the exact Aspose.Words for Java version you are using.
- Send a minimal reproducible sample (the XML file and the .docx template) so we can run it against the latest library.
- If the issue persists after applying one of the suggestions above, we will log it as a defect and forward it to the product team.
Once we have the version and a small repro, we can confirm whether this is a bug in the current build or simply a naming‑collision limitation.
@vasanth18
This behavior comes from how XmlDataSource is implemented. In .NET, XmlDataSource uses System.Data.DataSet.ReadXml functionality to load XML and then uses the DataSet to access data internally. In Java, there is no such a class, so Aspose.Words for Java implements DataSet on its own by mimicking all the limitations and behavior of its .NET prototype. In .NET, loading of such XML is not supported for DataSet, hence it is not supported for Java as well.
You may want to check official documentation on System.Data.DataSet.ReadXml in .NET and consider mentioned limitations when loading data into XmlDataSource, those are the same in Java. As a workaround for this scenario, you can rename conflicting XML elements.
Thanks for the update @ivan.lyagin .
The xml data is coming from our legacy upstream system and its been used by many downstream systems, so its not possible to renaming xml elements.
Is there any other workaround for this? Because, when debugging the DataSource object I can see the DataTable of people is available, but unable to access it via the data.testData.persons.person syntax which I can understand its a limitation from .NET ReadXml function. Can we access that DataTable by any other way.
Thanks & appreciate your help on this.
@vasanth18
In .NET, reading such XML into DataSet throws an exception. Although it does not throw in Java, the things may change over time to better match behavior in .NET. So, I would not recommend trying to access this data table.
I think, you can perform the renaming not for all your systems, but just before feeding XML to the engine as a pre-processing step in your code.