gpt4 book ai didi

delphi - 阻止 XSLT 转换将 utf-8 XML 转换为 utf-16?

转载 作者:行者123 更新时间:2023-12-03 15:12:57 25 4
gpt4 key购买 nike

在 Delphi XE2 中,我对收到的 XML 文件进行 xslt 转换以删除所有命名空间信息。
问题:它改变了

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

进入

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

这是我从 Exchange 服务器返回的 XML:

<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<h:ServerVersionInfo MajorVersion="14" MinorVersion="0" MajorBuildNumber="722" MinorBuildNumber="0" Version="Exchange2010" xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<m:ResolveNamesResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
<m:ResponseMessages>
<m:ResolveNamesResponseMessage ResponseClass="Success">
<m:ResponseCode>NoError</m:ResponseCode>
<m:ResolutionSet TotalItemsInView="1" IncludesLastItemInRange="true">
<t:Resolution>
<t:Mailbox>
<t:Name>developer</t:Name>
<t:EmailAddress>developer@timetellbv.nl</t:EmailAddress>
<t:RoutingType>SMTP</t:RoutingType>
<t:MailboxType>Mailbox</t:MailboxType>
</t:Mailbox>
<t:Contact>
<t:Culture>nl-NL</t:Culture>
<t:DisplayName>developer</t:DisplayName>
<t:GivenName>developer</t:GivenName>
<t:EmailAddresses>
<t:Entry Key="EmailAddress1">SMTP:developer@timetellbv.nl</t:Entry>
</t:EmailAddresses>
<t:ContactSource>ActiveDirectory</t:ContactSource>
</t:Contact>
</t:Resolution>
</m:ResolutionSet>
</m:ResolveNamesResponseMessage>
</m:ResponseMessages>
</m:ResolveNamesResponse>
</s:Body>
</s:Envelope>

这是删除命名空间信息的函数:

Uses
MSXML2_TLB; // IXMLDOMdocument

class function TXMLHelper.RemoveNameSpaces(XMLString: String): String;
const
// An XSLT script for removing the namespaces from any document.
// From http://wiki.tei-c.org/index.php/Remove-Namespaces.xsl
cRemoveNSTransform =
'<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">' +
'<xsl:output method="xml" indent="no"/>' +

'<xsl:template match="/|comment()|processing-instruction()">' +
' <xsl:copy>' +
' <xsl:apply-templates/>' +
' </xsl:copy>' +
'</xsl:template>' +

'<xsl:template match="*">' +
' <xsl:element name="{local-name()}">' +
' <xsl:apply-templates select="@*|node()"/>' +
' </xsl:element>' +
'</xsl:template>' +

'<xsl:template match="@*">' +
' <xsl:attribute name="{local-name()}">' +
' <xsl:value-of select="."/>' +
' </xsl:attribute>' +
'</xsl:template>' +

'</xsl:stylesheet>';

var
Doc, XSL: IXMLDOMdocument2;
begin
Doc := ComsDOMDocument.Create;
Doc.ASync := false;
XSL := ComsDOMDocument.Create;
XSL.ASync := false;
try
Doc.loadXML(XMLString);
XSL.loadXML(cRemoveNSTransform);
Result := Doc.TransFormNode(XSL);
except
on E:Exception do Result := E.Message;
end;
end; { RemoveNameSpaces }

但在此之后,它突然变成了一个 utf-16 文档:

<?xml version="1.0" encoding="UTF-16"?>
<Envelope>
[snip]
</Envelope>

在谷歌搜索“xsl utf-8 utf-16”后,我尝试了几件事:

  • 更改行(例如 Output DataTable XML in UTF8 rather than UTF16 )

    <xsl:output method="xml" indent="no">

    进入其中之一:

    <xsl:output method="xml" encoding="utf-8" indent="no"/>
    <xsl:output method="xml" encoding="utf-8"/>
    <xsl:output encoding="utf-8"/>

    这不起作用。
    (根据http://www.xml.com/pub/a/2002/09/04/xslt.html,这将是最佳解决方案,“编码属性实际上不仅仅是向结果文档添加编码声明;它告诉 XSLT 处理器使用该编码写出结果。”)

  • 更改行(例如 XslCompiledTransform uses UTF-16 encoding )

    <xsl:output method="xml" indent="no"/>

    进入

    <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />

    省略了起始 xml 标记,但如果我只是在前面添加

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

    我会丢失字符,因为没有完成实际的 utf 转换。

  • IXMLDOMdocument2 没有 Encoding 属性

有什么想法可以解决这个问题吗?

备注/背景:

  • 如果所有其他方法都失败,也许仍然可以选择将 utf-16 XML 数据更改为 utf-8,但这是一种完全不同的方法。

  • 我正在尝试使用 utf-8 执行所有操作,因为我通过 EWS 与 Exchange 服务器进行通信,并且将 http 请求 header 设置为 utf-16 不起作用:Exchange 告诉我内容类型 '文本/xml; charset = utf-16' 不是预期的类型 'text/xml;字符集 = utf-8'。 EWS 返回 utf-8(请参阅帖子开头)。

最佳答案

问题是使用 transformNode 方法,它返回一个字符串,而对于 MSXML,这样的字符串是 UTF-16 编码的。因此,您需要为结果创建一个空的 MSXML DOM 文档并使用 the transformNodeToObject method ,传递空 DOM 文档作为第二个参数,然后您可以将结果文档保存到文件或流中,并且编码应按照 xsl:output 指令中指定。

关于delphi - 阻止 XSLT 转换将 utf-8 XML 转换为 utf-16?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16078480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com