Prevent XSLT conversion from utf-8 XML to utf-16 conversion?

In Delphi XE2, I do the xslt conversion in the resulting XML file to remove all namespace information.
Problem: he is changing

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

at

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

This is the XML that I am returning from the Exchange server:

<?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>

This is a function that removes namespace information:

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 }

But after that, utf-16 document appeared unexpectedly:

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

After Googling "xsl utf-8 utf-16" I tried a few things:

, ?

/ :

  • , , utf-16 XML utf-8, .

  • utf-8, Exchange EWS, HTTP- utf-16 : Exchange , ' /XML; charset = utf-16 ' text/xml; charset = utf-8 '. EWS utf-8 (. ).

+5
2

IXMLDocument , :

var
  iInp, iOtp, iXsl: IXMLDocument;
  Utf8: UTF8String;
begin
  iInp := LoadXMLData(XMLString);
  iXsl := LoadXMLData(cRemoveNSTransfrom);
  iOtp := NewXMLDocument;
  iInp.Node.TransformNode(iXsl.Node,iOtp);
  iOtp.SaveToXML(Utf8);
end

Utf8 XML UTF-8. /, SaveToXML

  iOtp.Encoding := 'UTF-8';
  iOtp.SaveToFile(....);
+1

transformNode, , MSXML UTF-16. DOM MSXML transformNodeToObject, DOM , , , xsl:output.

+2

All Articles