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. - Change the group name to myBookstore, and then click OK.
- Right-click the myBookstore group, and then click Add.
The Add Field or Group dialog box appears. - Type the name myBooks, click Group from the list of types, and then click OK.
- Right-click the myBooks group, and then click Add.
The Add Field or Group dialog box appears. - Type the name myBook, click Group from the list of types, click Repeating, and then click OK.
- Right-click the myBook group, and then click Add.
The Add Field or Group dialog box appears. - Type the name myTitle, and then click OK.
- 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: - In the Design Tasks task pane, click Data Source.
- In the Data Source task pane, right-click myFields, and then click Properties.
- Change the group name to myBookstore, and then click OK.
- In the Data Source task pane, right-click myBookstore, and then click Add.
- In the Add Field or Group dialog box, type the name myBooks.
- In the Type drop-down list, select Group, and then click OK.
- In the Data Source task pane, right-click myBooks, and then click Add.
- In the Add Field or Group dialog box, type the name myBook.
- In the Type drop-down list, select Group.
- Click to select the Repeating check box, and then click OK.
- In the Data Source task pane, right-click myBook, and then click Add.
- In the Add Field or Group dialog box, type the name myTitle.
- In the Type drop-down list, select Group, and then click OK.
- In the Data Source task pane, right-click myTitle, and then click Add.
- In the Add Field or Group dialog box, type the name myISBN.
- In the Type drop-down list, select Group, and then click OK.
- In the Data Source task pane, right-click myTitle, and then click Add.
- In the Add Field or Group dialog box, type the name myGenre.
In the Type drop-down list, select Group, and then click OK.
- Move the myBook group from the Data Source task pane to your form, and then click Repeating Table.
- In the Data Source task pane, right-click the myBookstore group, and then click Properties.
The Field or Group Properties dialog box appears. - 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. - 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:
- Return to InfoPath.
Bookstore.xsn must be open in Design mode. - To add Xmlfile.xml as a secondary data source for your form, follow these steps.
In InfoPath 2003, follow these steps:- On the Tools menu, click Secondary Data Sources. The Secondary Data Sources dialog box appears.
- Click Add.
The Data Source Setup Wizard starts. - Click XML Data File for the data source type, and then click Next.
- Click Browse. In the Open dialog box, locate Xmlfile.xml, and then click Open. Click Next.
- Click Finish to complete the Data Source Setup Wizard. When you are prompted to add the file to your form, click No.
- Click Close to close the Secondary Data Sources dialog box.
In InfoPath 2007, follow these steps:- On the Tools menu, click Convert Main Data Source.
The Data Source Wizard dialog box appears.
- Click Browse.
The Open dialog box appears.
- Move to Xmlfile.xml, and then click Open.
- Click Next, and then click Finish to complete the Data Source Setup Wizard.
- To add Xsltfile.xslt as a secondary data source for your form, follow these steps:
- On the Tools menu in InfoPath 2003, click Resource Manager.
In InfoPath 2007, click Resource Files on the Tools menu . - Click Add, and then add Xsltfile.xslt.
- Click OK.
- For Infopath 2003, follow these steps:
- On the Tools menu, click Secondary Data Sources.
The Secondary Data Sources dialog box appears. - Click Add.
The Data Source Setup Wizard starts. - Click XML Data File for the data source type, and then click Next.
- Click Resource Manager. In the Resource Manager dialog box, click Xsltfile.xslt, and then click OK. Click Next.
- Click Finish to complete the Data Source Setup Wizard.
- Click Close to close the Secondary Data Sources dialog box.
For InfoPath 2007, follow these steps:- On the Tools menu, click Convert Main Data Source.
The Data Source Wizard dialog box appears.
- Click Browse. The Open dialog box appears.
- Move to Xsltfile.xslt, and then click Open.
- Click Next, and then click Finish to complete the Data Source Setup Wizard.
- 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. - 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 );
- 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:
- Return to the InfoPath form.
Bookstore.xsn must be open in Design mode. - To add Xmlfile.xml as a secondary data source for your form, follow these steps:
- On the Tools menu, click Data Connections.
The Data Connections dialog box appears. - Click Add.
The Data Connection Wizard appears. - Select Receive data, and then click Next.
- Select XML document for the data source type, and then click Next.
- Click Browse. In the Open dialog box, locate Xmlfile.xml, and then click Open. Click Next.
- Click Finish to complete the Data Source Setup Wizard.
- To bind the XML data to the controls, follow these steps:
- On the form, double-click Repeating Table.
The Repeating Table Properties dialog box appears. - On the Data tab, click Change Binding.
The Repeating Table Binding dialog box appears. - Click XMLFile (Secondary) in the Data Source drop-down list.
- Click the book (Group) node in the Specify which group to bind the rows to section, and then click Next.
- Click the Title node in the Data binding section, and the click Add.
- Repeat step e to add ISBN and genre to the Columns in table section.
- Click Finish.
- 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-0 | autobiography |
The Confidence Man | 0-201-63361-2 | novel |
The Gorgias | 1-861001-57-6 | philosophy |