How create XML as UTF-8 without BOM using XSLT with SSIS XML task tool?

Pavithra Subramanian 116 Reputation points
2020-10-29T16:48:55.087+00:00

Hi All,

I have edited the XML with UTF-8 without BOM using XML task XSLT operation. The generated output is having BOM. I want to generate without BOM.

Here is my XSLT declaration.

<?xmlversion="1.0"encoding="utf-8"?>

<xsl:stylesheetversion="2.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:outputmethod="xml"byte-order-mark="no"indent="yes"encoding="UTF-8"omit-xml-declaration="no"/>

Thanks

SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,565 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yitzhak Khabinsky 25,851 Reputation points
    2020-10-29T17:14:54.477+00:00

    Hi @Pavithra Subramanian ,

    Microsoft XSLT supports XSLT 1.0 standard only.

    Unfortunately, SSIS XML Task, operation XSLT transformation always emits BOM.
    A solution for that is to use SSIS Script Task for XSLT Transformations.
    Here is how to do it. As a bonus, it even shows how to pass parameters to the XSLT.

    c#

    void Main()  
    {  
       const string SOURCEXMLFILE = @"e:\Temp\UniversalShipment.xml";  
       const string XSLTFILE = @"e:\Temp\UniversalShipment.xslt";  
       const string OUTPUTXMLFILE = @"e:\temp\UniversalShipment_output.xml";  
       bool paramXSLT = true;  
      
       try  
       {  
          XsltArgumentList xslArg = new XsltArgumentList();  
          if (paramXSLT)  
          {  
             // Create a parameter which represents the current date and time.  
             DateTime d = DateTime.Now;  
             xslArg.AddParam("date", "", d.ToString());  
          }  
      
          using (XmlReader src = XmlReader.Create(SOURCEXMLFILE))  
          {  
             XslCompiledTransform xslt = new XslCompiledTransform();  
             xslt.Load(XSLTFILE, new XsltSettings(true, true), new XmlUrlResolver());  
      
             XmlWriterSettings settings = xslt.OutputSettings.Clone();  
             settings.IndentChars = "\t";  
             // to remove BOM  
             settings.Encoding = new UTF8Encoding(false);  
      
             using (XmlWriter result = XmlWriter.Create(OUTPUTXMLFILE, settings))  
             {  
                xslt.Transform(src, xslArg, result, new XmlUrlResolver());  
                result.Close();  
             }  
          }  
          Console.WriteLine("File '{0}' has been generated.", OUTPUTXMLFILE);  
       }  
       catch (Exception ex)  
       {  
          Console.WriteLine(ex.Message);  
       }  
    }  
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Monalv-MSFT 5,896 Reputation points
    2020-10-30T09:22:55.82+00:00

    Hi @Pavithra Subramanian ,

    Hope the following links will be helpful:

    XDocument: saving XML to file without BOM

    How to remove BOM from any text/XML file

    Best Regards,
    Mona

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    Hot issues in October--Users always get connection timeout problem when using multi subnet AG via listener. Especially after failover to another subnet.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.