我正在尝试使用龙卷风提供xml文件并使用xsl进行转换。
我正在使用:
javascript / html中的 xmlhttp.open("GET",url,true);
和xmlhttp.send();
self.render("cd_catalog.xml")
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
。
结果是一个非格式化的xml文件。
为什么xsl不能转换xml?我错过了什么?
的Python:
import os
import tornado.httpserver
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render("index.html")
class Test(tornado.web.RequestHandler):
def get(self):
self.render("cd_catalog.xml")
def main():
application = tornado.web.Application([
(r"/", MainHandler),
(r"/Test", Test),
])
http_server = tornado.httpserver.HTTPServer(application)
port = int(os.environ.get("PORT", 5003))
http_server.listen(port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()
JS / HTML:
<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc(url)
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="myDiv"><h2>get list of presidents</h2></div>
<button type="button" onclick="loadXMLDoc('/Test')">OK</button>
</body>
</html>
XML:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="cd_catalog.xsl"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
.
.
</catalog>
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>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:0)
龙卷风并没有应用XSL,因为你没有要求它。相反,通过使用RequestHandler.render()
,您要求Tornado将文件呈现为Tornado模板并将结果发送到浏览器。相反,您应该通过您选择的XSL处理器显式运行该文件,然后使用RequestHandler.write()
将结果发送到浏览器。
答案 1 :(得分:0)
它有效,谢谢。这是一个例子:
import os
import tornado.httpserver
import tornado.ioloop
import tornado.web
import lxml.etree as ET
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render("index.html")
class Test(tornado.web.RequestHandler):
def get(self):
dom = ET.parse("cd_catalog.xml")
xslt = ET.parse("cd_catalog.xsl")
transform = ET.XSLT(xslt)
newdom = transform(dom)
self.write(ET.tostring(newdom, pretty_print=True))
def main():
application = tornado.web.Application([
(r"/", MainHandler),
(r"/Test", Test),
])
http_server = tornado.httpserver.HTTPServer(application)
port = int(os.environ.get("PORT", 5003))
http_server.listen(port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()