If you have multiple
if /
else groups on the same level then you can wrap each group into its own content control and set the tag to '
group' without specifying the title to group these conditions.
Outputting simple values
This is what you would use to output actual values from your data source, such as values in each column of a table or names of your sections. In order to output simple values you should also add a content control in your document, but this time around you would click non-bold Aa button in the Developer tab to insert a Plain Text Content Control as it will not require any nested controls.
After you open the content control properties, you should set the Tag to '
val' and Title to a string that evaluates to your value, which would be XPath if your data source is XML-based.
Technically, you can specify anything or nothing inside of your content control, since it will be replaced with the evaluated value, but in practicality you would want to put some sample data inside of that control to help the designer see what it would look like in the actual document.
Template example
To demonstrate this lets create a template for a simple price list with tables of product names and prices grouped into sections by product category. We will create a section with the title and a table with one row under the header, which will be wrapped into a
loop content control and each cell will have a
val content control for the product name and price respectively. The title will be also a
val content control for the product category and we will wrap the entire section into another
loop content control. Here is what it will look like in the Design Mode.
Below is a sample XML that conforms to the expected input structure.
<products>
<pc Category="Mountain Bikes">
<p Name="Mountain-100 Silver, 38" ListPrice="$3,399.99" />
<p Name="Mountain-200 Black, 38" ListPrice="$2,294.99" />
<p Name="Mountain-300 Black, 48" ListPrice="$1,079.99" />
<p Name="Mountain-500 Black, 52" ListPrice="$539.99" />
</pc>
<pc Category="Road Bikes">
<p Name="Road-150 Red, 62" ListPrice="$3,578.27" />
<p Name="Road-650 Red, 52" ListPrice="$782.99" />
<p Name="Road-250 Red, 58" ListPrice="$2,443.35" />
<p Name="Road-750 Black, 52" ListPrice="$539.99" />
</pc>
</products>
Generating documents with Xomega
In order to support transformation of such MS Word templates in Open Office format you can write a generic XSLT stylesheet that reads XML from the parts of the origin word template, applies it to your supplied XML data source, and packages the resulting XML into the corresponding part of the target document.
Xomega.Net for Visual Studio automatically comes with such a generic XSLT stylesheet, which it uses to create design documents for your domain and service models. You can, however, use it to generate documents from any custom XML. All you have to do is create your stylesheet that includes
doc_common.xsl from Xomega and calls the generate-document with your XML as the parameter. You'll need to make sure that the values for
DocumentTemplate and
OutputPath parameters are specified either at run time or design time.
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- import Xomega base generator -->
<xsl:import href="doc_common.xsl"/>
<xsl:param name="DocumentTemplate">PriceListTemplate.docx</xsl:param>
<xsl:param name="OutputPath">BikePriceList.docx</xsl:param>
<xsl:template match="/">
<!-- call Xomega template to convert XML data to the output document -->
<xsl:call-template name="generate-document">
<xsl:with-param name="xmlData" select="."/>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>
If you run such a stylesheet with our sample price list data from above then you will get a document generated like this.
Conclusion
We have learned how to create document templates using Content Controls in MS Word and saw how you can generate documents from those templates and custom XML data source using generic Xomega stylesheet.
We would like to hear your thoughts, so please post your comments or contact us with any questions or feedback.