相关疑难解决方法(0)

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

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

<?xml version="1.0" encoding="utf-8"?>
Run Code Online (Sandbox Code Playgroud)

<?xml version="1.0" encoding="utf-16"?>
Run Code Online (Sandbox Code Playgroud)

这是我从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>
Run Code Online (Sandbox Code Playgroud)

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

Uses
   MSXML2_TLB; // IXMLDOMdocument

class function TXMLHelper.RemoveNameSpaces(XMLString: String): String;
const
  // An XSLT script …
Run Code Online (Sandbox Code Playgroud)

delphi xslt utf-8 utf-16 delphi-xe2

5
推荐指数
1
解决办法
4321
查看次数

与Encoding和XslCompiledTransform混淆

我搞乱了编码.

一方面我有一个在UTF-8中响应我的网址(我非常肯定感谢firebug插件).

我使用以下代码打开以UTF-8读取内容的URL:

StreamReader reader = new StreamReader(response.GetResponseStream(),System.Text.Encoding.UTF8);
Run Code Online (Sandbox Code Playgroud)

另一方面,我有一个转换xslt表与以下代码:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
            <br/>
            hello
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

此xslt表也以UTF-8格式保存.

我使用以下代码将xml与xslt混合:

StringWriter writer = new StringWriter();
XslCompiledTransform transformer = new XslCompiledTransform();

transformer.Load(HttpContext.Current.Server.MapPath("xslt\\xsltsheet.xslt");  
XmlWriterSettings xmlsettings = new XmlWriterSettings();
xmlsettings.Encoding = System.Text.Encoding.UTF8;
transformer.Transform(xmlreader, null, writer);   

return writer;
Run Code Online (Sandbox Code Playgroud)

然后毕竟我在webbrowser中呈现此作者的内容,我得到以下错误:

无法显示XML页面无法使用样式表查看XML输入.请更正错误,然后单击"刷新"按钮,或稍后重试.

从当前编码切换到不支持的指定编码.处理资源' http:// localhost:2541/Results ....时出错

<?xml version="1.0" encoding="utf-16"?>
Run Code Online (Sandbox Code Playgroud)

我想知道在哪里找到UTF-16编码需要计算:

  • 我的所有文件都保存为UTF-8
  • 服务器的响应是UTF-8
  • 读取xslt表的方式配置为UTF-8.

任何帮助,将不胜感激.

提前致谢.

最好的祝福.

何塞.

xml xslt httpwebrequest

4
推荐指数
1
解决办法
3885
查看次数

标签 统计

xslt ×2

delphi ×1

delphi-xe2 ×1

httpwebrequest ×1

utf-16 ×1

utf-8 ×1

xml ×1