This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Problem with Chinese (Solution)


Hi,

Thanks to all who responded.  I have now fixed the problem and I will now
show how for others who may experience similar problems.

I was connecting to the web server using XMLHTTP, using the following
code:

    // Format post URL
    //
    var postUrl = webRoot + "/" + "xgetnihao.asp";

    // Create request xml document from passed xml
    //
    xmlRequestDoc = new ActiveXObject("MSXML2.DOMDocument");
    xmlRequestDoc.async = 0;
    xmlRequestDoc.loadXML(xml);

    // Send xml object to the post URL
    //
    xmlPostObject = new ActiveXObject("Microsoft.XMLHTTP");
    xmlPostObject.Open("POST", postUrl , false);
    xmlPostObject.send(xmlRequestDoc);

    // Get response object
    //
    var xmlResponseDoc = new ActiveXObject("MSXML2.DOMDocument");
    xmlResponseDoc.async = 0;
    xmlResponseDoc.loadXML(xmlPostObject.responseText);

    // Create stylesheet object
    //
    var xslFile = "nihao.xsl";
    var xslDoc = new ActiveXObject("MSXML2.DOMDocument");
    xslDoc.async = 0;
    xslDoc.load(Server.MapPath(xslFile));

    // Apply stylesheet to xml
    //
    var outputHtml = xmlResponseDoc.transformNode(xslDoc);

    // Output result
    //
    Response.Write(outputHtml);


This works great for standard encodings, but it will never work for
encodings like Chinese (GB2312).

The offending line which breaks it is:

       xmlResponseDoc.loadXML(xmlPostObject.responseText);

The responseText attibute will always give some other encoding and
corrupt the data.  So you should use:

       xmlResponseDoc.load(xmlPostObject.responseXML);

Note that I have changed 2 things here.  First to responseXML,
second the loadXML to just load.

Ok, so you do all that and it STILL doesn't work !!  So, next you
need to go to the ASP page which is generating your beautiful XML
and insert the following line of code:

       Response.contentType = "text/xml";

Of course, all of this is useless unless the ASP page generating
your XML does not start with:

       <?xml version="1.0" encoding="GB2312" ?>

Ok, so just to complete the full Chinese example you can use
this ASP page to test some of this code (called "xgetnihao.asp")

  <%@ Language="JavaScript" %>
  <%
  Response.contentType = "text/xml";
  var xml = "<?xml version=\"1.0\" encoding=\"GB2312\" ?>";
  xml += "<TestChinese>";
  xml += "你好";
  xml += "</TestChinese>";
  Response.Write(xml);
  %>


An xsl stylesheet follows (called  "nihao.xsl"):

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output encoding="GB2312"/>
    <xsl:template match="/">
      <html>
      <body>
      <br/>
      <xsl:value-of select="/TestChinese" />
      <br/>
      </body>
      </html>
    </xsl:template>
</xsl:stylesheet>


You can test the above code it should work.

However !!!  I did notice one interesting undesirable "feature" in the
MSXML.  If you put in some <HEAD></HEAD> tags into the
above XSL, then your output HTML contains the following by
magic.

  <head>
  <META http-equiv="Content-Type" content="text/html; charset=UTF-16">
  </head>

Maybe this is what MSXML thinks the closest thing to GB2312 is.
The bad thing is that the IE5.5 browser doesn't know how to Auto-Select
the GB2312 encoding when this is present.  This might be considered a
bug.  If you do not include HEAD tags, no content-type meta appears
and the GB2312 encoding is correctly auto-detected.

The above solution is partly in thanks to the following articles which
I merged and fiddled with to get the answer !

http://lists.xml.org/archives/xml-dev/200101/msg00153.html
http://p2p.wrox.com/archive/xml/2001-06/5.asp

Hope this helps somebody, now I can get back to work !

ps.  I can confirm, the localized version of MSXML3.0 for Chinese
just shows all the error messages in Chinese, you don't need it to
make the code work.

pps.  I'm still in Side-by-side mode !!

Shaun Bliss (sb@cris.com)
Techical Architect, Paisley Crawford Ltd., Australia. (web site pending)
http://www.blissweb.com/   (personal)















 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]