You want to perform a mail merge in Word using an XML file as the data source instead of a standard Excel sheet or Access database. Word’s built-in mail merge wizard expects data in a specific format, but an XML file with a custom schema does not automatically map to merge fields. This article explains how to create and attach a custom XML schema (XSD) to a Word document so that you can map XML elements to mail merge fields and generate personalized documents from your XML data. By the end, you will be able to configure a custom schema, load an XML data source, and run a mail merge without losing data formatting or field alignment.
Key Takeaways: Using a Custom XML Schema for Mail Merge
- Developer > Schema > Add Schema: Attach your custom XSD file to the Word document so that XML elements become available for mapping.
- XML Mapping Pane (Developer > XML Mapping): Drag XML elements from the pane onto the document to create content controls that hold merge data.
- Mail Merge > Select Recipients > Use an Existing List: Point to your XML file after the schema is attached so Word reads the data correctly.
What Is a Custom XML Schema and Why Is It Needed for Mail Merge
A custom XML schema is a file with an .xsd extension that defines the structure of an XML data file. It specifies which elements and attributes are allowed, their data types, and their hierarchy. When you attach a schema to a Word document, Word understands the layout of your XML data and can map each element to a content control in the document.
Standard mail merge in Word works with flat data sources like Excel tables or Access queries. XML files can contain nested structures, repeating groups, and complex hierarchies that do not map directly to Word’s merge field system. By using a custom schema, you bridge this gap. You tell Word exactly where each piece of data lives inside the XML file so that the mail merge engine can pull the right values.
You need two files: an XML data file (your data) and an XML schema file (the definition). Both must be present before you start. If your XML file does not have a reference to the schema, you can still attach the schema manually in Word. The schema does not have to be embedded in the XML file; Word uses it separately to interpret the data structure.
Prerequisites
Before you begin, confirm the following:
- You have a valid XML data file with well-formed tags and a root element.
- You have an XSD schema file that describes the XML structure. You can create one using a text editor or a tool like Visual Studio or XML Notepad.
- You are using a desktop version of Word (Microsoft 365, Word 2021, Word 2019, or Word 2016). Word Online does not support custom XML mapping.
- The Developer tab is visible in the Word ribbon. If it is not, go to File > Options > Customize Ribbon and check the Developer box.
Steps to Attach a Custom XML Schema and Map Data for Mail Merge
Follow these steps in order. Do not skip the schema attachment step, because Word will not recognize the XML structure without it.
- Open a new blank Word document
Start with a blank document. You will build the merge template here. Save the document before attaching the schema to avoid losing changes. - Attach the custom XSD schema
Go to the Developer tab. In the XML group, click Schema. In the Templates and Add-ins dialog, click Add Schema. Browse to your .xsd file and select it. Click Open. In the Schema Settings dialog, ensure the schema is checked and click OK. The schema is now attached to the document. - Open the XML Mapping pane
On the Developer tab, in the XML group, click XML Mapping. The XML Mapping pane appears on the right side of the window. It shows the tree structure of your schema, with elements you can insert into the document. - Insert content controls for each data field
In the XML Mapping pane, right-click an element and choose Insert Content Control > Plain Text. A content control box appears in the document. Repeat for every element you want to include, such as FirstName, LastName, Address, or OrderNumber. Position each content control where you want the merge data to appear. - Add repeating section content controls for repeating data
If your XML file contains repeating groups, such as multiple orders per customer, insert a Repeating Section Content Control. In the XML Mapping pane, right-click the repeating element (for example, Orders) and choose Insert Content Control > Repeating Section. Then drag the child elements inside that section. Word will repeat the section for each item in the XML group. - Connect the XML data file for mail merge
Go to the Mailings tab. Click Select Recipients > Use an Existing List. In the Select Data Source dialog, change the file type filter to XML Files. Select your XML data file and click Open. Word reads the data using the attached schema. - Insert merge fields and complete the merge
On the Mailings tab, click Insert Merge Field. The fields from your schema appear in the list. Click each field to insert it into the document. Then click Preview Results to see the data. If everything looks correct, click Finish & Merge and choose Print Documents or Edit Individual Documents.
If Word Still Has Issues After the Main Fix
Word does not show any XML elements in the XML Mapping pane
This usually means the schema was not attached correctly or the schema file is invalid. Open the Developer tab, click Schema, and verify that your schema is listed and checked. If it is missing, add it again. If the schema is present but the pane is empty, open the .xsd file in a text editor and confirm it has at least one element declaration. A minimal schema must contain an xs:element tag inside an xs:schema root.
Mail merge says the data source has no records
Word may read the XML file but find zero records if the XML structure does not match the schema exactly. Check that the root element name in the XML file matches the root element defined in the schema. Also ensure that the namespace declared in the XML file (if any) matches the namespace in the schema. Open the XML file in a browser or text editor and compare the tag names and hierarchy to the schema definition.
Content control does not show data after preview
The content control might be mapped to the wrong element. In the XML Mapping pane, click the content control in the document. The pane highlights the mapped element. If the highlight does not match the expected field, right-click the correct element and choose Map to Selected Content Control. Then preview again.
Word Mail Merge With XML vs Standard Data Sources: Key Differences
| Item | XML Data Source With Custom Schema | Standard Data Source (Excel, Access) |
|---|---|---|
| Setup complexity | Requires manual schema attachment and content control mapping | No schema needed; fields map automatically |
| Data structure support | Supports nested elements and repeating groups | Flat table only; no nesting |
| Field mapping method | XML Mapping pane with drag-and-drop content controls | Insert Merge Field from a predefined list |
| Schema requirement | Must attach an .xsd file before loading data | No schema required |
| Repeating data handling | Repeating Section Content Control automatically duplicates rows | Requires separate merge per record or advanced field codes |
| Compatibility | Desktop Word only (Windows and Mac) | Word Online and desktop versions |
You can now run a mail merge in Word using any XML data source by attaching a custom schema and mapping content controls. Start by creating a schema file that matches your XML structure, then follow the steps to attach it and insert content controls. For complex data with repeating groups, use Repeating Section Content Controls to avoid manual duplication. As an advanced tip, save your schema-attached document as a template (.dotx) so you can reuse the same mapping for future mail merges without rebuilding the schema attachment each time.