Recently I used XSLT to convert XML data when talking with apple peel about optimizing Weather For Google Earth. The conversion engine must be used here, the general process is to reload the XML and XSLT files to the memory and use the DOM engine to convert them to the HTML we want (I want to generate a KML file in this instance ). The conversion process is divided into the client and the server, because the client conversion requires the user's browser to fully support XML, but not all users' browsers currently support (IE5, IE4, etc.), so it is ideal for server-side conversion.
XML file format:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Weather ver = "2.0">
<Head> [...]
</Head>
<Loc id = "CHXX0101"> [...]
</Loc>
<Cc> [...]
</Cc>
<Dayf>
<Lsup> 10/28/06 AM Local Time </lsup>
<Day d = "0" t = "Saturday" dt = "Oct 28"> [...]
</Day>
<Day d = "1" t = "Sunday" dt = "Oct 29"> [...]
</Day>
</Dayf>
</Weather>
XSLT file format (content omitted ):
<? Xml version = "1.0" encoding = "UTF-8"?>
<Xsl: stylesheet version = "1.0" xmlns: xsl = "http://www.w3.org/1999/XSL/Transform">
<Xsl: output method = "xml" version = "1.0" encoding = "UTF-8" indent = "yes"/>
<Xsl: template match = "/"> [...]
</Xsl: stylesheet>
The conversion code I started using ASP + export cirpt:
// ========= Output type and stream encoding ==================================== =
Response. ContentType = "application/vnd. google-earth.kml + xml ";
Response. CharSet = "UTF-8 ";
// =======Obtain and load the remote XML file ================================
Var oXHy = Server. CreateObject ("MSXML2.XMLHTTP ");
Var url = http://www.dnxh.cn/ge/CHXX0101.xml;
OXHy. open ("GET", url, false );
OXHy. send ();
Var oXD = Server. CreateObject ("MSXML2.DOMDocument ");
OXD. loadXML (oXHy. responseText );
// ======= Load the XSL file ============================
Var xsl = Server. CreateObject ("Microsoft. XMLDOM ");
Xsl. async = false;
Xsl. load (Server. MapPath ("gew. xsl "));
// ======= File conversion ==========================
Response. Write (oXD. transformNode (xsl ));
It is reasonable to say that there should be no encoding problem, because the declaration of the encoding is declared. But something went wrong. The statement at the beginning of the output KML file is always
<? Xml version = "1.0" encoding = "UTF-16"?>
Test shows that there are no problems with the XML and XSLT source files, that is, the Problem lies in the conversion engine in ASP code, and then in RE: [xsl] Problem with Chinese (Solution) this article probably found the reason, here it is said that the engine transformNode is to generate a string, and on the win32 platform is always to process the string with a UTF-16, then we use this string to generate the KML file, then the result can only be UTF-16.
The solution is to use the transformNodeToObject engine. Replace the file conversion part with oXD. transformNodeToObject (xsl, Response ). The difference between the two methods is that the first one generates a string variable, and the other is to directly Save the converted XML data to the specified node.