下边是xslt文件
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:template match="root">
<html><title>ss</title>
<body>
<xsl:value-of select="message" />
</body>
</html>
</xsl:template>
</xsl:stylesheet>
下边是XML文件
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="1.xslt"?>
<root>
<message>这里的</message>
</root>
下边是通过Ajax调用的函数
function getHtml(xmlText, xsltFile){
var text;
if(typeof(window.ActiveXObject) != 'undefined'){
// 支持IE浏览器
try{
var xmlDoc=new ActiveXObject("Msxml2.DOMDocument.3.0");
xslDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
xmlDoc.async=false;
xslDoc.async = false;
xmlDoc.loadXML(xmlText);
xslDoc.load(xsltFile);
text = xmlDoc.documentElement.transformNode(xslDoc.documentElement);
}catch(e){
if (isDebug) alert(e.name + ": " + e.message);
alert("Unable to do xml/xsl processing");
alert(e.name + ": " + e.message);
}
}else if(document.implementation && document.implementation.createDocument){
// 支持Mozilla浏览器
try {
// 在Firefox的XML DOM实现中,并没有loadXML()方法,不过通过Firefox中的DOMParser类可以模拟loadXML()的行为
var oParser = new DOMParser();
var xmlDoc = oParser.parseFromString(xmlText,"text/xml");
xslDoc = document.implementation.createDocument("", "", null);
xslDoc.async = false;
xslDoc.load(xsltFile);
// 定义XSLTProcessor对象
var xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xslDoc);
// transformToDocument方式
var result = xsltProcessor.transformToDocument(xmlDoc);
var xmls = new XMLSerializer();
text = xmls.serializeToString(result);
}
catch(e) {
if (isDebug) alert(e.name + ": " + e.message);
alert("Unable to do xml/xsl processing");
}
}
return text;
}
posted on 2007-06-07 16:15
SIMONE 阅读(1145)
评论(1) 编辑 收藏 所属分类:
JAVA 、
JSP 、
JavaScript