Search code examples
c#xsltxmlwriter

Writing an XSLT Stylesheet Header


I'm building a C# application that will build me XSLT files based on a pre-configured file.

I can generate the XSLT file, and its close to what I want, but I'm having a couple issues.

Issue 1:
The stylesheet header at the top of the XSLT file is formatting weird. Here is what I'm expecting:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="1.0" xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">

This is what I'm getting:

<xsl:stylesheet p1:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="1.0" xmlns="urn:schemas-microsoft-com:office:spreadsheet" p3:o="urn:schemas-microsoft-com:office:office"
p3:x="urn:schemas-microsoft-com:office:excel" p3:ss="urn:schemas-microsoft-com:office:spreadsheet" p3:html="http://www.w3.org/TR/REC-html40" xmlns:p3="xmlns" xmlns:p1="stylesheet"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

And here is the C# Code:

//Write the namespaces for the xslt
        xmlWriter.WriteStartElement("xsl", "stylesheet", "http://www.w3.org/1999/XSL/Transform");
        xmlWriter.WriteAttributeString("xs", "stylesheet", "http://www.w3.org/2001/XMLSchema");
        xmlWriter.WriteAttributeString("exclude-result-prefixes", "xs");
        xmlWriter.WriteAttributeString("version", "1.0");
        xmlWriter.WriteAttributeString("xmlns", "urn:schemas-microsoft-com:office:spreadsheet");
        xmlWriter.WriteAttributeString("o", "xmlns", "urn:schemas-microsoft-com:office:office");
        xmlWriter.WriteAttributeString("x", "xmlns", "urn:schemas-microsoft-com:office:excel");
        xmlWriter.WriteAttributeString("ss", "xmlns", "urn:schemas-microsoft-com:office:spreadsheet");
        xmlWriter.WriteAttributeString("html", "xmlns", "http://www.w3.org/TR/REC-html40");

Issue 2:
In the general body of my XSLT file, there are multiple locations where these "p" declarations seems to be showing up. In my output above, an example is:

p3:x="urn:schemas-microsoft-com:office:excel"

I think I'm miscalling the method in some way, but I'm not sure how to correct this.


Solution

  • It seems the arguments need to alter the position and be used in proper way.

    ref: https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmlwriter.writeattributestring?view=netframework-4.7.2

    In your case, It should be written as

    For Example :

    xmlWriter.WriteAttributeString("xmlns", "ss", null, "urn:schemas-microsoft-com:office:spreadsheet");
    

    Because WriteAttributeString(String, String, String, String)

    When overridden in a derived class, writes out the attribute with the specified prefix, local name, namespace URI, and value.

    public void WriteAttributeString (string prefix, string localName, string ns, string value);