Using Custom XSLT in InfoPath Form Templates

Applies to: InfoPath 2010 | InfoPath Forms Services | Office 2010 | SharePoint Server 2010 | Visual Studio | Visual Studio Tools for Microsoft Office

You can create most of the view elements you're likely to need in the Microsoft InfoPath 2010 form designer. If you need a custom view element that InfoPath can't create for you, however, you can manually modify the XSL Transformation (XSLT) that InfoPath uses to generate the view. To do so, extract the form into its component files by using Export Source Files on the Publish tab of the Microsoft Office Backstage, and then edit the transform in your preferred XML editor, such as Microsoft Visual Studio or Notepad.

If you make changes to a view transform outside of InfoPath and then open the view in design mode and make changes, InfoPath will overwrite the changes you made manually. To keep InfoPath from overwriting the changes you make, you must place those changes in an <xsl:template> element in the transform and use the xd:preserve mode, as shown here:

<xsl:template match="my:field1" mode="xd:preserve"> 
   <div> 
      The value of field1 is <xsl:value-of select="."/> 
   </div> 
</xsl:template>

To include the template in the transformed file, use the <xsl:apply-templates> element with the same xd:preserve mode:

<xsl:apply-templates select="my:field1" mode="xd:preserve"/>

Elements and constructs defined within XSL templates with the xd:preserve mode will not be displayed in the InfoPath design environment. Instead, InfoPath will mark the custom section with a control labeled Preserve Code Block with a red border. When a user opens the form to fill it out, the custom XSL transforms are applied and the Preserve Code Block controls will not appear.