使用xslt通过onclick函数对xml进行排序 [英] using xslt to sort xml via onclick function

查看:91
本文介绍了使用xslt通过onclick函数对xml进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道通过单击关联按钮对xml / xslt表进行排序的最简单方法。我对xslt非常熟悉,但对javascript很新。所以对我来说很容易。

I would like to know the simplest way to sort an xml/xslt table by clicking associated buttons. I'm pretty familiar with xslt but very new to javascript so go easy on me.

我在互联网上看了很多例子,但似乎没有什么真的适合我正在尝试做什么,或者我的编码技巧可能达不到标准。

I've looked at many examples on the internet but it seems like nothing really fits what I'm trying to do or perhaps my coding skills just aren't up to par.

我可能会离开,但我正在考虑的事情。 ..

I might be way off but I was thinking something along the lines of...

xslt:

<button onclick="title()">sort by title</button>
<!--some xsl code-->
<xsl:for each select="record">
<xsl:sort id="title" select="dates/year"/>
<!--more xsl code-->

javascript:

javascript:

function title() {
document.getElementById(title).select="titles/title";
}

我也不清楚javascript代码的放置位置。我已经有一个.js文件显示我的xml& xsl文件为html。我可以把这段代码放在那里吗?或者我的xsl文件需要内联脚本吗?我已经看到很多方法将javascript附加到xsl文件,但我不确定哪种方式最适合我的目的

I'm also not exactly clear on where to put the javascript code. I've already got a .js file that displays my xml & xsl files as an html. Can I put this code there? Or do I need inline script on my xsl file? I've seen many ways of attaching javascript to an xsl file, but I'm not sure which way is best for my purposes

推荐答案

这就是我最终做的并且效果很好!

This is what I ended up doing and it works well!

HTML:xml文档加载有一个div

HTML:there was a div for the xml document load

<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

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文件,它指定了某种排序。每个函数都调用一个不同的样式表。

XML: I then had multiple xsl files where it specified a certain sort. Each function called a different style sheet.

这篇关于使用xslt通过onclick函数对xml进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