DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2009
    Posts
    2

    Filter,sort xml before binding to repeater

    How I can filter, sort xml before binding it to a repeater? I have a xml data which I need to filter on the basis of querystring and then sort it before binding it into repeater.

    I know how to bind xml with repeater and its working fine too but I am facing problem in filtering based on query string and sorting.

    Any help would really be appreciated?

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    You can use an XPath query to select only the elements you want from the XML document. I can give you a generic example, but if you provide an example of your XML and how you'd like to filter it, we can give you a more specific example. Also, what programming language are you using?
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Dec 2009
    Posts
    2
    My xml is like this: I m using C#
    Code:
    <Categories>
      <Category>
        <Title>Food<Title>
        <Date>12/1/2009</Date>
        <Duration>12/1/2009-12/1/2011</Duration>
        <Description>Who is hungry</Description>
      </Category>
    </Categories>
    I want to sort on the basis of Date and duration and filter if querystring matches with title
    Last edited by PS123; 12-22-2009 at 05:43 PM. Reason: Reformatted XML

  4. #4
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Try something like this:
    Code:
    using System;
    using System.Xml;
    using System.Xml.Xsl;
    
    // Use XSL to sort input file by date and duration            
    XslCompiledTransform xsl = new XslCompiledTransform();
    xsl.Load("sort_categories.xslt");
    
    XmlDocument doc = new XmlDocument();
    using (XmlWriter writer = XmlWriter.Create(doc.CreateNavigator().AppendChild()))
    {
        xsl.Transform("categories.xml", writer); 
    }
    
    // Use XPath to select item(s) whose title matches
    // query string
    string title = Request.QueryString["title"].ToString();
    XmlNodeList nodes = doc.SelectNodes(
        String.Format("/Categories/Category[Title='{0}']", title));
    The variable nodes will then contain the sorted, filtered XML. The stylesheet (sort_categories.xslt) looks like this:
    Code:
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" indent="yes"/>
      <xsl:template match="/">
    
        <Categories>
          <xsl:for-each select="Categories/Category">
            <xsl:sort select="Date" />
            <xsl:sort select="Duration" />
            <Category>
              <Title>
                <xsl:value-of select="Title"/>
              </Title>
              <Date>
                <xsl:value-of select="Date"/>
              </Date>
              <Duration>
                <xsl:value-of select="Duration"/>
              </Duration>
              <Description>
                <xsl:value-of select="Description"/>
              </Description>
            </Category>
          </xsl:for-each>
        </Categories>
    
      </xsl:template>
    </xsl:stylesheet>
    If you want the dates to sort correctly, you'll probably need to use substrings to extract the year, month and day as shown here: http://www.xml.com/pub/a/2002/07/03/...rm.html?page=2
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

Similar Threads

  1. Try XML Junction
    By Tim in forum xml.announcements
    Replies: 0
    Last Post: 10-11-2001, 04:00 PM
  2. XML Startkabel Updates
    By xmlstartkabel in forum XML
    Replies: 0
    Last Post: 10-10-2001, 04:07 AM
  3. Data Junction Announces XML Junction 7.51
    By Tim Frost in forum xml.announcements
    Replies: 0
    Last Post: 04-02-2001, 10:53 AM
  4. Check out Turbo XML: Free trial download
    By Extensibility in forum web.announcements
    Replies: 0
    Last Post: 07-20-2000, 06:20 PM
  5. Check out Turbo XML
    By Sales in forum xml.announcements
    Replies: 0
    Last Post: 07-20-2000, 06:18 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links