Microsoft KB Archive/827005

= How to bind an XML data source to InfoPath fields in Office InfoPath =

Article ID: 827005

Article Last Modified on 1/30/2007

-

APPLIES TO


 * Microsoft Office InfoPath 2007
 * Microsoft Office InfoPath 2003, Service Pack 1 (SP1)
 * Microsoft Office InfoPath 2003

-



IN THIS TASK

 * INTRODUCTION
 * Design your form
 * Create the XML file and the XML style sheet
 * Bind fields to secondary data sources
 * Use a script with your form to transfer the data to your Form fields in InfoPath
 * Use the UI to bind fields to secondary data sources in InfoPath
 * Test your form with the XML transformation
 * REFERENCES



INTRODUCTION
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.

back to the top



Design your form
 Start a new blank form in InfoPath.

To open a blank form in Office InfoPath 2007, follow these steps:  Start InfoPath 2007. In the left pane of the Fill Out a Form dialog box, click Design a Form Template. In the Design a Form Template dialog box, click Blank, and then click OK.  Switch to the Data Source task pane and add fields to your form.

To add fields to your form in InfoPath 2003, follow these steps:  Right-click the myFields group, and then click Properties.

The Field or Group Properties dialog box appears.</li> Change the group name to myBookstore, and then click OK.</li> Right-click the myBookstore group, and then click Add.

The Add Field or Group dialog box appears.</li> Type the name myBooks, click Group from the list of types, and then click OK.</li> Right-click the myBooks group, and then click Add.

The Add Field or Group dialog box appears.</li> Type the name myBook, click Group from the list of types, click Repeating, and then click OK.</li> Right-click the myBook group, and then click Add.

The Add Field or Group dialog box appears.</li> Type the name myTitle, and then click OK.</li> Repeat step g and step h to create a field that is named myISBN and to create a field that is named myGenre.</li></ol>

To add fields to your form in InfoPath 2007, follow these steps:  In the Design Tasks task pane, click Data Source.</li> In the Data Source task pane, right-click myFields, and then click Properties.</li> Change the group name to myBookstore, and then click OK.</li> In the Data Source task pane, right-click myBookstore, and then click Add.</li> In the Add Field or Group dialog box, type the name myBooks.</li> In the Type drop-down list, select Group, and then click OK.</li> <li>In the Data Source task pane, right-click myBooks, and then click Add.</li> <li>In the Add Field or Group dialog box, type the name myBook .</li> <li>In the Type drop-down list, select Group.</li> <li>Click to select the Repeating check box, and then click OK.</li> <li>In the Data Source task pane, right-click myBook, and then click Add.</li> <li>In the Add Field or Group dialog box, type the name myTitle .</li> <li>In the Type drop-down list, select Group, and then click OK.</li> <li>In the Data Source task pane, right-click myTitle, and then click Add.</li> <li>In the Add Field or Group dialog box, type the name myISBN .</li> <li>In the Type drop-down list, select Group, and then click OK.</li> <li>In the Data Source task pane, right-click myTitle, and then click Add.</li> <li>In the Add Field or Group dialog box, type the name myGenre.

In the Type drop-down list, select Group, and then click OK.</li></ol> </li> <li>Move the myBook group from the Data Source task pane to your form, and then click Repeating Table.</li> <li>In the Data Source task pane, right-click the myBookstore group, and then click Properties.

The Field or Group Properties dialog box appears.</li> <li>Click the Details tab, and then make a note of the namespace.

The namespace is in the following form:

http://schemas.microsoft.com/office/infopath/2003/myXSD/2003-09-30T04:46:51

Click Cancel to dismiss the dialog box.</li> <li>Save your form as Bookstore.xsn.</li></ol>

back to the top

Create the XML file and the XML style sheet
Use an XML editor or a text editor such as Notepad to create Xmlfile.xml and to create Xsltfile.xslt as follows:

Xmlfile.xml
<?xml version='1.0' ?> <book genre=&quot;autobiography&quot; publicationdate=&quot;1981&quot; ISBN=&quot;1-861003-11-0&quot;> The Autobiography of Benjamin Franklin <book genre=&quot;novel&quot; publicationdate=&quot;1967&quot; ISBN=&quot;0-201-63361-2&quot;> The Confidence Man <book genre=&quot;philosophy&quot; publicationdate=&quot;1991&quot; ISBN=&quot;1-861001-57-6&quot;> The Gorgias

