XML對資料島資料實現動態排序二法(1)

來源:互聯網
上載者:User
1、為<xsl:param>標籤設定參數實現動態排序(推薦)
html>
<body>
<table DATASRC="#catalogs" border=1>
<thead>
<tr>
<td onclick="sort('TITLE','descending');">TITLE</td>
<td>ARTIST</td>
<td onclick="sort('COUNTRY','ascending');">COUNTRY</td>
<td>COMPANY</td>
<td>PRICE</td>
<td>YEAR</td>
</tr>
</thead>
<tbody>
<tr>
<td ><div DATAFLD="TITLE"></div></td>
<td ><div DATAFLD="ARTIST"></div></td>
<td ><div DATAFLD="COUNTRY"></div></td>
<td ><div DATAFLD="COMPANY"></div></td>
<td ><div DATAFLD="PRICE"></div></td>
<td ><div DATAFLD="YEAR"></div></td>
</tr>
</tbody>
</table>

<xml id="catalogs">
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
Bob Dylan
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Burlesque</TITLE>
Dylan
<COUNTRY>UA</COUNTRY>
<COMPANY>umbia</COMPANY>
<PRICE>1.90</PRICE>
<YEAR>1987</YEAR>
</CD>
<CD>
<TITLE>Empire</TITLE>
Bob
<COUNTRY>US</COUNTRY>
<COMPANY>bia</COMPANY>
<PRICE>12.90</PRICE>
<YEAR>1995</YEAR>
</CD>
</CATALOG>
</xml>

<xml id="xstyle">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml"/>
<xsl:param name="sortorder" select="'descending'"/>
<xsl:param name="sortfield" select="'xxx'"/>
<xsl:template match="/">
<CATALOG>
<xsl:for-each select="CATALOG/CD">
<xsl:sort select="*[name()=$sortfield]" order="{$sortorder}" />
<CD>
<TITLE><xsl:value-of select="TITLE"/></TITLE>
<xsl:value-of select="ARTIST"/>
<COUNTRY><xsl:value-of select="COUNTRY"/></COUNTRY>
<COMPANY><xsl:value-of select="COMPANY"/></COMPANY>
<PRICE><xsl:value-of select="PRICE"/></PRICE>
<YEAR><xsl:value-of select="YEAR"/></YEAR>
</CD>
</xsl:for-each>
</CATALOG>
</xsl:template>
</xsl:stylesheet>
</xml>

<script language="vbscript">
function sort(strSortField, strSortOrder)

dim objXSL, objXML, objTemplate, objProcessor, strHTML, strDrinkType

Set objXML = CreateObject("Msxml2.FreeThreadedDOMDocument")
Set objXSL = CreateObject("Msxml2.FreeThreadedDOMDocument")

'Load the XML document
objXML.async = False
objXML.Loadxml catalogs.xml

'Load the XSL document
objXSL.async = False
objXSL.Loadxml xstyle.xml

'Create an instance of our XSL Template object
Set objTemplate = CreateObject("MSXML2.XSLTemplate")

'Create an instance of our stylesheet object using our recently loaded XSLT document
Set objTemplate.stylesheet = objXSL

'Create an instance of our Processor object
Set objProcessor = objTemplate.createProcessor

'Define the input object for our object equal to our recently loaded XML document
objProcessor.input = objXML

'Now, finally we can add any parameters that we require to our Template processor
objProcessor.AddParameter "sortfield", strSortField
objProcessor.AddParameter "sortorder", strSortOrder

'Last but not least we do our transformation
objProcessor.Transform

'Store the results of the output into a string.
strXML = objProcessor.output

'Load up an XML DOM object from the recent XML output
objXML.loadxml strXML

'Select only the "employees" elements from our document object
objXML.selectNodes("//CATALOG")

'Load our Data Island using our new XML object
catalogs.loadxml objXML.xml
end function
</script>

<script language="javascript">
function sort2(xmlObj, xslObj, sortByColName)
{
var xmlData=eval("document.all."+xmlObj).XMLDocument;
var xslData=eval("document.all."+xslObj).XMLDocument;
var nodes=xslData.documentElement.selectSingleNode("xsl:for-each");
nodes.selectSingleNode("@order-by").value=sortByColName;

xmlData.documentElement.transformNodeToObject(xslData.documentElement,xmlData);
}
</script>

</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.