Coding problems and solutions that occur during server-side XSLT

Source: Internet
Author: User
Tags xsl xsl file xslt client
Coding | server | process | solution | Problem

After the XSLT file Writing society, in the real application, I also need to use the transformation engine, carries on the transformation of the file. XSLT transformations, in general, are divided into server-side transformations and client transformations. Server-side transformations, that is, XSLT transformations with asp/jsp code, so that the user is given a well-formed HTML file (XML file in Atom2rss). )。 Client-side conversions, which are sent directly to the user's client XML file, are converted by the client, which requires the client to support XML standards. The Ie6,mozilla of XML standards is currently fully supported, and many users are still using Ie5, even Ie4, or other browsers that do not support XML. So, in many cases, we need to do server-side conversion.

The program code is very simple, the following code from MSDN, I changed from VB Script code to ASP code:

Dim Xslt,xsldoc,xmldoc,xslproc
Set xslt=server.createobject ("Msxml2.xsltemplate")
Set xsldoc=server.createobject ("Msxml2.freethreadeddomdocument")
Set xmldoc=server.createobject ("Msxml2.domdocument")
Set xslproc=server.createobject ("Ixslprocessor")
Xsldoc.async = False
Xsldoc.resolveexternals = False
Xsldoc.load "Sample.xsl"
Set Xslt.stylesheet = Xsldoc
Xmldoc.async = False
Xmldoc.resolveexternals = False
Xmldoc.load "books.xml"
Set Xslproc = Xslt.createprocessor ()
Xslproc.input = xmldoc
Xslproc.addparameter "param1", "Hello"
Xslproc.transform
Response.Write Xslproc.output

This code is simple and easy to read, but when I actually applied it, I found a very painful problem, that is, my XML file encoding is GB2312, my XSLT file encoding is GB2312, and in XSLT the output XML file encoding is GB2312, But the encoding of the generated file is UTF-16. This caused my file coding error, in IE there is no way to read.

I searched this question on the Internet and found the problem quite common and found a more clearly spoken article re: [XSL] Problem with Chinese (Solution).
  
The general meaning of the article is: if you need to generate a document that specifies the encoding, you should not use string in your code. Because string encoding is always Utf-16 on the Win32 platform, you can't expect MSXML to output a GB2312 string. This problem can be avoided if the flow is used to load,transformnodetoobject the way it is handled.

The following procedures are made according to this idea:

Dim xsldoc,xmldoc,xmlfile,xslfile
Xmlfile= "Test.xml"
Xslfile= "Test.xsl"
' The object needed to generate the XML transformation
Set xsldoc = Server. CreateObject ("MSXML2.") Freethreadeddomdocument ")
Set XmlDoc =server. CreateObject ("MSXML2.") DOMDocument ")
' Mount an XSL file
Xsldoc.async=false
Xsldoc.resolveexternals =false
Xsldoc.load server. MapPath (Xslfile)
' Load XML file
Xmldoc.async=false
Xmldoc.resolveexternals =false
Xmldoc.load server. MapPath (xmlfile)
Xmldoc.transformnodetoobject Xsldoc,response
' To convert
' Clear all memory
Set xslt=nothing
Set xsldoc=nothing
Set xmldoc=nothing

So far, the problem has been completely resolved.







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.