XSLT paging XML data and sorting by a specific value

Source: Internet
Author: User
Tags dotnet xsl xslt
The last time we met this requirement, the data in the XML file is converted to HTML through XSLT for paging, and the data needs to be re-ordered based on the user's selection according to a certain field. the following method was initially thought: <? XML version = "1.0" encoding = "iso-8859-1"?>
<XSL: stylesheet version = "1.0" xmlns: XSL = "http://www.w3.org/1999/XSL/Transform"
Xmlns: msxsl = "urn: Schemas-Microsoft-com: XSLT" xmlns: User = "http://dotnet.aspx.cc /"
Exclude-result-prefixes = "msxsl user">
<XSL: output method = "html" omit-XML-declaration = "yes"/>

<XSL: template match = "/">
<XSL: For-each select = "// items/item [(position () & lt; = $ pageno * $ count) and (
Position () & gt; (number ($ pageno)-1) * $ count)] ">
<XSL: sort order = "ASCENDING" data-type = "text" select = "title"/>
</XSL: For-each>
</XSL: Template>

</XSL: stylesheet>

When you need to re-sort by a field, use JavaScript to modify the select attribute value in the XSL: Sort element. but soon I found the problem: this is just to re-sort the data on the current page, and not really re-Sort all the data in XML according to the required fields.
But in the end, I didn't always think of a good solution to solve the problem. I had to use the so-called "data island" (I don't know if the term is correct), that is, I used XSLT to sort the XML before generating XML, in the web page, and then perform paging operations. when you need to re-sort the data, you can sort the data first and then page by page. If you do not need to re-sort the data, you can directly retrieve the data from the current table. I have always felt that this method is very uncomfortable. It takes two XSLT segments to solve this problem.

Finally, in the process of using XSLT, I thought that only one piece of XSLT was used to solve the problem of data re-sorting during paging. The code is roughly as follows: <? XML version = "1.0" encoding = "iso-8859-1"?>
<XSL: stylesheet version = "1.0" xmlns: XSL = "http://www.w3.org/1999/XSL/Transform" xmlns: msxsl = "urn: Schemas-Microsoft-com: XSLT" xmlns: user = "http://dotnet.aspx.cc/" exclude-result-prefixes = "msxsl user">
<XSL: output method = "html" omit-XML-declaration = "yes"/>

<XSL: template match = "/">
<XSL: For-each select = "// items/item">
<XSL: If test = "(position () & lt; = $ pageno * $ count) and (position () & gt; (number ($ pageno)-1) * $ count) ">
<XSL: variable name = "I" select = "position ()"/>
<XSL: For-each select = "// items/item">
<XSL: Sort select = "price" data-type = "Number"/>
<XSL: If test = "(position () = $ I)">

</XSL: If>
</XSL: For-each>
</XSL: If>
</XSL: If>
</XSL: Template>

</XSL: stylesheet>

The efficiency may not be very good. Please give me more advice.
Pageno indicates the current page number; count indicates the number of records on each page. The following is an XML file. <? XML version = "1.0"?>
<Items>
<Item>
<Author> ZK </author>
<Title> yname of TTT </title>
<Price> 1.3 </price>
</Item>
<Item>
<Author> ZK </author>
<Title> aname of TTT </title>
<Price> 3.3 </price>
</Item>
<Item>
<Author> ZK </author>
<Title> xname of TTT </title>
<Price> 2.3 </price>
</Item>
<Item>
<Author> ZK </author>
<Title> cname of TTT </title>
<Price> 10.3 </price>
</Item>
</Items>

The weather is often like the second or the second month.Welcome to our blog.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.