通过代码进行的 XSL 转换不会产生与通过工具完成的 XSL 转换相同的输出

发布于 2024-09-12 04:02:11 字数 1440 浏览 0 评论 0原文

我正在使用 .NET Framework 3.5 中提供的 XslCompiledTransform 类将我的 XML 文件转换为另一个 XML 文件,

这是我的代码。

private static void transformUtil(string sXmlPath, string sXslPath, string outputFileName)
    {
        try
        {

            XPathDocument myXPathDoc = new XPathDocument(sXmlPath);
            XslCompiledTransform myXslTrans = new XslCompiledTransform();
            //load the Xsl 
            myXslTrans.Load(sXslPath);
            //create the output stream
            XmlTextWriter myWriter = new XmlTextWriter(outputFileName, null);
            //do the actual transform of Xml
            myXslTrans.Transform(myXPathDoc, null, myWriter);
            myWriter.Close();
        }
        catch (Exception e)
        {

            EventLogger eventLog;
            eventLog = new EventLogger("transformUtil", e.ToString());
        }

    }
}

该代码有效,但输出文件标头中没有 XML 声明。

**<?xml version="1.0" encoding="utf-8"?>**

我很不明白这一点。当我使用同一个 XSL 文件通过 notepad++ 或 Visual Studio 等工具转换 XML 时,转换将在标头中包含 XML 声明。那么 XslCompiledTransform 是否负责截断此声明?我很困惑。

还有其他人面临类似的问题吗?

我的 XSL 文件头如下所示。

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:template match="/">

I am transforming my XML file to another XML file using the XslCompiledTransform classes available in the .NET framework 3.5

Here's my code.

private static void transformUtil(string sXmlPath, string sXslPath, string outputFileName)
    {
        try
        {

            XPathDocument myXPathDoc = new XPathDocument(sXmlPath);
            XslCompiledTransform myXslTrans = new XslCompiledTransform();
            //load the Xsl 
            myXslTrans.Load(sXslPath);
            //create the output stream
            XmlTextWriter myWriter = new XmlTextWriter(outputFileName, null);
            //do the actual transform of Xml
            myXslTrans.Transform(myXPathDoc, null, myWriter);
            myWriter.Close();
        }
        catch (Exception e)
        {

            EventLogger eventLog;
            eventLog = new EventLogger("transformUtil", e.ToString());
        }

    }
}

The code works, but the output file does not have the XML declaration in the header.

**<?xml version="1.0" encoding="utf-8"?>**

I am at a loss to understand this. When I use the same XSL file to transform the XML, using a tool like notepad++ or visual studio, the transformation contains the XML declaration in the header. So is XslCompiledTransform responsible for truncating this declaration? I am puzzled.

Anyone else facing similar issues?

My XSL file header looks like this.

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:template match="/">

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肩上的翅膀 2024-09-19 04:02:11

使用的 XML 编写器没有任何与其关联的设置。

更改

//create the output stream
XmlTextWriter myWriter = new XmlTextWriter(outputFileName, null);

XmlWriterSettings settings = 
   new XmlWriterSettings
   {
      OmitXmlDeclaration = false
   };
XmlWriter myWriter = XmlWriter.Create(outputFileName, settings);

或者,您可以减少设置转换的次数:

private static void transformUtil(string sXmlPath, string sXslPath, 
                                  string outputFileName)
{
    try
    {
       XslCompiledTransform xsl = new XslCompiledTransform();

       // Load the XSL
       xsl.Load(sXslPath);

       // Transform the XML document
       xsl.Transform(sXmlPath, outputFileName);
    }
    catch (Exception e)
    {
       // Handle exception
    }

}

这也应该遵循 xsl:output 来自 XSLT 文件本身的指令,特别是 omit-xml-declaration 属性,其默认值为“no”,如果未指定。

The XML writer used does not have any settings associated with it.

change

//create the output stream
XmlTextWriter myWriter = new XmlTextWriter(outputFileName, null);

to

XmlWriterSettings settings = 
   new XmlWriterSettings
   {
      OmitXmlDeclaration = false
   };
XmlWriter myWriter = XmlWriter.Create(outputFileName, settings);

Alternatively, you could do less to setup the transform:

private static void transformUtil(string sXmlPath, string sXslPath, 
                                  string outputFileName)
{
    try
    {
       XslCompiledTransform xsl = new XslCompiledTransform();

       // Load the XSL
       xsl.Load(sXslPath);

       // Transform the XML document
       xsl.Transform(sXmlPath, outputFileName);
    }
    catch (Exception e)
    {
       // Handle exception
    }

}

This should also honor the xsl:output instructions from the XSLT file itself, in particular the omit-xml-declaration attribute, for which the default value is "no" if left unspecified.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文