使用 MSXML 的 XSLT 转换未使用正确的编码

发布于 2024-10-21 12:34:40 字数 1086 浏览 1 评论 0原文

我正在使用 IXMLDOMDocument::transformNode 从 MSXML 3.0 开始应用 XSLT 转换。每个转换都有一个 xsl:output 指令,指定 UTF-8 作为编码。例如,

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                ...
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:str="http://exslt.org/strings"
                xmlns:math="http://exslt.org/math"
                extension-element-prefixes="str math">
  <xsl:output encoding="UTF-8" indent="yes" method="xml" />
  ...
</xsl:stylesheet>

然而转换后的结果始终是 UTF-16(编码属性为 UTF-16)。

<?xml version="1.0" encoding="UTF-16"?>

这是 MSXML 中的错误吗?

由于各种原因,我真的很想要 UTF-8。有解决方法吗?或者我是否必须自己将转换后的结果转换为 UTF-8 并修补编码属性?

更新:我已经通过接受 UTF-16 编码并在前面添加字节顺序标记来解决这个问题,这满足了转换结果的下游用户的需求,但是我我仍然对如何获取 UTF-8 输出感兴趣。

I'm using IXMLDOMDocument::transformNode from MSXML 3.0 to apply XSLT transforms. Each of the transforms has an xsl:output directive that specifies UTF-8 as the encoding. For example,

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                ...
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:str="http://exslt.org/strings"
                xmlns:math="http://exslt.org/math"
                extension-element-prefixes="str math">
  <xsl:output encoding="UTF-8" indent="yes" method="xml" />
  ...
</xsl:stylesheet>

Yet the transformed result is always UTF-16 (and the encoding attribute says UTF-16).

<?xml version="1.0" encoding="UTF-16"?>

Is this a bug in MSXML?

For various reasons, I'd really like to have UTF-8. Is there a workaround? Or do I have to convert the transformed result to UTF-8 myself and patch up the encoding attribute?

Update: I've worked around the problem by accepting the UTF-16 encoding and prepending a byte-order mark, which satisfies the downstream users of the transformed result, but I'm still be interested in how to get UTF-8 output.

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

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

发布评论

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

评论(3

梅倚清风 2024-10-28 12:34:40

您可能将输出发送到 DOM 树或字符流,而不是字节流。如果是这种情况,那么就不是 MSXML 进行编码,并且最终编码所做的任何事情都不了解 xsl:output 指令(或者实际上是 XSLT)。

You're probably sending the ouput to a DOM tree or to a character stream, not to a byte stream. If that's the case then it's not MSXML that's doing the encoding, and whatever does do the final encoding has no knowledge of the xsl:output directive (or indeed, of XSLT).

萌梦深 2024-10-28 12:34:40

补充 Michael Kay 所说的内容(当然,这是正确的),这里有一个 JScript 示例,说明如何在过程中使用 XSLT 序列化转换为流:

// command line args
var args = WScript.Arguments;
if (args.length != 3) {
    WScript.Echo("usage: cscript msxsl.js in.xml ss.xsl out.xml");
    WScript.Quit();
}
xmlFile = args(0);
xslFile = args(1);
resFile = args(2);

// DOM objects
var xsl = new ActiveXObject("MSXML2.DOMDOCUMENT.6.0");
var xml = xsl.cloneNode(false);

// source document
xml.validateOnParse = false;
xml.async = false;
xml.load(xmlFile);
if (xml.parseError.errorCode != 0)
    WScript.Echo ("XML Parse Error : " + xml.parseError.reason);

// stylesheet document
xsl.validateOnParse = false;
xsl.async = false;
xsl.resolveExternals = true;
//xsl.setProperty("AllowDocumentFunction", true);
//xsl.setProperty("ProhibitDTD", false);
//xsl.setProperty("AllowXsltScript", true);
xsl.load(xslFile);
if (xsl.parseError.errorCode != 0)
    WScript.Echo ("XSL Parse Error : " + xsl.parseError.reason);

// output object, a stream
var stream = WScript.createObject("ADODB.Stream");
stream.open();
stream.type = 1;
xml.transformNodeToObject( xsl, stream );
stream.saveToFile( resFile );
stream.close();

您可以使用此输入进行测试:

<Urmel>
    <eins>Käse</eins>
    <deux>café</deux>
    <tre>supplì</tre>
</Urmel>

还有此样式表:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output encoding="UTF-8"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

我认为它会您可以轻松地将 JScript 示例改编为 C++。

Supplementing what Michael Kay said (which is spot on, of course), here's a JScript example how to transform to a stream, using the XSLT serialization in the process:

// command line args
var args = WScript.Arguments;
if (args.length != 3) {
    WScript.Echo("usage: cscript msxsl.js in.xml ss.xsl out.xml");
    WScript.Quit();
}
xmlFile = args(0);
xslFile = args(1);
resFile = args(2);

// DOM objects
var xsl = new ActiveXObject("MSXML2.DOMDOCUMENT.6.0");
var xml = xsl.cloneNode(false);

// source document
xml.validateOnParse = false;
xml.async = false;
xml.load(xmlFile);
if (xml.parseError.errorCode != 0)
    WScript.Echo ("XML Parse Error : " + xml.parseError.reason);

// stylesheet document
xsl.validateOnParse = false;
xsl.async = false;
xsl.resolveExternals = true;
//xsl.setProperty("AllowDocumentFunction", true);
//xsl.setProperty("ProhibitDTD", false);
//xsl.setProperty("AllowXsltScript", true);
xsl.load(xslFile);
if (xsl.parseError.errorCode != 0)
    WScript.Echo ("XSL Parse Error : " + xsl.parseError.reason);

// output object, a stream
var stream = WScript.createObject("ADODB.Stream");
stream.open();
stream.type = 1;
xml.transformNodeToObject( xsl, stream );
stream.saveToFile( resFile );
stream.close();

You may test using this input:

<Urmel>
    <eins>Käse</eins>
    <deux>café</deux>
    <tre>supplì</tre>
</Urmel>

And this stylesheet:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output encoding="UTF-8"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

I think it'll be easy for you to adapt the JScript example to C++.

温柔一刀 2024-10-28 12:34:40

正如您所指出的,BSTR 都是 UTF-16。然而,我认为迈克尔·路德维希可能在这里有所发现。您尝试过使用这种方法吗?

HRESULT IXMLDOMDocument::transformNodeToObject(
    IXMLDOMNode *stylesheet,
    VARIANT outputObject);

您应该能够仅使用 CreateStreamOnHGlobal,将生成的 IStream ptr 存储到 VARIANT 中,并将其作为 outputObject 参数传递。理论上来说。不过我还没有真正尝试过:)

As you noted, BSTRs are all UTF-16. However, I think Michael Ludwig might be on to something here. Have you tried using this method?

HRESULT IXMLDOMDocument::transformNodeToObject(
    IXMLDOMNode *stylesheet,
    VARIANT outputObject);

You should be able to just use CreateStreamOnHGlobal, stash the resultant IStream ptr into a VARIANT, and pass that in as the outputObject parameter. Theoretically. I haven't actually tried this, though :)

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