Xsltfile.xslt
<xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; xmlns:my=&quot;http://schemas.microsoft.com/office/infopath/2003/myXSD/2003-09-30T04:46:51&quot;> <xsl:template match=&quot;/&quot;> <xsl:apply-templates /> </xsl:template> <xsl:template match=&quot;bookstore&quot;> <my:myBooks> <xsl:apply-templates select=&quot;book&quot; /> </my:myBooks> </xsl:template> <xsl:template match=&quot;book&quot;> <my:myBook> <my:myTitle> <xsl:value-of select=&quot;title&quot; /> </my:myTitle> <my:myISBN> <xsl:value-of select=&quot;@ISBN&quot; /> </my:myISBN> <my:myGenre> <xsl:value-of select=&quot;@genre&quot; /> </my:myGenre> </my:myBook> </xsl:template> </xsl:stylesheet> Note Change the  namespace in Xsltfile.xslt to match the namespace that you created in step 5 in the previous section.

back to the top

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: <ol> <li>Return to InfoPath.

Bookstore.xsn must be open in Design mode.</li> <li>To add Xmlfile.xml as a secondary data source for your form, follow these steps.

In InfoPath 2003, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>On the Tools menu, click Secondary Data Sources. The Secondary Data Sources dialog box appears.</li> <li>Click Add.

The Data Source Setup Wizard starts.</li> <li>Click XML Data File for the data source type, and then click Next.</li> <li>Click Browse. In the Open dialog box, locate Xmlfile.xml, and then click Open. Click Next.</li> <li>Click Finish to complete the Data Source Setup Wizard. When you are prompted to add the file to your form, click No.</li> <li>Click Close to close the Secondary Data Sources dialog box.</li></ol>

In InfoPath 2007, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>On the Tools menu, click Convert Main Data Source. The Data Source Wizard dialog box appears.</li> <li>Click Browse. The Open dialog box appears.</li> <li>Move to Xmlfile.xml, and then click Open.</li> <li>Click Next, and then click Finish to complete the Data Source Setup Wizard.</li></ol> </li> <li>To add Xsltfile.xslt as a secondary data source for your form, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>On the Tools menu in InfoPath 2003, click Resource Manager.

In InfoPath 2007, click Resource Files on the Tools menu .</li> <li>Click Add, and then add Xsltfile.xslt.</li> <li>Click OK.</li> <li>For Infopath 2003, follow these steps: <ol> <li>On the Tools menu, click Secondary Data Sources.

The Secondary Data Sources dialog box appears.</li> <li>Click Add.

The Data Source Setup Wizard starts.</li> <li>Click XML Data File for the data source type, and then click Next.</li> <li>Click Resource Manager. In the Resource Manager dialog box, click Xsltfile.xslt, and then click OK. Click Next.</li> <li>Click Finish to complete the Data Source Setup Wizard.</li> <li>Click Close to close the Secondary Data Sources dialog box.</li></ol>

For InfoPath 2007, follow these steps: <ol> <li>On the Tools menu, click Convert Main Data Source. The Data Source Wizard dialog box appears.</li> <li>Click Browse. The Open dialog box appears.</li> <li>Move to Xsltfile.xslt, and then click Open.</li> <li>Click Next, and then click Finish to complete the Data Source Setup Wizard.</li></ol> </li></ol> </li> <li>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.</li> <li> 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(&quot;XMLFile&quot;).Query; var XMLFileDOM = XDocument.GetDOM(&quot;XMLFile&quot;);

//Get the transform that is stored as a secondary data source. var XSLTFileDOM = XDocument.GetDOM(&quot;XSLTFile&quot;);

//Transform Xmlfile by using the style sheet that is named Xsltfile. var TransformedDOM = new ActiveXObject(&quot;MSXML2.DomDocument.5.0&quot;); 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( &quot;/my:myBookstore&quot; ); var oBooks_Dest = oBookstore.selectSingleNode(&quot;my:myBooks&quot;); 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 ); </li> <li>Save your changes to the script, and then close Script Editor.</li></ol>

back to the top

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: <ol> <li>Return to the InfoPath form.

Bookstore.xsn must be open in Design mode.</li> <li>To add Xmlfile.xml as a secondary data source for your form, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>On the Tools menu, click Data Connections.

The Data Connections dialog box appears.</li> <li>Click Add.

The Data Connection Wizard appears.</li> <li>Select Receive data, and then click Next.</li> <li>Select XML document for the data source type, and then click Next.</li> <li>Click Browse. In the Open dialog box, locate Xmlfile.xml, and then click Open. Click Next.</li> <li>Click Finish to complete the Data Source Setup Wizard.</li></ol> </li> <li>To bind the XML data to the controls, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>On the form, double-click Repeating Table.

The Repeating Table Properties dialog box appears.</li> <li>On the Data tab, click Change Binding.

The Repeating Table Binding dialog box appears.</li> <li>Click XMLFile (Secondary) in the Data Source drop-down list.</li> <li>Click the book (Group) node in the Specify which group to bind the rows to section, and then click Next.</li> <li>Click the Title node in the Data binding section, and the click Add.</li> <li>Repeat step e to add ISBN and genre to the Columns in table section.</li> <li>Click Finish.</li></ol> </li> <li>Save your forms.</li></ol>

back to the top

Test your form with the XML transformation
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:

back to the top

<div class="references_section">