Eric White's Blog RSS icon RSS icon

Open XML, SharePoint, and Office

Release of V2 of Doc Gen System: XPath in Content Controls

Today I’m posting the release of version 2 of my simple document generation system.  In this example, you configure the document generation process by creating a template document that contains content controls.  You then enter XPath expressions in those content controls.  Those XPath expressions specify the data that the document generator pulls from the source data.  The source data is an XML document that contains data for each and every document that you generate.  The source XML document can also contain detail (children records) that populate tables in the generated document.  I detailed how the template document works in the post Generating Open XML WordprocessingML Documents using XPath Expressions in Content Controls.

This post is the 14th in a series of blog posts on generating Open XML documents. Here is the complete list: Generating Open XML WordprocessingML Documents Blog Post Series

Download: Generate Open XML WordprocessingML Documents using XPath Expressions in Content Controls

In my opinion, the use of XPath expressions in content controls is a superior approach to the one of entering C# code in content controls.  The code is cleaner and smaller (this first example is less than 240 lines of code).

I’ve recorded a short (2 minute) screen-cast that demonstrates this example in action.

Demonstrates the XPath-in-Content-Controls approach to document generation

So please download the example, try it out, and give me feedback.

22 Responses to “Release of V2 of Doc Gen System: XPath in Content Controls”

  • Robert Nattenberg says:

    Hi Eric,

    If I’m not mistaken, you’re the Eric White I worked with about 20+ years ago when I was at Unisys when I was working on XVT.

    If I am mistaken, pardon me. If I’m not, I hope all is well with you.

    Best,
    Bob

  • [...] White has put out a document generation example which uses XPath and Word Content Controls.  I applaud Eric for the amount of work he has done [...]

  • Mike Brennan says:

    Sometimes forms do not fall into computationally convienent patterns (Low Volume High Complexity (LVHC) #8).
    Let me suggest a significant and potientially valuable enhancement to your example from the LVHC viewpoint.
    - Consider adding a column to the table that is a single character such as a taxable flag, promotional flag, discounted flag, etc.
    - Setup the table such that the output will always be narrow, only one or two characters wide.
    - Determine how to support the column with an expression that is much wider than the column output format will permit.

    When solved with a mapping approach, such as using something in place of the xpath expression that maps to an xpath expression, a great deal of generality is added to the solution. This kind of approach can also lead to separation of the ‘user view’ of the template fields from the ‘technical view’ of the template fields.

    I like the elegance of your solution and hope you can find a way to include this enhancement.
    - Mike

    • Eric White says:

      Great idea! This could be done with something like *1 or ~1 that maps to XPath expressions in the Config content control. I’ll have to do a bit of research to make sure that the starting character is not allowed in XPath expressions.

      -Eric

  • Srinath says:

    I didn’t find any licensing information in the downloads. I find the code highly instructional and in case, I wanted to use this as a starting point for my project, just wanted to be sure it’s okay.

  • Jayadev Thimmaraju says:

    hi Eric,

    I have been asked to develop a custom proposal management solution(output is word,pdf,excel) using Microsoft technology platform. I came across your site. Could you please advise, if I can go ahead with the Open XML approach and use your code, will Dell have any licensing issues if I develop a prototype and I need to move this code to production

    Also, do we have any thirdparty tools which generates word documents on servers

  • Tom says:

    Hi Eric,
    I’ve been looking to implement something similar to this for some time.
    Unfortunately I’m not experienced in C# and I’m having a hard time converting it to VB. Are you able to make a release in VB as well?

    Thanks,

    Tom

    • Eric White says:

      Hi Tom, that code can be converted to VB – it will require some fairly deep knowledge of LINQ to do so. Have you gone through my functional programming tutorial for VB? http://blogs.msdn.com/ericwhite/pages/fp-tutorial-vb.aspx

      Converting that code to VB is a great idea, but at the moment, I am buried in other projects.

      One thing you may consider is to just use it in C#. It is easy enough to mix C# and VB in the same project.

      -Eric

      • Tom says:

        Hi Eric,
        I appreciate the reply.
        I’m a newbie to LINQ which is where I was having problems with the conversion. Everything else was converting across fine except for the LINQ queries.
        I went through a couple of different converters and managed to find one which did it perfectly. I’ll use the converted code to teach myself LINQ as I build on the solution.

        Thanks for the starting point!

  • Kevin says:

    I have a project that I am using this technique for substituting values from my business objects into a document (I will call these merged documents). My application uses DocumentBuilder 2.0 to assemble my final document based on decisions in the application using several Word2010 documents. My problem is that DocumentGenerator writes a document to the physical disk. Is it possible to create resulting documents from DocumentGenerator as WmlDocument without having to write a document to the physical disk?

    • Eric White says:

      Hi Kevin,

      I haven’t put together a version that generates the documents in-memory. It would not be hard to do, but I hadn’t considered a case where this would be useful. DocumentGenerator could return a list of WmlDocument objects. I’ll put this on my list of things to do…

      -Eric

  • chandramouli says:

    Dear Mr. Jayadev:
    I have been trying to get your contact numbers for a quite some time. We spent some time at your house also in chennai.
    Kindly mail to the following email id
    chandravmouli@rediffmail.com
    moulivsastry@yahoo.co.in

  • Kristen says:

    Any chance you could send me the zip file (http://cid-5e385848af211ba9.office.live.com/self.aspx/11-03-08-Doc-Gen/11-03-24-Gen-Docs-XPath.zip)? For some reason, I am not able to download it.

    Also, I’m wondering if you can tell me if the Express version of Visual Studio will suffice?

    Thanks so much,
    Kristen

  • Kevin says:

    My application uses DocumentBuilder 2.0 and OpenXMLPowerTools to assemble my final document based on decisions in the application using several Word2010 documents.

    When doc1.docx has a numbered list (with letters A. through G. or numbers 1. through 8.) and doc2.docx is a continuation of the same list (so the first numbered paragraph should be H or 9).

    When I add the second document, I use the Source(WmlDocument source, bool keepSections) constructor and keepSections is always false.

    Is it possible to do a continuous list? Thanks for your input.

  • Stuart Rivenbark says:

    I am having trouble downloading the sample. The link http://cid-5e385848af211ba9.office.live.com/self.aspx/11-03-08-Doc-Gen/11-03-24-Gen-Docs-XPath.zip does not work.

    Could you please send the ZIp to the following two addresses. My work network often blocks ZIP file attachments.

    Please send to stuart.rivenbark.ctr@navy.mil and newbernrivenbarks@suddenlink.net.

    Thansk so much.


Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>