How to make a section optional when mapped to optional data in a Word OpenXml Part?
Asked Answered
S

1

6

I'm using OpenXml SDK to generate word 2013 files. I'm running on a server (part of a server solution), so automation is not an option.

Basically I have an xml file that is output from a backend system. Here's a very simplified example:

<my:Data 
    xmlns:my="https://schemas.mycorp.com">
    <my:Customer>
        <my:Details>
            <my:Name>Customer Template</my:Name>
        </my:Details>
        <my:Orders>
            <my:Count>2</my:Count>
            <my:OrderList>
                <my:Order>
                    <my:Id>1</my:Id>
                    <my:Date>19/04/2017 10:16:04</my:Date>
                </my:Order>
                <my:Order>
                    <my:Id>2</my:Id>
                    <my:Date>20/04/2017 10:16:04</my:Date>
                </my:Order>
            </my:OrderList>
        </my:Orders>
    </my:Customer>
</my:Data>

Then I use Word's Xml Mapping pane to map this data to content control:

enter image description here

I simply duplicate the word file, and write new Xml data when generating new files.

This is working as expected. When I update the xml part, it reflects the data from my backend.

Thought, there's a case that does not works. If a customer has no order, the template content is kept in the document. The xml data is :

<my:Data 
    xmlns:my="https://schemas.mycorp.com">
    <my:Customer>
        <my:Details>
            <my:Name>Some customer</my:Name>
        </my:Details>
        <my:Orders>
            <my:Count>0</my:Count>
            <my:OrderList>
            </my:OrderList>
        </my:Orders>
    </my:Customer>
</my:Data>

(see the empty order list).

In Word, the xml pane reflects the correct data (meaning no Order node):

mapping with empty data

But as you can see, the template content is still here.

Basically, I'd like to hide the order list when there's no order (or at least an empty table).

How can I do that?

PS: If it can help, I uploaded the word and xml files, and a small PowerShell script that injects the data : repro.zip

Swift answered 19/4, 2017 at 14:23 Comment(2)
Sure - upload the docs, please.Homopolar
@Taterhead: voilà, it has been uploadedSwift
H
1

Thanks for sharing your files so we can better help you.

I had a difficult time trying to solve your problem with your existing Word Content Controls, XML files and the PowerShell script that added the XML to the Word document. I found what seemed to be Microsoft's VSTO example solution to your problem, but I couldn't get this to work cleanly.

I was however able to write a simple C# console application that generates a Word file based on your XML data. The OpenXML code to generate the Word file was generated code from the Open XML Productivity Tool. I then added some logic to read your XML file and generate the second table rows dynamically depending on how many orders there are in the data. I have uploaded the code for you to use if you are interested in this solution. Note: The xml data file should be in c:\temp and the generated word files will be in c:\temp also.

Another added bonus to this solution is if you were to add all of the customer data into one XML file, the application will create separate word files in your temp directory like so:

customer_<name1>.docx

customer_<name2>.docx

customer_<name3>.docx

etc.

Here is the document generated from the first xml file enter image description here

Here is the document generated from the second xml file with the empty row enter image description here

Hope this helps.

Homopolar answered 28/4, 2017 at 22:56 Comment(1)
I really appreciate your effort to try to solve my issue. However, generating word ML is not actually an option. I didn't describe the whole solution to simplify the question, but in the application, users can edit the document template (adding branding, choosing columns, etc.). The XML data cames from a query system that can result in an arbitrary set of data. The word template file then must match the XML data. Everything is manually setup, instance per instance of a generation of document (and specific to each of our customer where we ship our product)Swift

© 2022 - 2024 — McMap. All rights reserved.