Just for practice i am trying to render the same markup in two different places on a page. It works fine in FF opera etc but not in IE8.
Is there a better way to do this? have mercy i just started xslt
Here is the 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.getEle开发者_如何学运维mentById("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);
And here is the markup:
<html>
<head>
<script>
include myscripts
</script>
</head>
<body onload="displayResult()">
<div id="example" />
<div id="eexample" />
</body>
</html>
There is a simpler way :)
Install the XML tool from Microsoft - http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=17280
Then create your XSL - example - on your desktop name 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>
Corresponding - test.xml on the desktop
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<root>foo</root>
Open test.xml in IE and the XSLT transformation should have been done.
精彩评论