今天在做一个xml的小试验时,发现Chrome竟然无法显示经过xsl修饰的文档!!这么高级的浏览器不应该存在这样的情况啊。经过一翻折腾加google后,发现确实是因为Chrome太高级了所以无法显示:因为安全机制,Chrome不允许本地的xml访问本地的xsl。
解决方案:
1. 换IE或firefox。
2. 把文档上载到web服务器,从服务器可正常访问。
3. 命令行启动chrome的时候加上--allow-file-access-from-files
参数。
参考:http://stackoverflow.com/questions/4558160/xsl-not-working-in-google-chrome
测试xml:test.xml
<!--test.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<booklist>
<book classify="computer science">
<ISBN>001</ISBN>
<title>c++ primer</title>
<authorlist>
<author>Stanley B. Lippman</author>
<author>Josee Lajoie</author>
</authorlist>
</book>
<book classify="computer science">
<ISBN>002</ISBN>
<title>Advanced Programming in the UNIX Environment</title>
<authorlist>
<author>W. Richard Stevens</author>
<author>Stephen A. Rago</author>
</authorlist>
</book>
</booklist>
测试xsl: test.xsl
<!--test.xsl-->
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<title>Book Information</title>
</head>
<body>
<center>
<h2>BOOK</h2>
<table border="1" cellspacing="0" cellpadding="0" style="width: 60%;text-align: center">
<tr>
<th>ISBN</th>
<th>Classification</th>
<th>Book Title</th>
<th>Author(s)</th>
</tr>
<xsl:apply-templates/>
</table>
</center>
</body>
</html>
</xsl:template>
<xsl:template match="book">
<tr>
<td>
<xsl:value-of select="./ISBN"/>
</td>
<td>
<xsl:value-of select="@classify"/>
</td>
<td>
<xsl:value-of select="./title"/>
</td>
<td>
<xsl:value-of select=".//author[position()=1]"/>
<xsl:value-of select=".//author[position()>1]"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>