只是为了练习我试图在页面上的两个不同位置呈现相同的标记。它在FF歌剧等中工作正常但在IE8中却没有。
有更好的方法吗?怜悯我刚刚开始xslt
这是js:
function loadXMLDoc(fname)
{
var xmlDoc;
// code for IE
if (window.ActiveXObject)
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load(fname);
return(xmlDoc);
}
// code for Mozilla, Firefox, Opera, etc.
else
{
xhttp=new XMLHttpRequest();
xhttp.open("GET",fname,false);
xhttp.send("");
return xhttp.responseXML;
}
}
function displayResult()
{
xmlcd=loadXMLDoc("cdcatalog.xml");
xslcd=loadXMLDoc("cdcatalog.xsl");
if (window.ActiveXObject)
{
exs=xmlcd.transformNode(xslcd);
document.getElementById("example").innerHTML=exs;
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
exs=xmlcd.transformNode(xslcd);
document.getElementById("eexample").innerHTML=exs;
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation
&& document.implementation.createDocument)
{
xsltProcessor=new XSLTProcessor();
xsltProcessor.importStylesheet(xslcd);
resultDocument = xsltProcessor.transformToFragment(xmlcd,document);
document.getElementById("example").appendChild(resultDocument);
xsltProcessor=new XSLTProcessor();
xsltProcessor.importStylesheet(xslcd);
resultDocument = xsltProcessor.transformToFragment(xmlcd,document);
document.getElementById("eexample").appendChild(resultDocument);
}
}
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(displayResult);
这是标记:
<html>
<head>
<script>
include myscripts
</script>
</head>
<body onload="displayResult()">
<div id="example" />
<div id="eexample" />
</body>
</html>
答案 0 :(得分:0)
有一种更简单的方法:)
从Microsoft安装XML工具 - http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=17280
然后在您的桌面名称test.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:value-of select="/root"/>
</xsl:template>
</xsl:stylesheet>
对应 - 桌面上的test.xml
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<root>foo</root>
在IE中打开test.xml,应该完成XSLT转换。