This article describes how to programmatically transform the data in your secondary data source. Then, the transformed data can be transferred to fields in your primary data source at runtime. You do this in Microsoft Office InfoPath.
InfoPath lets you bind fields to XML data sources by using the UI. This article also explains how to use the UI to bind fields to secondary data sources in Microsoft Office InfoPath.
Use a script with your form to transfer the data to your Form fields in InfoPath
In Microsoft Office InfoPath, you cannot directly bind fields in a form to XML data in a secondary data source. However, you can use a script to transform the data in your secondary data source. To bind fields to XML data sources by using a script, follow these steps:
1.
Return to InfoPath.
Bookstore.xsn must be open in Design mode.
2.
To add Xmlfile.xml as a secondary data source for your form, follow these steps.
In InfoPath 2003, follow these steps:
a.
On the Tools menu, click Secondary Data Sources. The Secondary Data Sources dialog box appears.
b.
Click Add.
The Data Source Setup Wizard starts.
c.
Click XML Data File for the data source type, and then click Next.
d.
Click Browse. In the Open dialog box, locate Xmlfile.xml, and then click Open. Click Next.
e.
Click Finish to complete the Data Source Setup Wizard. When you are prompted to add the file to your form, click No.
f.
Click Close to close the Secondary Data Sources dialog box.
In InfoPath 2007, follow these steps:
a.
On the Tools menu, click Convert Main Data Source.
The Data Source Wizard dialog box appears.
b.
Click Browse.
The Open dialog box appears.
c.
Move to Xmlfile.xml, and then click Open.
d.
Click Next, and then click Finish to complete the Data Source Setup Wizard.
3.
To add Xsltfile.xslt as a secondary data source for your form, follow these steps:
a.
On the Tools menu in InfoPath 2003, click Resource Manager.
In InfoPath 2007, click Resource Files on the Tools menu .
b.
Click Add, and then add Xsltfile.xslt.
c.
Click OK.
d.
For Infopath 2003, follow these steps:
1.
On the Tools menu, click Secondary Data Sources.
The Secondary Data Sources dialog box appears.
2.
Click Add.
The Data Source Setup Wizard starts.
3.
Click XML Data File for the data source type, and then click Next.
4.
Click Resource Manager. In the Resource Manager dialog box, click Xsltfile.xslt, and then click OK. Click Next.
5.
Click Finish to complete the Data Source Setup Wizard.
6.
Click Close to close the Secondary Data Sources dialog box.
For InfoPath 2007, follow these steps:
1.
On the Tools menu, click Convert Main Data Source.
The Data Source Wizard dialog box appears.
2.
Click Browse. The Open dialog box appears.
3.
Move to Xsltfile.xslt, and then click Open.
4.
Click Next, and then click Finish to complete the Data Source Setup Wizard.
4.
In InfoPath 2007, point to Script on the Tools menu, and then click On Load Event.
In InfoPath 2007, point to Programming on the Tools menu, and then click On Load Event.
The Microsoft Script Editor starts.
5.
Add the following code sample to the XDocument::OnLoad event handler.
//Update the data in the secondary data source, and then
//get a reference to the DOM of the secondary data source.
XDocument.DataObjects.Item("XMLFile").Query();
var XMLFileDOM = XDocument.GetDOM("XMLFile");
//Get the transform that is stored as a secondary data source.
var XSLTFileDOM = XDocument.GetDOM("XSLTFile");
//Transform Xmlfile by using the style sheet that is named Xsltfile.
var TransformedDOM = new ActiveXObject("MSXML2.DomDocument.5.0");
TransformedDOM.async = false;
TransformedDOM.validateOnParse = true;
TransformedDOM.loadXML( XMLFileDOM.transformNode(XSLTFileDOM));
//Get a reference to the node in the primary document where you want to insert
//the secondary data.
var oBookstore = XDocument.DOM.selectSingleNode( "/my:myBookstore" );
var oBooks_Dest = oBookstore.selectSingleNode("my:myBooks");
var oBooks_Src = TransformedDOM.documentElement;
//Replace the node in the primary document with the data from the
//secondary data source.
oBookstore.replaceChild( oBooks_Src, oBooks_Dest );
6.
Save your changes to the script, and then close Script Editor.
Use the UI to bind fields to secondary data sources in InfoPath
InfoPath lets you bind fields to an XML data source by using the UI. To do this, follow these steps:
1.
Return to the InfoPath form.
Bookstore.xsn must be open in Design mode.
2.
To add Xmlfile.xml as a secondary data source for your form, follow these steps:
a.
On the Tools menu, click Data Connections.
The Data Connections dialog box appears.
b.
Click Add.
The Data Connection Wizard appears.
c.
Select Receive data, and then click Next.
d.
Select XML document for the data source type, and then click Next.
e.
Click Browse. In the Open dialog box, locate Xmlfile.xml, and then click Open. Click Next.
f.
Click Finish to complete the Data Source Setup Wizard.
3.
To bind the XML data to the controls, follow these steps:
a.
On the form, double-click Repeating Table.
The Repeating Table Properties dialog box appears.
b.
On the Data tab, click Change Binding.
The Repeating Table Binding dialog box appears.
c.
Click XMLFile (Secondary) in the Data Source drop-down list.
d.
Click the book (Group) node in the Specify which group to bind the rows to section, and then click Next.
e.
Click the Title node in the Data binding section, and the click Add.
f.
Repeat step e to add ISBN and genre to the Columns in table section.
In InfoPath 2003, point to Preview Form on the File menu, and then click Default. In InfoPath 2007, point to Preview on the File menu, and then click Form.
When the form opens in Preview mode, the data in Xmlfile.xml is transformed by using the style sheet. Then, the contents of the form are replaced with the transformation results. The output in the InfoPath Preview window appears as follows:
For additional information about how to obtain the latest service pack for Microsoft Office, click the following article number to view the article in the Microsoft Knowledge Base:
870924 (http://support.microsoft.com/kb/870924/)
How to obtain the latest service pack for Office 2003
For additional information about scripting for InfoPath, click the following article numbers to view the articles in the Microsoft Knowledge Base:
827002 (http://support.microsoft.com/kb/827002/)
How to debug a script for a Microsoft Office InfoPath 2003 form
828853 (http://support.microsoft.com/kb/828853/) How to change the script language for your InfoPath form
Need More Help? Contact a Support professional by E-mail, Online or Phone.
Customer Service For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
Newsgroups Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.