That you for your help so far. But I think I still don’t understand this completely.
I added to my small example program.
import com.aspose.words.*;
import com.aspose.words.net.System.Data.*;
import java.io.FileInputStream;
public class test {
private static void print_values(DataSet dataSet, String label) {
System.out.println("\n" + label);
for (DataTable table : dataSet.getTables()) {
System.out.print(table.getTableName() + ":\nChildTables: ");
for (DataRelation dr : table.getChildRelations()) {
System.out.print(dr.getChildTable().getTableName() + " ");
}
System.out.print("\nColumns: ");
for (DataColumn column : table.getColumns()) {
System.out.print(column.getColumnName() + " ");
}
int row_num = 0;
for (DataRow row : table.getRows()) {
System.out.print("\nRow " + row_num + ": ");
for (DataColumn column : table.getColumns()) {
System.out.print(row.get(column) + " ");
}
}
System.out.println("\n");
}
}
static void create_doc(String data_path, String path_schema, String template_path, String output_path) {
try {
DataSet data = new DataSet();
data.readXmlSchema(path_schema);
data.readXml(data_path, XmlReadMode.READ_SCHEMA);
print_values(data, data_path);
Document doc = new Document(new FileInputStream(template_path));
MailMerge mailMerge = doc.getMailMerge();
mailMerge.executeWithRegions(data);
doc.save(output_path);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String dataDir = "C:\\Temp\\";
create_doc(dataDir + "test_data2.xml", dataDir + "schema.xsd", dataDir + "temp2.docx", dataDir + "result4.pdf");
}
}
and created a small schema for my xml.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="bar"/>
<xs:element ref="foo"/>
</xs:sequence>
<xs:attribute name="a" use="optional" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="foo">
<xs:complexType>
<xs:sequence>
<xs:element ref="bar"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="bar">
<xs:complexType>
<xs:sequence>
<xs:element ref="Name"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Name" type="xs:string"/>
</xs:schema>
this gives me this as output:
C:\Temp\test_data2.xml
root:
ChildTables: bar foo
Columns: a root_Id
foo:
ChildTables: bar
Columns: foo_Id root_Id
Row 0: 0
bar:
ChildTables:
Columns: Name root_Id foo_Id
Row 0: Text1
Row 0: Text2 0
as you can see the root table has two child tables bar and foo and bar has two rows ‘Text1’ and ‘Text2’
but I still get no replacements in my document.
«TableStart:root»
«TableStart:bar»
«Name»
«TableEnd:bar»
«TableEnd:root»
Why is ‘Text1’ not in root? (In fact it seems to only be in bar)
If I run this with an attribute in root ‘Text1’ is in root.