Update August 26, 2015: I have enhanced this document generation system, and published it as part of Open-Xml-PowerTools, which you can find at https://github.com/OfficeDev/Open-Xml-PowerTools. Going forward, I will be enhancing and maintaining that document generation system. Please feel free to clone / fork that repo, report issues on GitHub, and interact with me there.
I recently received a very good request for an enhancement to this document generation system. The request was for a “Repeat” control that works in a similar way to tables, but instead of putting child records into a table, the document generation system generates a repeating section of content.
To review, here is what the template document looked like in the last iteration of this document generation system. Below, you can see a screen-shot of the template document. Following that screen-shot, there is a listing of the XML file that contains the data that will be used in the document generation process.
- The green oval in the template document contains the XPath expression that selects the XML elements that contain the data for each of the documents. That XPath expression selects the Customer elements in the XML document (also circled with a green oval).
- Then, having selected the records for documents, the XPath expression in the blue oval selects the child records for the rows in the table. The context nodes for that XPath expression are the Customer elements selected by the XPath expression in the green oval. The selected elements in the XML document are encircled by a blue rounded rectangle.
- And then finally, the XPath expressions circled by red select the values to place in the cells in the table. In the XML document, the first set of nodes selected by those XPath expressions are also circled with a red oval.
The Repeat construct is parallel to that of a table. The following template document is similar in structure to the above template document, except that instead of generating a table, it generates repeating content.
When generated with the above XML document, the first document in the generated document looks as follows. I have encircled the repeating content with green rounded rectangles:
Of course, due to the recursive implementation, you can get really elaborate with this setup. You can, for instance have repeating content within repeating content, or conditional content that contains a table within repeating content, and so on.
Download: 12-02-21-Gen-Docs-XPath