这就是我最终做的,而且效果很好!
HTML:xml 文档加载有一个 div
<button type="button" id="sorttitle" onclick="sorttitle()">sort by title</button>
<button type="button" id="sortauthor" onclick="sortauthor()">sort by author</button>
<button type="button" id="sortyear" onclick="sortyear()">sort by year</button>
<button type="button" id="sortpublisher" onclick="sortpublisher()">sort by publisher</button>
JAVASCRIPT
function sorttitle(){
document.getElementById("sortauthor").style.backgroundColor="#000";
document.getElementById("sortyear").style.backgroundColor="#000";
document.getElementById("sortpublisher").style.backgroundColor="#000";
document.getElementById("sorttitle").style.backgroundColor="#666";
xsl=loadXMLDoc("sorttitle.xsl");
if (window.ActiveXObject)
{
sortedDocument=xml.transformNode(xsl);
document.getElementById('content').innerHTML=sortedDocument;
}
else {
xsltProcessor=new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
sortedDocument = xsltProcessor.transformToFragment(xml,document);
document.body.replaceChild(sortedDocument,document.getElementById('content'));
}}
function sortauthor(){
document.getElementById("sorttitle").style.backgroundColor="#000";
document.getElementById("sortyear").style.backgroundColor="#000";
document.getElementById("sortpublisher").style.backgroundColor="#000";
document.getElementById("sortauthor").style.backgroundColor="#666";
xsl=loadXMLDoc("sortauthor.xsl");
if (window.ActiveXObject)
{
sortedDocument=xml.transformNode(xsl);
document.getElementById('content').innerHTML=sortedDocument;
}
else {
xsltProcessor=new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
sortedDocument = xsltProcessor.transformToFragment(xml,document);
document.body.replaceChild(sortedDocument,document.getElementById('content'));
}}
function sortyear(){
document.getElementById("sorttitle").style.backgroundColor="#000";
document.getElementById("sortauthor").style.backgroundColor="#000";
document.getElementById("sortpublisher").style.backgroundColor="#000";
document.getElementById("sortyear").style.backgroundColor="#666";
xsl=loadXMLDoc("sortyear.xsl");
if (window.ActiveXObject)
{
sortedDocument=xml.transformNode(xsl);
document.getElementById('content').innerHTML=sortedDocument;
}
else {
xsltProcessor=new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
sortedDocument = xsltProcessor.transformToFragment(xml,document);
document.body.replaceChild(sortedDocument,document.getElementById('content'));
}}
function sortpublisher(){
document.getElementById("sorttitle").style.backgroundColor="#000";
document.getElementById("sortauthor").style.backgroundColor="#000";
document.getElementById("sortyear").style.backgroundColor="#000";
document.getElementById("sortpublisher").style.backgroundColor="#666";
xsl=loadXMLDoc("sortpublisher.xsl");
if (window.ActiveXObject)
{
sortedDocument=xml.transformNode(xsl);
document.getElementById('content').innerHTML=sortedDocument;
}
else {
xsltProcessor=new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
sortedDocument = xsltProcessor.transformToFragment(xml,document);
document.body.replaceChild(sortedDocument,document.getElementById('content'));
}}
XML:然后我有多个 xsl 文件,其中指定了某种类型。每个函数调用一个不同的样式表。