none
how to create XSLT for creating XML to .doc file?? RRS feed

  • Question

  • Hello,

      i am using xml & xslt for generate MS word doc.but my problem start after that as document is being created but there is no formatting. How to write XSLT for formatting????

    simple steps for generating ms doc file are-

    1) creating xml file

    2)creating xslt file

     use both files as embeed resourses

    3)transfom xslt and generate MS doc file

     

     

    • Moved by edhickey Tuesday, August 23, 2011 1:46 PM (From:.NET 3.0/3.5 Windows Workflow Foundation)
    • Moved by Cindy Meister MVPModerator Tuesday, August 23, 2011 3:44 PM Open XML related (From:Word for Developers)
    Tuesday, August 23, 2011 9:02 AM

All replies

  • I'm not quite sure what you are asking. An XLST transforms XML from one structure to another; it doesn't generate content or formatting, although there is some capacity to provide context-sensitive data in the code. If you have the formatting then you can transform it in much the same way as you transform the content.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Tuesday, August 23, 2011 3:34 PM
  • Hi Rachel

    I think the more appropriate forum for this would be the Office Open XML SDK forum, as that's the forum that deals with the open file formats. I assume that is what you're trying to use. I'm going to move your message there.

    Also, you should consult the information available at OpenXMLDeveloper.org. You'll find all the information there and in the Open XML SDK on how to include formatting information in a Word Open XML file.

    another possibility is to SAVE a Word document file that contains the formatting you require, then open it up and look at the XML.


    Cindy Meister, VSTO/Word MVP
    Tuesday, August 23, 2011 3:44 PM
    Moderator
  • Ok.Thanks

    Hi.I want to generate word file from the XML in a descriptive format.

    Till now i am just being able to create file with the data but there is no format or spaces.

    strXML='<Root><VersionObjectIdList>
                            <ObjectID>2438</ObjectID>
                          </VersionObjectIdList>
                          <Components>
                            <Component>TESTDATAONE</Component>
                          </Components>
                          <Tables>
                            <Table>SAMPLE</Table>
                          </Tables>
                          <StoredProcedures>
               </Root>'

     ////

    public bool GenerateWordFile(strXml)
            {
                DataSet ds;
                XmlDataDocument xmlDoc;
                XslCompiledTransform xslTran;
                XmlElement root;
                XPathNavigator nav;
                XmlTextWriter writer;
                try
                {
                    // objVersionReleaseBL.
                    //Convertion starts from here
                    XmlDocument XDoc = new XmlDocument();
                    XDoc.LoadXml(strXML);
                    XDoc.Save(@"genereateWord.xml");
                    //Create the DataSet from the XML file
                    ds = new DataSet();
                    ds.ReadXml("genereateWord.xml");
                    //Create the XML from the DataSet
                    xmlDoc = new XmlDataDocument(ds);
                    //Load the XSLT for Transformation
                    xslTran = new XslCompiledTransform();
                    xslTran.Load("genereateWord.xslt");
                    //Determine the Root object in the XML
                    root = xmlDoc.DocumentElement;
                    //Create the XPath Navigator to navigate throuth the XML
                    nav = root.CreateNavigator();
                    //First delete the RTF, if already exist
                    if (File.Exists("genereateWord.rtf"))
                    {
                        File.Delete("genereateWord.rtf");
                    }
                    //Create the RTF by Transforming the XML and XSLT
                    writer = new XmlTextWriter("genereateWord.rtf", System.Text.Encoding.Default);
                    xslTran.Transform(nav, writer);
                    //Close the Writer after Transformation
                    writer.Close();
                    //Release all objects
                    writer = null;
                    nav = null;
                    root = null;
                    xmlDoc = null;
                    ds = null;

                }

                catch (Exception ex)
                {
                }
                return true;
            }

    Wednesday, August 24, 2011 9:25 AM
  • Thanks

    but still not able to get formatting part.

    How to format the word file created?showing labels ,proper indentation etc

    Friday, August 26, 2011 10:34 AM
  • Hi Rachel17,

    After you have generated the word file, you can use Open XML SDK to set the format for the text. For example, here is an article about setting the font for the text:

    http://msdn.microsoft.com/en-us/library/cc850848.aspx

    and you can also set the indentation:

    http://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.indentation.aspx

    Hope this can help you and just feel free to follow up after you have tried.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Bruce Song Monday, September 5, 2011 11:32 AM
    Tuesday, August 30, 2011 6:03 AM