2012-09-24 75 views
2

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

我已经看过很多互联网上的例子,但它似乎没有什么真正适合我想要做的或者我的编码技能只是没有达到标准。

我可能的路要走,但我想沿着线的东西...

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:

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

我也并不很清楚关于在哪里放置JavaScript代码。我已经有了一个.js文件,它将我的xml & xsl文件显示为html。我可以把这个代码放在那里吗?或者我需要在我的xsl文件上使用内联脚本?我见过很多将javascript附加到xsl文件的方式,但我不确定哪种方式最适合我的用途

+0

您的问题在目前状态下不能真正答复,除非我们更了解您正在做的事情。您是在服务器上还是在浏览器上运行XSLT?你在使用哪种XSLT处理器? –

+0

我的文件存储在服务器上。我不确定我使用的是哪种xslt处理器。 – daniella

+0

我不相信我正在使用一个。我是不是该? – daniella

回答

0

这是我落得这样做,它工作得很好!

HTML:有XML文档负载

<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一个div:然后我就在那里所规定的一定排序多XSL文件。每个函数都称为不同的样式表。

1

如果您对XSLT感到满意,但无法使用Javascript,那么您可能想看看Saxon -CE,它在浏览器中提供XSLT 2.0,并带有扩展来处理用户交互事件。还有它演示了如何表响应排序一个简单的例子鼠标点击这里:

http://www.saxonica.com/ce/doc/samples/booklist.xml

+0

是的,我认为这很有希望。但是,在代码中出现“ixsl:...”错误。它表示它尚未映射到任何URI – daniella

+0

ixsl前缀应该映射到命名空间'xmlns:ixsl =“http://saxonica.com/ns/interactiveXSLT”'。你正在运行我们的示例应用程序(链接)还是你自己的?随意使用dev.saxonica.com上的Saxon论坛讨论任何问题。 –

+0

这是我自己的解决方案。当我可以使用我自己的脚本时,我觉得萨克斯尼卡复杂的东西。 – daniella

相关问题