在xml應(yīng)用中,經(jīng)常將一些URL信息作為xml數(shù)據(jù)存儲(chǔ),其中URL參數(shù)有可能包含有中文字符。
當(dāng)使用dom對(duì)xml數(shù)據(jù)進(jìn)行解析時(shí),可以對(duì)中文字符進(jìn)行編碼。
但如果只使用xslt來(lái)顯示xml數(shù)據(jù)時(shí)(data.xml+data.xsl),發(fā)現(xiàn)此時(shí)的URL會(huì)出現(xiàn)編碼錯(cuò)誤.
即使指定編碼類(lèi)型(encoding=gb2312),依然會(huì)出現(xiàn)同樣的問(wèn)題.
測(cè)試發(fā)現(xiàn):是IE的緩存機(jī)制問(wèn)題,IE仍會(huì)把新的頁(yè)面(所鏈接的URL)的MIME內(nèi)容類(lèi)型默認(rèn)為text/xml
解決方法:
1.指定輸出文檔類(lèi)型為xml文檔? (example:data.xsl)
?<xsl:output method=xml? encoding=gb2312 media-type=text/xml />
2.在新的窗口打開(kāi),給聯(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>