multiple sorting using AddSort method of the XPathExpression class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • milecimm
    New Member
    • Oct 2007
    • 24

    multiple sorting using AddSort method of the XPathExpression class

    Hi
    I'm trying to sort a table that contains four columns. I've filled this table with data from an xml file. The idea is to sort the table by clicking on the header of the column. I'm not sure if this is the correct approach. I cannot use xsl and would like to maitain the same code structure but my AddSort method of the XPathExpression class doesnt semm to work. Thanks for your help.

    My xml is
    Code:
    CONTRACTS
    --CONTRACT
    ---SUPPLIER
    ---COMMODITIES
    ----COMMODITY
    -----COMODDITYNAME
    
    
    
    
           Dim letter As String
            If Request.QueryString("letter") = "" Then
                letter = "A"
            ElseIf Request.QueryString("letter") = "All" Then
                letter = ""
            Else
                letter = Request.QueryString("letter")
            End If
    
            Dim SortingOrder As String = Nothing
    
            If Not Page.IsPostBack Then
                SortingOrder = XmlSortOrder.Ascending
                Session("getSortingOrder") = SortingOrder
            End If
    
            If Session("getSortingOrder") = XmlSortOrder.Ascending Then
                SortingOrder = XmlSortOrder.Descending
            End If
    
            Dim order As String = Nothing
            If Request.QueryString("sort") = "" Then
                order = "pf:NAME"
            ElseIf Request.QueryString("sort") = "name" Then
                order = "pf:NAME"
            ElseIf Request.QueryString("sort") = "commodity" Then
                order = "pf:COMMODITIES/pf:COMMODITY/pf:COMMODITYNAME"
            ElseIf Request.QueryString("sort") = "supplier" Then
                order = "pf:SUPPLIER"
            End If
    
            Dim myString As StringBuilder = New StringBuilder(10)
            Dim xdoc As New XPathDocument("local_xml.xml")
            Dim nav As XPathNavigator = xdoc.CreateNavigator()
            Dim expr As XPathExpression
            expr = nav.Compile("/pf:CONTRACTS/pf:CONTRACT[pf:NAME[starts-with(., '" & letter & "')]]")
    
            Dim namespaceManager As XmlNamespaceManager = New XmlNamespaceManager(nav.NameTable)
            namespaceManager.AddNamespace("pf", "http://namespace.ac.uk/")
            expr.AddSort(order, SortingOrder, XmlCaseOrder.None, String.Empty, XmlDataType.Text)
            expr.SetContext(namespaceManager)
    
            Dim nodes As XPathNodeIterator = nav.Select(expr)
    
            If nodes.Count > 0 Then
    
                Dim tr As String = Nothing
                myString.AppendLine("<table width='96%' border='0' cellpadding='0' cellspacing='0' border='0' class='datatable1'>")
                myString.AppendLine("<th width='35%'><a href='?letter=" & letter & "&sort=name'>Name</a></th><th width='35%'><a href='?letter=" & letter & "&sort=commodity'>Commodity</a></th><th width='20%'><a href='?letter=" & letter & "&sort=supplier'>Supplier</a></th>")
    
                While nodes.MoveNext()
    
                    Dim node As XPathNavigator = nodes.Current.SelectSingleNode("pf:NAME", namespaceManager)
                     Dim supplier As XPathNavigator = nodes.Current.SelectSingleNode("pf:SUPPLIER", namespaceManager)
                    Dim commodity As XPathNavigator = nodes.Current.SelectSingleNode("pf:COMMODITIES/pf:COMMODITY/pf:COMMODITYNAME", namespaceManager)
                  
                    Dim sChars As String = " "
                    myString.AppendLine("<tr>")
                    myString.AppendLine("<td>")
                    myString.AppendLine(node.ToString())
                    myString.AppendLine("</td>")
                    myString.AppendLine("<td>")
                    myString.AppendLine(commodity.ToString())
                    myString.AppendLine("</td>")
                    myString.AppendLine("<td>")
                    myString.AppendLine(supplier.ToString())
                    myString.AppendLine("</td>")
                    myString.AppendLine("</tr>")
    
                End While
    
                myString.AppendLine("</table>")
    
                Dim strOutput As String = myString.ToString()
                lblOutput.Text = strOutput
    
            Else
    
                lblOutput.Text = "No results for your search<br/>"
    
            End If
    Last edited by jkmyoung; Mar 24 '10, 06:20 PM. Reason: [code]tags[/code]
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Ideally you should be able to call expr.AddSort multiple times, based on the sorting criteria, but I'm not sure how it works.
    If this doesn't work, you could try changing your sort expression to combine both of the values.

    But first off, how are you changing your querystring code to account for multiple sort criteria? Will you have a value stored in Request.QuerySt ring("sort2"), or something similar?

    Comment

    • milecimm
      New Member
      • Oct 2007
      • 24

      #3
      Hi,
      Thanks for your help. I've modified my code to take out the "multiple" sorting because my real problem is that my code doesn't sort at all. If I use "pf:SUPPLIE R" as my sorting criteria nothing happens, for example. I've done sorting before (see http://bytes.com/topic/xml/answers/8...h-unique-value) but when changing the xpath expression then the sorting doesn't work. Thanks again for your help.

      contracts.aspx. vb
      Code:
       Dim letter As String
              If Request.QueryString("letter") = "" Then
                  letter = "A"
              ElseIf Request.QueryString("letter") = "All" Then
                  letter = ""
              Else
                  letter = Request.QueryString("letter")
              End If
              
              Dim myString As StringBuilder = New StringBuilder(200)
              Dim xdoc As New XPathDocument("local_xml.xml")
              
              Dim nav As XPathNavigator = xdoc.CreateNavigator()
              Dim expr As XPathExpression
              expr = nav.Compile("/pf:CONTRACTS/pf:CONTRACT[pf:NAME[starts-with(., '" & letter & "')]]")
             
              Dim namespaceManager As XmlNamespaceManager = New XmlNamespaceManager(nav.NameTable)
              namespaceManager.AddNamespace("pf", "http://namespace.ac.uk/")
              expr.AddSort("pf:SUPPLIER", XmlSortOrder.Ascending, XmlCaseOrder.None, String.Empty, XmlDataType.Text)
              expr.SetContext(namespaceManager)
      
              Dim nodes As XPathNodeIterator = nav.Select(expr)
      
              If nodes.Count > 0 Then
      
                  myString.AppendLine("<table width='96%' border='0' cellpadding='0' cellspacing='0' border='0' class='datatable1'>")
                  myString.AppendLine("<th width='35%'>Name</th><th width='35%'>Commodity</th><th width='20%'>Supplier</a></th>")
      
                  While nodes.MoveNext()
      
                      Dim node As XPathNavigator = nodes.Current.SelectSingleNode("pf:NAME", namespaceManager)
                      Dim supplier As XPathNavigator = nodes.Current.SelectSingleNode("pf:SUPPLIER", namespaceManager)
                      Dim commodity As XPathNavigator = nodes.Current.SelectSingleNode("pf:COMMODITIES/pf:COMMODITY/pf:COMMODITYNAME", namespaceManager)
      
                      Dim sChars As String = " "
                      myString.AppendLine("<tr>")
                      myString.AppendLine("<td>")
                      myString.AppendLine(node.ToString())
                      myString.AppendLine("</td>")
                      myString.AppendLine("<td>")
                      myString.AppendLine(commodity.ToString())
                      myString.AppendLine("</td>")
                      myString.AppendLine("<td>")
                      myString.AppendLine(supplier.ToString())
                      myString.AppendLine("</td>")
                      myString.AppendLine("</tr>")
      
                  End While
      
                  myString.AppendLine("</table>")
      
                  Dim strOutput As String = myString.ToString()
                  lblOutput.Text = strOutput
      
              Else
      
                  lblOutput.Text = "No results for your search<br/>"
      
              End If
      contracts.aspx
      Code:
      <asp:Label ID="lblOutput" runat="server"></asp:Label>
          
           <a href="contracts.aspx?letter=A">A</a> -
      <a href="contracts.aspx?letter=B">B</a> -
      <a href="contracts.aspx?letter=C">C</a> -
      etc etc
      My xml
      CONTRACTS
      -CONTRACT
      --NAME
      --SUPPLIER
      --COMMODITIES
      ---COMMODITY
      ----COMMODITYNAME

      Comment

      Working...