.NET XslTransform 之谜 - 转换输出中的 META 字符集
我有以下代码:
using (Stream stream = new MemoryStream())
{
xslt.Transform(document, xslArg, stream);
stream.Seek(0, SeekOrigin.Begin);
StreamReader reader = new StreamReader(stream);
var result = reader.ReadToEnd();
return result;
}
该转换输出 HTML 文档。让我困惑的是,即使输入 xsl contains:
<html>
<head>
<style>
@page Section1
{size:612.0pt 792.0pt;
margin:42.55pt 42.55pt 42.55pt 70.9pt;
mso-header-margin:35.45pt;
mso-footer-margin:35.45pt;
mso-paper-source:0;}
div.Section1
{page:Section1;}
</head>
<body>
<div class="Section1">
.....
输出是 :
<html xmlns:ms="urn:schemas-microsoft-com:xslt" xmlns:dt="urn:schemas-microsoft-com:datatypes">
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>.....
如您所见,除了其他内容之外,还添加了字符集信息。
但真正令我惊讶的是,当我将代码更改为:
StringBuilder sb = new StringBuilder();
using (StringWriter writer = new StringWriter(sb))
{
xslt.Transform(document, xslArg, writer);
}
var result = sb.ToString();
return result;
生成的输出具有以下形式:
<html xmlns:ms="urn:schemas-microsoft-com:xslt" xmlns:dt="urn:schemas-microsoft-com:datatypes">
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-16">
<style>....
如您所见,字符集已更改。我猜这是因为 StringBuilder 和 .NET 默认使用 UTF-16 运行。但是,为什么转换会附加带有字符集的 META 标记呢?
I have following piece of code:
using (Stream stream = new MemoryStream())
{
xslt.Transform(document, xslArg, stream);
stream.Seek(0, SeekOrigin.Begin);
StreamReader reader = new StreamReader(stream);
var result = reader.ReadToEnd();
return result;
}
That transformation outputs HTML document. What is bewildering to me is that even though the input xsl contains:
<html>
<head>
<style>
@page Section1
{size:612.0pt 792.0pt;
margin:42.55pt 42.55pt 42.55pt 70.9pt;
mso-header-margin:35.45pt;
mso-footer-margin:35.45pt;
mso-paper-source:0;}
div.Section1
{page:Section1;}
</head>
<body>
<div class="Section1">
.....
output is :
<html xmlns:ms="urn:schemas-microsoft-com:xslt" xmlns:dt="urn:schemas-microsoft-com:datatypes">
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>.....
as you see, charset info was added, apart from other stuff.
But what really amazed me, was that when I changed code that makes transformation into:
StringBuilder sb = new StringBuilder();
using (StringWriter writer = new StringWriter(sb))
{
xslt.Transform(document, xslArg, writer);
}
var result = sb.ToString();
return result;
generated output had the following form:
<html xmlns:ms="urn:schemas-microsoft-com:xslt" xmlns:dt="urn:schemas-microsoft-com:datatypes">
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-16">
<style>....
As you can see, charset has changed. I guess it's because StringBuilder, and .NET by default operates using UTF-16. But, why transformation appends META tag with charset anyway?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么,要么您的样式表具有
,要么结果树的根元素具有本地名称html
并且不在命名空间中。在这两种情况下,XSLT 规范都要求 XSLT 处理器在序列化结果树时在标头部分添加具有内容类型和字符集的元元素。Well either your stylesheet has
<xsl:output method="html"/>
or the root element of the result tree has the local namehtml
and is in no namespace. In both cases the XSLT specification mandates that the XSLT processors adds a meta element with content type and charset in the head section when serializing the result tree.