delphij's Chaos

选择chaos这个词是因为~~实在很难找到一个更合适的词来形容这儿了……

06 Apr 2004

Beat MSXML's utf-16 cling

Today I got in a rush to implement a XML+XSL -> HTML translation at server side. I looked at MSDN and found that there is a simple implementation which can fit my requirements, the code is the following (for some reasons I have to use ASP and not .net technologies):

o Query the database and generate an XML DOM object
o Load XSLT template
o Apply it, then, response with the apply HTML.

Unfortunatelly, the text was encoded in utf-16 and therefore appears to be unreadable in my browser.

Because my template and xml services works well previously, I thought that’s something wrong with my XSLT functions. It reads like:

xslt.load(“score.xsl”)
Response.Write g_oXMLResponse.transformNode xslt

With a Google search I found that transformNode will return BSTR and hence only utf-16 will be the output - This behavior is by design!

The doubt then turns out that whether I have to implement a utf-16 to utf-8 converter, as this is not so hard for me. However, before re-implementing wheels, it will be good to RTFM and I then searched the Internet, found that transformNodeToObject will adopt stream’s current encoding setting. I therefore rewrote the code as the follow:

xslt.load(“score.xsl”)
Response.Charset = “utf-8”
g_oXMLResponse.transformNodeToObject xslt, Response

The code works well this time.

As a bonus I found some minor typos in Microsoft XML Core Services documentation - The samples will never run, or will work inproperly because these typos. You are expected to encounter these and they are so trivial so you can correct them yourself when you are actually doing some real programming.