.NET XslTransform 之谜 - 转换输出中的 META 字符集

发布于 2024-11-08 05:21:35 字数 1866 浏览 2 评论 0原文

我有以下代码:

        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 技术交流群。

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

发布评论

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

评论(1

纵性 2024-11-15 05:21:35

那么,要么您的样式表具有 ,要么结果树的根元素具有本地名称 html 并且不在命名空间中。在这两种情况下,XSLT 规范都要求 XSLT 处理器在序列化结果树时在标头部分添加具有内容类型和字符集的元元素。

Well either your stylesheet has <xsl:output method="html"/> or the root element of the result tree has the local name html 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.

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