Repeating Content in Document Generation System that uses XPath Expressions in Content Controls

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.

Template1

XML1

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.

Template2

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:

GenDocs1

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

!!!

Comments »

  1. Tom W said,

    February 28, 2012 @ 9:52 pm

    Nice post Eric.
    You never got around to adding repeating in horizontally across cells did you? To refresh your memory, like if I wanted a column per Year for financial data.

    Tom

  2. Joe Cardali said,

    May 31, 2012 @ 7:26 pm

    Upon closer reading, this appears to be similar to the sort of functionality that I am looking for, as well.

  3. chirag said,

    March 23, 2012 @ 11:28 am

    Hi Eric,
    Your written blog is very useful, I want to generate Document using this method.

    I am already generating very complex document using COM+. But it’s too slow and very tedious to handle.

    I found your article it’s very useful, I want to Generate Document with this method. now I have query is that I want to generate table for amount column and at last I want to do some of the column, Now I tried lots but I can’t found it.

    My table like this.

    ——————————————————————————————
    Description Amount Field1 Amount Field2 Amount Field3

    ——————————————————————————————-
    Total Sum of Field1 Sum of Field2 sum of Field3
    ——————————————————————————————-

    Please Help.

    Chirag

  4. John Holliday said,

    April 9, 2012 @ 12:55 pm

    After reviewing your excellent code samples, I noticed that in your templates, you use both the “Title” and “Tag” fields of the content controls, but in your code, you rely only on the “Title” (alias) field to locate your placeholders, even though your routine is called “GetContentControlByTag”.

    Is there a particular reason you decided to switch from using the Tag field to using the Title field?

  5. Joe Cardali said,

    May 31, 2012 @ 2:19 pm

    Hi Eric – I am looking to use this system to generate some documents that use tables the purposes of structure (no tabular data). I am having some trouble accomplishing this, my one bright idea was wrapping individual cells in “SelectValue” tags, but this yielded no results. Can you think of any way to accomplish this?

  6. Document Management – A better means to actively engage global business!Document Generation Software said,

    March 8, 2013 @ 11:05 pm

    […] Document management using web wizards is an innovative approach to high-speed documentation. Exploring the potential of XML helps to manage document authoring in the conventional querying manner. However, web wizards simplify this process for ordinary users. The most commonly observed phenomena why employees resist automation is the inertia to learn new methods with hours of intensive training. Wizards eliminate entire process and ensure these users master the tool with minimal learning. This flexible platform offers complete control of the document dynamics of its built-in features to define designs, layouts, and other formatting options. […]

  7. Tor said,

    August 8, 2013 @ 12:10 pm

    Eric,

    Thanks for all your hard work. I have been following your blog posts for a while, and I am very impressed with how much material you are able to produce. I don’t see how you find time for it all. In any case, keep up the good work.

    My question is:

    Any chance that you will translate this app: “Repeating Content in Document Generation System that uses XPath Expressions in Content Controls” using your new javascript libraries?

  8. Eric White said,

    August 8, 2013 @ 1:23 pm

    Hi Tor,

    I’m glad you like the blog posts / etc.

    Funny you should ask about this – very definitely this is high on my list. As *one* example of a use case, supply an XML data source, along with a template document to a JavaScript application. It returns a zip file that contains all generated documents. This can even be a JavaScript application that I host on OpenXmlDeveloper.org or on this blog, there for anybody to use as a ‘service’.

    Another use case – use Node.JS to generate the documents.

    A third use case – a JavaScript application that is a Word app – while editing the template document, supply an XML file, and immediately save a ZIP file that contains the generated documents.

    There are more use cases.

    Anyway, I have some other projects to get out of the way first, but this is definitely on the radar.

    Cheers, Eric

  9. Victor said,

    September 12, 2013 @ 5:41 am

    Hi, Eric. The example sources is not avaibale.

  10. Mauro Garcia said,

    March 27, 2014 @ 5:56 pm

    Hey Eric, I want the example too :/

    (Nice blog btw)

  11. Rich P said,

    April 8, 2014 @ 12:20 pm

    Eric – great blog. I was really looking forward to playing around with this example but can’t find the download. Will it ever be available again?

    Thanks,
    Rich P

  12. Eric White said,

    April 9, 2014 @ 3:53 pm

    Hi Rich, Here is the link:

    https://skydrive.live.com/redir?resid=5E385848AF211BA9!5731&authkey=!AOs_0PT_ICgWjcg

    Cheers, Eric

  13. Dave said,

    June 19, 2014 @ 4:19 pm

    Eric,
    I love your coverage of what is possible with the OpenXML technologies and am learning a lot from your articles and code listings. I cannot seem to access the link to the download for the article (refer to the URI) and was wondering if you could please make the download available once again?
    thanks,
    Dave

  14. Dave said,

    June 19, 2014 @ 4:25 pm

    Eric,
    Sorry for the oversight regarding the download link…I was able to get to the download from your response to a previous comment post.
    thanks,
    Dave

  15. mactavish2191 said,

    July 29, 2014 @ 10:28 pm

    Eric Please help me out in processing word document.
    I have to extract the contents of word files with formatting and everything
    within special tags.Can u help me.

  16. Dave R said,

    October 10, 2014 @ 4:57 am

    Never mind – I found the updated download link to the Repeating Content source in your response to a previous comment.

  17. Akos said,

    October 21, 2014 @ 1:04 pm

    Hi!

    The source is not available.

  18. Bradley Rogers said,

    July 9, 2015 @ 6:55 pm

    Hi Eric
    the link does not work. i am trying to duplicate your GenDocsDataBoundContentControls project that i see in a youtube but cannot find the source. i typed in the source looking at youtube but it does not work for me.

    i defined an xml file with what are my contentcontrol names in a word doc. when it gets to foreach xelement abc in () and in there its got every element showing up just fine, it seems to not see any XElements in the document and goes by and does nothing and exits.

    i found another where it seems to create a copy of one doc to another but i cannot tell how its using the xml, and there is no external xml file that can be found

  19. Bradley Rogers said,

    July 10, 2015 @ 12:27 pm

    I ran into problems when trying to add more XML nodes and associated fields within the template.docx file

    on 2 completely different systems, when adding fields and deleting the content controls in the template that are not needed, the debugger throws an exception for object ref not set to an instance of an object in the (iftag=SelectValue) method.

    what is the requirement to set xml nodes and set content controls in the doc?
    thanks

  20. Grace Kelly said,

    January 11, 2016 @ 10:04 am

    Hi Eric,

    Can you please fix the link?

    It says “This item might have been deleted, expired, or you might not have permission to view it. Contact the owner of this item for more information.”

    I want to see how you do it.
    Or maybe there is another link?

    Thanks.

  21. Eric White said,

    January 30, 2016 @ 12:43 am

    @Grace Kelly,

    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.

    Please see the following screen-casts:

    http://www.ericwhite.com/blog/high-performance-docx-generation-using-documentassembler/

    http://www.ericwhite.com/blog/getting-started-with-open-xml-powertools-documentassembler/

    Cheers, Eric

  22. Adam Swierszczkow said,

    June 24, 2016 @ 10:09 pm

    Please fix the link with example.

RSS feed for comments on this post · TrackBack URI

Leave a Comment