Ghasem
Ghasem

Reputation: 15623

How to create XSL for an XML

In my C# application, I get a word document's XML code and I want to convert it to HTML using XslCompiledTransform, just like this answer and this one suggest.

But the problem is how to get or create the XSL stylesheet to use in this line:

var myXslTrans = new XslCompiledTransform(); 
myXslTrans.Load("stylesheet.xsl");  //<- How to get this
myXslTrans.Transform("source.xml","result.html");

In this tutorial, it shows how to create an XSL for an XML. But is it possible to do it programmatically for whatever XML you have?

It's a sample of what XML document I got:

<w:wordDocument xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" xmlns:wsp="http://schemas.microsoft.com/office/word/2003/wordml/sp2" xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core"
  w:macrosPresent="no"
  w:embeddedObjPresent="no"
  w:ocxPresent="no"
  xml:space="preserve">
  <w:ignoreSubtree
    w:val="http://schemas.microsoft.com/office/word/2003/wordml/sp2" />
  <o:DocumentProperties>
    <o:Author>Soft</o:Author>
    <o:LastAuthor>Soft</o:LastAuthor>
    <o:Revision>1</o:Revision>
    <o:TotalTime>0</o:TotalTime>
    <o:Created>2015-05-12T03:13:00Z</o:Created>
    <o:LastSaved>2015-05-12T03:13:00Z</o:LastSaved>
    <o:Pages>1</o:Pages>
    <o:Words>58</o:Words>
    <o:Characters>336</o:Characters>
    <o:Lines>2</o:Lines>
    <o:Paragraphs>1</o:Paragraphs>
    <o:CharactersWithSpaces>393</o:CharactersWithSpaces>
    <o:Version>12</o:Version>
  </o:DocumentProperties>
  <o:CustomDocumentProperties>
    <o:EDOID
      dt:dt="float">657360</o:EDOID>
  </o:CustomDocumentProperties>
  <w:fonts>
    <w:defaultFonts
      w:ascii="Calibri"
      w:fareast="Calibri"
      w:h-ansi="Calibri"
      w:cs="Arial" />
    <w:font
      w:name="Arial">
      <w:panose-1
        w:val="020B0604020202020204" />
      <w:charset
        w:val="00" />
      <w:family
        w:val="Swiss" />
      <w:pitch
        w:val="variable" />
      <w:sig
        w:usb-0="E0002AFF"
        w:usb-1="C0007843"
        w:usb-2="00000009"
        w:usb-3="00000000"
        w:csb-0="000001FF"
        w:csb-1="00000000" />
    </w:font>
    <w:font
      w:name="Symbol">
      <w:panose-1
        w:val="05050102010706020507" />
      <w:charset
        w:val="02" />
      <w:family
        w:val="Roman" />
      <w:pitch
        w:val="variable" />
      <w:sig
        w:usb-0="00000000"
        w:usb-1="10000000"
        w:usb-2="00000000"
        w:usb-3="00000000"
        w:csb-0="80000000"
        w:csb-1="00000000" />
    </w:font>
    <w:font
      w:name="Cambria Math">
      <w:panose-1
        w:val="02040503050406030204" />
      <w:charset
        w:val="01" />
      <w:family
        w:val="Roman" />
      <w:notTrueType />
      <w:pitch
        w:val="variable" />
      <w:sig
        w:usb-0="00000000"
        w:usb-1="00000000"
        w:usb-2="00000000"
        w:usb-3="00000000"
        w:csb-0="00000000"
        w:csb-1="00000000" />
    </w:font>
    <w:font
      w:name="Calibri">
      <w:panose-1
        w:val="020F0502020204030204" />
      <w:charset
        w:val="00" />
      <w:family
        w:val="Swiss" />
      <w:pitch
        w:val="variable" />
      <w:sig
        w:usb-0="E10002FF"
        w:usb-1="4000ACFF"
        w:usb-2="00000009"
        w:usb-3="00000000"
        w:csb-0="0000019F"
        w:csb-1="00000000" />
    </w:font>
    <w:font
      w:name="B Titr">
      <w:panose-1
        w:val="00000700000000000000" />
      <w:charset
        w:val="B2" />
      <w:family
        w:val="auto" />
      <w:pitch
        w:val="variable" />
      <w:sig
        w:usb-0="00002001"
        w:usb-1="80000000"
        w:usb-2="00000008"
        w:usb-3="00000000"
        w:csb-0="00000040"
        w:csb-1="00000000" />
...
  </w:body>
</w:wordDocument>

Update

Reading the comments makes me believe that it's not possible to create XSL for whatever XML document programmatically.

So how can I get XSl for this specific XML which is in my question? If I open this XML in Microsoft word it shows the real word document with all styles. I know there is HTML code for it as well. So I want to get the HTML code to show the same result in Microsoft word

Upvotes: 1

Views: 1634

Answers (3)

Sebastian G. Marinescu
Sebastian G. Marinescu

Reputation: 2394

Can't you use XSL-Parameters for your dynamic work?

https://msdn.microsoft.com/en-us/library/dfktf882%28v=vs.110%29.aspx

You could pass some parameter for switching through some xsl-statements.

Upvotes: 0

killer
killer

Reputation: 602

Updated answer:

As we know XSLT is an XML document, we can simply create an xml file with xslt attritubes and extension. we can follow this strategy.

... 
XDocument document = documentBuilder.newDocument();

Element rootElement = document.createElement("xsl:stylesheet");
// adding attributes like namespaces etc... 

document.appendChild(rootElement); 
Element em = document.createElement("xsl:template");
em.setAttribute("match", "/"); 
....

This is just an idea. You can design your xslt document like this and follow the correct syntax.Apology for not understanding question in the first answer.Hope this helps

Upvotes: -1

Jeremy Thompson
Jeremy Thompson

Reputation: 65722

So how can I get XSL for this specific XML which is in my question?

Use Microsoft Words XSL file:
C:\Program Files\Microsoft Office\Office15\XML2WORD.XSL

Upvotes: 2

Related Questions