SoFunction
Updated on 2025-04-08

URL encoding problem containing Chinese characters

In xml applications, some URL information is often stored as xml data, where URL parameters may contain Chinese characters. When using dom to parse XML data, Chinese characters can be encoded.
However, if you only use xslt to display xml data (+), you will find that the URL at this time will have an encoding error. Even if you specify the encoding type (encoding="gb2312"), the same problem will still occur.
Test found that it is a problem with IE's cache mechanism. IE will still default to text/xml for the MIME content type of the new page (the linked URL).

Solution:
1. Specify the output document type as xml document (example:)
 <xsl:output method="xml"  encoding="gb2312" media-type="text/xml" />
2. Open in a new window, add attributes to the connection, and indicate that the target window is another window (example:)
 <xsl:attribute name="target">_blank</xsl:attribute>


examples:


/*** ***/

<?xml version="1.0" encoding="gb2312"?>
<?xml-stylesheet type="text/xsl" href=""?>
<root>
 <search>
  <url>/search?q=</url>
<word>xml data</word>
 </search>
 <search>
  <url>/baidu?word=</url>
<word>xml data</word>
 </search>
 <search>
  <url>/search?q=</url>
<word>Extreme Programming (xp)</word>
 </search>
 <search>
  <url>/baidu?word=</url>
<word>Extreme Programming (xp)</word>
 </search>
</root>


/*** ***/

<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http:///1999/XSL/Transform">
<!-- Remove the following sentence and an error will appear -->
<xsl:output method="xml"  encoding="gb2312" media-type="text/xml" />

<xsl:template match="/">
 <xsl:apply-templates /> 
</xsl:template>

<xsl:template match="search">
 <xsl:element name="a">
  <xsl:attribute name="href"><xsl:value-of select="url" /><xsl:value-of select="word" /></xsl:attribute>
  <xsl:value-of select="word" />
 </xsl:element>
 <br />
</xsl:template>

</xsl:stylesheet>


/*** ***/

<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http:///1999/XSL/Transform">

<xsl:template match="/">
 <xsl:apply-templates /> 
</xsl:template>

<xsl:template match="search">
 <xsl:element name="a">
  <xsl:attribute name="href"><xsl:value-of select="url" /><xsl:value-of select="word" /></xsl:attribute>
<!-- Remove the following sentence and an error will appear -->
  <xsl:attribute name="target">_blank</xsl:attribute>
  <xsl:value-of select="word" />
 </xsl:element>
 <br />
</xsl:template>

</xsl:stylesheet>