I recently needed to create an InfoPath form that enabled the user to post the data to another user in a format they could open in *their* InfoPath form. After some thought, I went with the idea of running an XSL transform on the data and posting it via an email connector.
Of course, first I needed to create the XSLT. Obviously you can do this in notepad, but I prefer tools that are less likely to make my eyes bleed. I chose to go with Altova's MapForce - it's a great visual designer for mapping one schema to another:
I extracted the form files from each InfoPath form and loaded the myschema.xsd files as source and destination. MapForce handled most of the mapping, connecting similarly-named nodes. Then I generated the XSLT. I had to do a quick edit on the XSL to provide the processing instructions and pull out the schemaLocation attribute on the root. (You can probably do this in MapForce too, but I was in a rush...)
So, next thing - add the xslt to the form as a data connection. You want to create a data connection that will Receive Data from an XML file - then you can select your XSLT file and add it as a resource.
Now create a data connection to submit the form via email - set properties as needed.
Add a button and add form code - I'm working in jscript so that I didn't need a fully trusted form.
The final step, add the following code:
//Get the current form datavar CurrentForm=XDocument.DOM;
//Get the transformvar XSLTFileDOM=XDocument.GetDOM("MTCXSLT"); //Initialize the final xml DOMvar tmpDOM=new ActiveXObject("MSXML2.DomDocument.5.0");tmpDOM.validateOnParse=true;tmpDOM.async=false;
//Perform the transform; strDOM gets the XML Stringvar strDOM=CurrentForm.transformNode(XSLTFileDOM);
//Load the XML String from above into our DOM variabletmpDOM.loadXML(strDOM);
//Post the DOM via email. XDocument.DataAdapters("SubmitToMTC").SubmitData(tmpDOM);