Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

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


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:
    1. Start InfoPath 2007.
    2. In the left pane of the Fill Out a Form dialog box, click Design a Form Template.
    3. 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:
    1. Right-click the myFields group, and then click Properties.

      The Field or Group Properties dialog box appears.
    2. Change the group name to myBookstore, and then click OK.
    3. Right-click the myBookstore group, and then click Add.

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

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

      The Add Field or Group dialog box appears.
    8. Type the name myTitle, and then click OK.
    9. 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:
    1. In the Design Tasks task pane, click Data Source.
    2. In the Data Source task pane, right-click myFields, and then click Properties.
    3. Change the group name to myBookstore, and then click OK.
    4. In the Data Source task pane, right-click myBookstore, and then click Add.
    5. In the Add Field or Group dialog box, type the name myBooks.
    6. In the Type drop-down list, select Group, and then click OK.
    7. In the Data Source task pane, right-click myBooks, and then click Add.
    8. In the Add Field or Group dialog box, type the name myBook.
    9. In the Type drop-down list, select Group.
    10. Click to select the Repeating check box, and then click OK.
    11. In the Data Source task pane, right-click myBook, and then click Add.
    12. In the Add Field or Group dialog box, type the name myTitle.
    13. In the Type drop-down list, select Group, and then click OK.
    14. In the Data Source task pane, right-click myTitle, and then click Add.
    15. In the Add Field or Group dialog box, type the name myISBN.
    16. In the Type drop-down list, select Group, and then click OK.
    17. In the Data Source task pane, right-click myTitle, and then click Add.
    18. 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:
    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 Browse. In the Open dialog box, locate Xmlfile.xml, and then click Open. Click Next.
    5. Click Finish to complete the Data Source Setup Wizard. When you are prompted to add the file to your form, click No.
    6. Click Close to close the Secondary Data Sources dialog box.
    In 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 Xmlfile.xml, and then click Open.
    4. 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:
    1. On the Tools menu in InfoPath 2003, click Resource Manager.

      In InfoPath 2007, click Resource Files on the Tools menu .
    2. Click Add, and then add Xsltfile.xslt.
    3. Click OK.
    4. 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:
    1. On the Tools menu, click Data Connections.

      The Data Connections dialog box appears.
    2. Click Add.

      The Data Connection Wizard appears.
    3. Select Receive data, and then click Next.
    4. Select XML document for the data source type, and then click Next.
    5. Click Browse. In the Open dialog box, locate Xmlfile.xml, and then click Open. Click Next.
    6. Click Finish to complete the Data Source Setup Wizard.
  3. To bind the XML data to the controls, follow these steps:
    1. On the form, double-click Repeating Table.

      The Repeating Table Properties dialog box appears.
    2. On the Data tab, click Change Binding.

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

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:
Collapse this tableExpand this table
The Autobiography of Benjamin Franklin 1-861003-11-0autobiography
The Confidence Man0-201-63361-2novel
The Gorgias1-861001-57-6philosophy

↑ Back to the top


References

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� 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� How to debug a script for a Microsoft Office InfoPath 2003 form
828853� How to change the script language for your InfoPath form

↑ Back to the top


Keywords: KB827005, kbhowto, kbxml

↑ Back to the top

Article Info
Article ID : 827005
Revision : 4
Created on : 1/30/2007
Published on : 1/30/2007
Exists online : False
Views : 358