これは私がやったことであり、うまく機能します!
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>
ジャバスクリプト
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 ファイルがありました。各関数は異なるスタイル シートを呼び出します。