delphi - Prevent XSLT transform from converting utf-8 XML into utf-16? -


in delphi xe2, i'm doing xslt transform on received xml file remove namespace information.
problem: changes

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

into

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

this xml 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 function removes namespace info:

uses    msxml2_tlb; // ixmldomdocument  class function txmlhelper.removenamespaces(xmlstring: string): string; const   // xslt script removing namespaces document.   // 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 result := e.message;   end; end; { removenamespaces } 

but after this, it's utf-16 document:

<?xml version="1.0" encoding="utf-16"?> <envelope> [snip] </envelope> 

after googling "xsl utf-8 utf-16" tried several things:

  • change line (e.g. output datatable xml in utf8 rather utf16)

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

    into either:

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

    that did not work.
    (it optimal solution, according http://www.xml.com/pub/a/2002/09/04/xslt.html "the encoding attribute more add encoding declaration result document; tells xslt processor write out result using encoding.")

  • change line (e.g. xslcompiledtransform uses utf-16 encoding)

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

    into

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

    which leaves out starting xml tag, if prepend

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

    i lose characters because no actual utf conversion done.

  • ixmldomdocument2 not have encoding property

any ideas how fix this?

remarks/background:

  • if else fails there's maybe still option change utf-16 xml data utf-8, that's entirely different approach.

  • i'm trying utf-8 because i'm communicating exchange server through ews, , setting http request header utf-16 not work: exchange tells me content-type 'text/xml; charset = utf-16' not expected type 'text/xml; charset = utf-8'. ews returns utf-8 (see start of post).

to use ixmldocument in original code, should this:

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 

now variable utf8 should contain transformed xml in utf-8 encoding, if want save stream/file, replace savetoxml by

  iotp.encoding := 'utf-8';   iotp.savetofile(....); 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -