包含中文字符的URL编码问题

在xml应用中,经常将一些URL信息作为xml数据存储,其中URL参数有可能包含有中文字符。当使用dom对xml数据进行解析时,可以对中文字符进行编码。
但如果只使用xslt来显示xml数据时(data.xml+data.xsl),发现此时的URL会出现编码错误.即使指定编码类型(encoding="gb2312"),依然会出现同样的问题.
测试发现:是IE的缓存机制问题,IE仍会把新的页面(所链接的URL)的MIME内容类型默认为text/xml

解决方法:
1.指定输出文档类型为xml文档 (example:data.xsl)

1<xsl:output encoding='\"gb2312\"' media-type='\"text/xml\"' method='\"xml\"'></xsl:output>

2.在新的窗口打开,给联接增加属性,指明目标窗口为其他窗口 (example:data2.xsl)

1<xsl:attribute name='\"target\"'>_blank</xsl:attribute>

examples:

/*** data.xml ***/

 1<root>
 2<search>
 3<url>http://www.google.com/search?q=</url>
 4<word>xml数据</word>
 5</search>
 6<search>
 7<url>http://www1.baidu.com/baidu?word=</url>
 8<word>xml数据</word>
 9</search>
10<search>
11<url>http://www.google.com/search?q=</url>
12<word>极限编程(xp)</word>
13</search>
14<search>
15<url>http://www1.baidu.com/baidu?word=</url>
16<word>极限编程(xp)</word>
17</search>
18</root>

/*** data.xsl ***/

 1<xsl:stylesheet 1999="" \"="" _http:="" transform_="" version='\"1.0\"' www.w3.org="" xmlns:xsl='\"' xsl="">
 2<!-- 去掉下面一句,将出现错误 -->
 3<xsl:output encoding='\"gb2312\"' media-type='\"text/xml\"' method='\"xml\"'></xsl:output>
 4<xsl:template match='\"/\"'>
 5<xsl:apply-templates></xsl:apply-templates>
 6</xsl:template>
 7<xsl:template match='\"search\"'>
 8<xsl:element name='\"a\"'>
 9<xsl:attribute name='\"href\"'><xsl:value-of select='\"url\"'></xsl:value-of><xsl:value-of select='\"word\"'></xsl:value-of></xsl:attribute>
10<xsl:value-of select='\"word\"'></xsl:value-of>
11</xsl:element>
12<br/>
13</xsl:template>
14</xsl:stylesheet>

/*** data2.xsl ***/

 1<xsl:stylesheet 1999="" \"="" _http:="" transform_="" version='\"1.0\"' www.w3.org="" xmlns:xsl='\"' xsl="">
 2<xsl:template match='\"/\"'>
 3<xsl:apply-templates></xsl:apply-templates>
 4</xsl:template>
 5<xsl:template match='\"search\"'>
 6<xsl:element name='\"a\"'>
 7<xsl:attribute name='\"href\"'><xsl:value-of select='\"url\"'></xsl:value-of><xsl:value-of select='\"word\"'></xsl:value-of></xsl:attribute>
 8<!-- 去掉下面一句,将出现错误 -->
 9<xsl:attribute name='\"target\"'>_blank</xsl:attribute>
10<xsl:value-of select='\"word\"'></xsl:value-of>
11</xsl:element>
12<br/>
13</xsl:template>
14</xsl:stylesheet>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus