包含中文字符的URL編碼問題
在xml應(yīng)用中,經(jīng)常將一些URL信息作為xml數(shù)據(jù)存儲,其中URL參數(shù)有可能包含有中文字符。當(dāng)使用dom對xml數(shù)據(jù)進(jìn)行解析時(shí),可以對中文字符進(jìn)行編碼。
但如果只使用xslt來顯示xml數(shù)據(jù)時(shí)(data.xml+data.xsl),發(fā)現(xiàn)此時(shí)的URL會出現(xiàn)編碼錯(cuò)誤.即使指定編碼類型(encoding="gb2312"),依然會出現(xiàn)同樣的問題.
測試發(fā)現(xiàn):是IE的緩存機(jī)制問題,IE仍會把新的頁面(所鏈接的URL)的MIME內(nèi)容類型默認(rèn)為text/xml
解決方法:
1.指定輸出文檔類型為xml文檔 (example:data.xsl)
<xsl:output method="xml" encoding="gb2312" media-type="text/xml" />
2.在新的窗口打開,給聯(lián)接增加屬性,指明目標(biāo)窗口為其他窗口 (example:data2.xsl)
<xsl:attribute name="target">_blank</xsl:attribute>
examples:
/*** data.xml ***/
<?xml version="1.0" encoding="gb2312"?>
<?xml-stylesheet type="text/xsl" href="data.xsl"?>
<root>
<search>
<url>http://www.google.com/search?q=</url>
<word>xml數(shù)據(jù)</word>
</search>
<search>
<url>http://www1.baidu.com/baidu?word=</url>
<word>xml數(shù)據(jù)</word>
</search>
<search>
<url>http://www.google.com/search?q=</url>
<word>極限編程(xp)</word>
</search>
<search>
<url>http://www1.baidu.com/baidu?word=</url>
<word>極限編程(xp)</word>
</search>
</root>
/*** data.xsl ***/
<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- 去掉下面一句,將出現(xiàn)錯(cuò)誤 -->
<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>
/*** data2.xsl ***/
<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/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>
<!-- 去掉下面一句,將出現(xiàn)錯(cuò)誤 -->
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:value-of select="word" />
</xsl:element>
<br />
</xsl:template>
</xsl:stylesheet>
相關(guān)文章
XSL簡明教程(4)在服務(wù)器端的實(shí)現(xiàn)
XSL簡明教程(4)在服務(wù)器端的實(shí)現(xiàn)...2006-10-10使用javascript+xml實(shí)現(xiàn)分頁
使用javascript+xml實(shí)現(xiàn)分頁...2006-10-10XMLHTTP下載遠(yuǎn)程數(shù)據(jù)輸出到瀏覽器
XMLHTTP下載遠(yuǎn)程數(shù)據(jù)輸出到瀏覽器...2006-10-10