Enable Custom Merging of InfoPath Forms
Applies to: InfoPath 2010 | InfoPath Forms Services | Office 2010 | SharePoint Server 2010 | Visual Studio | Visual Studio Tools for Microsoft Office
In this article
Creating a Custom Transform
select
insert
replace
mergeAttributes
delete
get-documentElement
Steps for Creating a Custom Merge
Creating a Custom Merge in Code
The Merge Forms feature of the Microsoft InfoPath 2010 editor is designed to combine the data from multiple forms into a single form. This is also known as data aggregation. If merging forms is enabled, you can click the File tab, click Save & Send, click Merge Forms under Import & Link, and then click the Merge Forms button to select one or more forms to merge with the currently opened form. The form that is currently open is the target form and the forms selected in the Merge Forms dialog box are known as the source forms.
The aggregation of data that results from merging forms can include all of the data that is contained in the source and target forms, or only a portion of the original data. The default operations are the following.
For repeating elements, data is inserted at a matching location in the target document. This happens when the MaxOccurs attribute of the destination element is greater than 1.
For non-repeating elements (that is, for elements where MaxOccurs is "1"), the attributes of the source elements are added to the existing attributes of the destination element.
Creating a Custom Transform
The default operation when merging forms works well for forms that are based on the same XML schema. In some circumstances, however, you may want to merge forms that are based on different schemas or override the default merge operation for forms that are based on the same schema. For these scenarios, you can create an XSL Transform (XSLT), which contains aggregation instructions for the merge operation. The transform is applied at merge time to create a DOM document that contains the information to be imported, together with annotations specifying how to incorporate this information into the target document. These annotations are XML attributes in the namespace https://schemas.microsoft.com/office/InfoPath/2003/aggregation.
The XML attributes and their values serve as aggregation instructions on how each node is merged with the destination XML document. These attributes are described in the following sections.
select
The agg:select attribute contains an absolute XPath expression which identifies the destination element.
insert
A value of "insert" for the agg:action attribute instructs InfoPath to insert the source element as a child of the destination element that is specified by using the agg:select attribute. The children of the source element are included in the insert operation. The selectChild attribute lets you select a certain location for the insert node operation. The order attribute is used to specify whether the source elements are inserted before existing destination elements or after. The default value if the order attribute is not present is "after".
<my:field1 agg:select="/my:myFields/my:field1"
agg:action="insert" agg:order="before">Some Value</my:field1>
replace
A value of "replace" for the agg:action attribute instructs InfoPath to replace each of the destination elements referred to by the select attribute with the source element.
<my:field1 agg:select="/my:myFields/my:field1"
agg:action="replace">Some Value</my:field1>
mergeAttributes
If the value of the agg:action attribute is "mergeAttributes", the attributes of the source element are merged with the attributes of each of the destination elements referred to by the select attribute.
<my:PMAwS agg:action="mergeAttributes"
agg:select="/my:Root/my:PMAwS">
delete
If the value of the agg:action attribute is "delete", each of the destination elements referred to by the select attribute are deleted from the destination document.
<my:field1 agg:select="/my:myFields/my:field1"
agg:action="delete"/>
In conjunction with the attributes specified in the https://schemas.microsoft.com/office/InfoPath/2003/aggregation namespace, you use the https://schemas.microsoft.com/office/infopath/2003/aggregation-target namespace to denote an XSL object that implements the interface IXMLDOMDocument. One of the most useful members of this interface is the method get-documentElement.
get-documentElement
The target:get-documentElement function provides access to the Document Object Model of the destination document. It can be used to change the way the merge operation works based on the current contents of the destination document.
<xsl:when test="function-available('target:get-documentElement')">
<xsl:variable name="target" select="target:get-documentElement()" />
<xsl:if test="not(boolean($target/my:Customer[Name="MyName"]))">
<my:Customer agg:action="insert" agg:select="/my:MergeForms" />
</xsl:if>
</xsl:when>
Steps for Creating a Custom Merge
Select the kinds of XML source documents from which you want to merge data. Collect a representative sample of each kind of source document.
Derive the XML schema for each kind of XML source document for an existing InfoPath form. This step is easily accomplished by exporting the XML schema with the Export Source Files command on the Publish tab of the Backstage. For XML documents that were not created in InfoPath, you can use a tool such as Microsoft Visual Studio to create a schema from the sample XML document, or you can create a sample form from the XML document in InfoPath, and then export the schema that InfoPath derives from the document structure.
Identify the data that you want to merge from each kind of XML source document. This step will depend almost entirely on the requirements of both your source and destination forms. For some forms, you may want to copy all of the data from the source form. For others, you may want only one or two elements from the form's underlying XML document. When identifying what data that you want to merge, pay extra attention to both the source and destination documents to ensure that the elements map logically between the two forms.
Create an XSL transform for each kind of XML source document. When configuring the merging of your forms, this step will take the most time. The purpose of the XSL transform is to transform the source XML document into a format that can be merged with the destination form. When designing your transform, decide whether you want to use merging from XML location paths, or merging from InfoPath aggregation instructions.
Visual Studio is a convenient tool for creating XSL transforms. Visual Studio provides syntax coloring and a document outline. It also automatically provides closing tags for your XSL elements, which can help decrease redundant typing and hard-to-find spelling errors. You can also create XSL transforms with a text editor such as Notepad.
Start with the identity transform, which is simply an XSL transform that outputs the same XML file that is input:
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:agg="https://schemas.microsoft.com/office/infopath/2003/aggregation" xmlns:target="https://schemas.microsoft.com/office/infopath/2003/aggregation-target" xmlns:my="https://schemas.microsoft.com/office/infopath/2003/myXSD/2003-05-29T20:30:47"> <xsl:template match="/"> <xsl:copy> <xsl:apply-templates select="@* | node()" /> </xsl:copy> </xsl:template> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()" /> </xsl:copy> </xsl:template> </xsl:stylesheet>
Then add overriding template sections for the elements you want to add special handling for. For example, this template changes a <src:Task> element into a <my:field1> element and sets the value of the agg:action attribute to "replace".
<xsl:template match="src:Task"> <my:field1 agg:select="/my:myFields/my:field1" agg:action="replace"> <xsl:value-of select="."/> </my:field1> </xsl:template>
Add the XSL transform files and schema files in the form template. After you have derived schemas for each kind of source document and created an XSL transform to convert each document type so that InfoPath can merge its data, add them to as resources to your form. Click Resource Files on the Data tab, and then click Add in the Resource Files dialog box, browse to your schema or XSL transform file, and then click OK. Do this to for each schema file and XSL transform file that you created.
If the schemas that you add use the targetNamespace attribute, you must add a property element to the form's .xsf file so that InfoPath knows the namespace of the schema. To access this file, click the File tab, click Publish, and then click Export Source Files. Select a location for the files, and then click OK. Then close the InfoPath form template that you are designing.
Browse to the location that you specified for the extracted files and find the file that has an .xsf file name extension. Usually, this file is called manifest.xsf. Open the file in Notepad, find the <xsf:file> tag that corresponds to your schema, and add a "namespace" property element to specify the targetNamespace as shown in the following example.
<xsf:file name="IndvTasks.xsd"> <xsf:fileProperties> <xsf:property name="fileType" type="string" value="Resource" /> <xsf:property name="namespace" type="string" value="urn:office-microsoft-com:InfoPath-designer-aggregation-IndStatusReport" /> </xsf:fileProperties> </xsf:file>
The last step in preparing your form to support custom merging is to update the importParameters element in the .xsf file associated with the form. First, find the <xsf:importParameters> tag in the .xsf file. For each schema/XSL transform pair you have created for your form, add an xsf:importSource element to the xsf:importParameters element: <xsf:importParameters enabled="yes"> <xsf:importSource name="" schema="IndvTasks.xsd" transform="ImportTasks.xsl"></xsf:importSource> </xsf:importParameters>The name attribute of the xsf:importSource element contains the form template's name that may be found in a source XML document. Usually, you can leave this empty. The schema attribute contains the name of a schema file that you added to the form template in the previous step. Finally, the transform attribute contains the name of the XSL transform that you want to use to convert forms that conform to the schema. You can use a custom transform either with the Merge event or on its own.
Creating a Custom Merge in Code
Custom merging with code is supported by using the Merge event handler, with its corresponding useScriptHandler attribute of the importParameters element in the .xsf file associated with the form. In managed code, you can enable the Merge event by checking the box Merge using custom code , and then clicking the Edit button, in the Advanced category of the Form Options dialog box available from the Backstage.