SoFunction
Updated on 2025-03-02

Coding issues in the XSLT process on the server side

Recently, when discussing the issue of optimizing Weather For Google Earth with Apple, I used XSLT to convert XML data. Then, the conversion engine must be used here. The rough process is to reprint both the XML file and the XSLT file into memory and use the DOM engine to convert it to the HTML we want (in this example, I want to generate a KML file). This conversion process is divided into client and server side. Because the conversion of client requires the user's browser to fully support XML, but not all users' browsers now support it (IE5, IE4, etc.), it is ideal to convert server side.
XML file form:

<?xml version="1.0" encoding="UTF-8"?>
<weather ver="2.0">
  <head>[...]
  </head>
  <loc >[...]  
  </loc>  
  <cc>[...]  
  </cc>  
  <dayf>  
    <lsup>10/28/06 11:16 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 form (content part omitted):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http:///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 was using ASP+JavaScipt:

//========== Output type and stream encoding=============================================================================================================================================================================================================================================
       = "application/+xml";
       = "UTF-8" ;
//===== Get and load the remote XML file=================================================================================================================================================================================================================================================
      var oXHy = ("");
      var url  = /ge/;  
      ("GET",url,false);
      ();
      var oXD = ("");
      ();
//====== Loading XSL file=================================================================================================================
      var xsl = ("");
       = false;
      ((""));
//======== File conversion===================================================================================================================
      ((xsl)); 
Logically speaking, there should be no coding problem in this way, because the places where the coding is declared are declared. But something went wrong. The output KML file's beginning statement is always in the
  <?xml version="1.0" encoding="UTF-16"?>
Through testing, we found that there is no problem with the two source files of XML and XSLT. The problem lies in the conversion engine in the ASP code. Later, we found the reason in the article RE:[xsl] Problem with Chinese (Solution). Here it is said that the engine transformNode generates a string, and on the win32 platform, UTF-16 is always used to process strings, and then we use this string to generate KML files, and the result can only be UTF-16.
The solution is to use the transformNodeToObject engine. The file conversion part is changed to ( xsl , Response). The difference between these two methods is that the first one generates a string variable, and the second one directly saves the converted XML data to the specified node.