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

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.

MORE INFORMATION

Design your form

1. Start a new blank form in InfoPath.To open a blank form in Office InfoPath 2007, follow these steps:

a. Start InfoPath 2007.
b. In the left pane of the Fill Out a Form dialog box, click Design a Form Template.
c. In the Design a Form Template dialog box, click Blank, and then click OK.
2. 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:

a. Right-click the myFields group, and then click Properties.The Field or Group Properties dialog box appears.
b. Change the group name to myBookstore, and then click OK.
c. Right-click the myBookstore group, and then click Add.The Add Field or Group dialog box appears.
d. Type the name myBooks, click Group from the list of types, and then click OK.
e. Right-click the myBooks group, and then click Add.The Add Field or Group dialog box appears.
f. Type the name myBook, click Group from the list of types, click Repeating, and then click OK.
g. Right-click the myBook group, and then click Add.The Add Field or Group dialog box appears.
h. Type the name myTitle, and then click OK.
i. Repeat step g and step h to create a field that is named myISBN and to create a field that is named myGenre.

To add fields to your form in InfoPath 2007, follow these steps:

a. In the Design Tasks task pane, click Data Source.
b. In the Data Source task pane, right-click myFields, and then click Properties.
c. Change the group name to myBookstore, and then click OK.
d. In the Data Source task pane, right-click myBookstore, and then click Add.
e. In the Add Field or Group dialog box, type the name myBooks.
f. In the Type drop-down list, select Group, and then click OK.
g. In the Data Source task pane, right-click myBooks, and then click Add.
h. In the Add Field or Group dialog box, type the name myBook.
i. In the Type drop-down list, select Group.
j. Click to select the Repeating check box, and then click OK.
k. In the Data Source task pane, right-click myBook, and then click Add.
l. In the Add Field or Group dialog box, type the name myTitle.
m. In the Type drop-down list, select Group, and then click OK.
n. In the Data Source task pane, right-click myTitle, and then click Add.
o. In the Add Field or Group dialog box, type the name myISBN.
p. In the Type drop-down list, select Group, and then click OK.
q. In the Data Source task pane, right-click myTitle, and then click Add.
r. In the Add Field or Group dialog box, type the name myGenre.
In the Type drop-down list, select Group, and then click OK.
3. Move the myBook group from the Data Source task pane to your form, and then click Repeating Table.
4. In the Data Source task pane, right-click the myBookstore group, and then click Properties.The Field or Group Properties dialog box appears.
5. 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.

6. Save your form as Bookstore.xsn.

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' ?>
<bookstore>
        <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
               <title>The Autobiography of Benjamin Franklin</title>
        </book>
        <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
               <title>The Confidence Man</title>
        </book>
        <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
               <title>The Gorgias</title>
        </book>
</bookstore>

Xsltfile.xslt

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2003-09-30T04:46:51">
        <xsl:template match="/">
                       <xsl:apply-templates />
        </xsl:template>
        <xsl:template match="bookstore">
                       <my:myBooks>
                               <xsl:apply-templates select="book" />
                       </my:myBooks>
        </xsl:template>
        <xsl:template match="book">
               <my:myBook>
                       <my:myTitle>
                               <xsl:value-of select="title" />
                       </my:myTitle>
                       <my:myISBN>
                               <xsl:value-of select="@ISBN" />
                       </my:myISBN>
                       <my:myGenre>
                               <xsl:value-of select="@genre" />
                       </my:myGenre>
               </my:myBook>
        </xsl:template>
</xsl:stylesheet>

Note Change the my namespace in Xsltfile.xslt to match the namespace that you created in step 5 in the previous section.

Bind fields to secondary data sources

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.
g. Click Finish.
4. Save your forms.

——————————————–

Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

 


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Leave a Reply

*
To prove that you're not a bot, enter this code
Anti-Spam Image