通过代码进行的 XSL 转换不会产生与通过工具完成的 XSL 转换相同的输出
我正在使用 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用的 XML 编写器没有任何与其关联的设置。
更改
到
或者,您可以减少设置转换的次数:
这也应该遵循 xsl:output 来自 XSLT 文件本身的指令,特别是
omit-xml-declaration
属性,其默认值为“no”,如果未指定。The XML writer used does not have any settings associated with it.
change
to
Alternatively, you could do less to setup the transform:
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.