473,320 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
Expand|Select|Wrap|Line Numbers
  1. CONTRACTS
  2. --CONTRACT
  3. ---SUPPLIER
  4. ---COMMODITIES
  5. ----COMMODITY
  6. -----COMODDITYNAME
  7.  
  8.  
  9.  
  10.  
  11.        Dim letter As String
  12.         If Request.QueryString("letter") = "" Then
  13.             letter = "A"
  14.         ElseIf Request.QueryString("letter") = "All" Then
  15.             letter = ""
  16.         Else
  17.             letter = Request.QueryString("letter")
  18.         End If
  19.  
  20.         Dim SortingOrder As String = Nothing
  21.  
  22.         If Not Page.IsPostBack Then
  23.             SortingOrder = XmlSortOrder.Ascending
  24.             Session("getSortingOrder") = SortingOrder
  25.         End If
  26.  
  27.         If Session("getSortingOrder") = XmlSortOrder.Ascending Then
  28.             SortingOrder = XmlSortOrder.Descending
  29.         End If
  30.  
  31.         Dim order As String = Nothing
  32.         If Request.QueryString("sort") = "" Then
  33.             order = "pf:NAME"
  34.         ElseIf Request.QueryString("sort") = "name" Then
  35.             order = "pf:NAME"
  36.         ElseIf Request.QueryString("sort") = "commodity" Then
  37.             order = "pf:COMMODITIES/pf:COMMODITY/pf:COMMODITYNAME"
  38.         ElseIf Request.QueryString("sort") = "supplier" Then
  39.             order = "pf:SUPPLIER"
  40.         End If
  41.  
  42.         Dim myString As StringBuilder = New StringBuilder(10)
  43.         Dim xdoc As New XPathDocument("local_xml.xml")
  44.         Dim nav As XPathNavigator = xdoc.CreateNavigator()
  45.         Dim expr As XPathExpression
  46.         expr = nav.Compile("/pf:CONTRACTS/pf:CONTRACT[pf:NAME[starts-with(., '" & letter & "')]]")
  47.  
  48.         Dim namespaceManager As XmlNamespaceManager = New XmlNamespaceManager(nav.NameTable)
  49.         namespaceManager.AddNamespace("pf", "http://namespace.ac.uk/")
  50.         expr.AddSort(order, SortingOrder, XmlCaseOrder.None, String.Empty, XmlDataType.Text)
  51.         expr.SetContext(namespaceManager)
  52.  
  53.         Dim nodes As XPathNodeIterator = nav.Select(expr)
  54.  
  55.         If nodes.Count > 0 Then
  56.  
  57.             Dim tr As String = Nothing
  58.             myString.AppendLine("<table width='96%' border='0' cellpadding='0' cellspacing='0' border='0' class='datatable1'>")
  59.             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>")
  60.  
  61.             While nodes.MoveNext()
  62.  
  63.                 Dim node As XPathNavigator = nodes.Current.SelectSingleNode("pf:NAME", namespaceManager)
  64.                  Dim supplier As XPathNavigator = nodes.Current.SelectSingleNode("pf:SUPPLIER", namespaceManager)
  65.                 Dim commodity As XPathNavigator = nodes.Current.SelectSingleNode("pf:COMMODITIES/pf:COMMODITY/pf:COMMODITYNAME", namespaceManager)
  66.  
  67.                 Dim sChars As String = " "
  68.                 myString.AppendLine("<tr>")
  69.                 myString.AppendLine("<td>")
  70.                 myString.AppendLine(node.ToString())
  71.                 myString.AppendLine("</td>")
  72.                 myString.AppendLine("<td>")
  73.                 myString.AppendLine(commodity.ToString())
  74.                 myString.AppendLine("</td>")
  75.                 myString.AppendLine("<td>")
  76.                 myString.AppendLine(supplier.ToString())
  77.                 myString.AppendLine("</td>")
  78.                 myString.AppendLine("</tr>")
  79.  
  80.             End While
  81.  
  82.             myString.AppendLine("</table>")
  83.  
  84.             Dim strOutput As String = myString.ToString()
  85.             lblOutput.Text = strOutput
  86.  
  87.         Else
  88.  
  89.             lblOutput.Text = "No results for your search<br/>"
  90.  
  91.         End If
Mar 24 '10 #1
2 4043
jkmyoung
2,057 Expert 2GB
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.QueryString("sort2"), or something similar?
Mar 24 '10 #2
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:SUPPLIER" 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
Expand|Select|Wrap|Line Numbers
  1.  Dim letter As String
  2.         If Request.QueryString("letter") = "" Then
  3.             letter = "A"
  4.         ElseIf Request.QueryString("letter") = "All" Then
  5.             letter = ""
  6.         Else
  7.             letter = Request.QueryString("letter")
  8.         End If
  9.  
  10.         Dim myString As StringBuilder = New StringBuilder(200)
  11.         Dim xdoc As New XPathDocument("local_xml.xml")
  12.  
  13.         Dim nav As XPathNavigator = xdoc.CreateNavigator()
  14.         Dim expr As XPathExpression
  15.         expr = nav.Compile("/pf:CONTRACTS/pf:CONTRACT[pf:NAME[starts-with(., '" & letter & "')]]")
  16.  
  17.         Dim namespaceManager As XmlNamespaceManager = New XmlNamespaceManager(nav.NameTable)
  18.         namespaceManager.AddNamespace("pf", "http://namespace.ac.uk/")
  19.         expr.AddSort("pf:SUPPLIER", XmlSortOrder.Ascending, XmlCaseOrder.None, String.Empty, XmlDataType.Text)
  20.         expr.SetContext(namespaceManager)
  21.  
  22.         Dim nodes As XPathNodeIterator = nav.Select(expr)
  23.  
  24.         If nodes.Count > 0 Then
  25.  
  26.             myString.AppendLine("<table width='96%' border='0' cellpadding='0' cellspacing='0' border='0' class='datatable1'>")
  27.             myString.AppendLine("<th width='35%'>Name</th><th width='35%'>Commodity</th><th width='20%'>Supplier</a></th>")
  28.  
  29.             While nodes.MoveNext()
  30.  
  31.                 Dim node As XPathNavigator = nodes.Current.SelectSingleNode("pf:NAME", namespaceManager)
  32.                 Dim supplier As XPathNavigator = nodes.Current.SelectSingleNode("pf:SUPPLIER", namespaceManager)
  33.                 Dim commodity As XPathNavigator = nodes.Current.SelectSingleNode("pf:COMMODITIES/pf:COMMODITY/pf:COMMODITYNAME", namespaceManager)
  34.  
  35.                 Dim sChars As String = " "
  36.                 myString.AppendLine("<tr>")
  37.                 myString.AppendLine("<td>")
  38.                 myString.AppendLine(node.ToString())
  39.                 myString.AppendLine("</td>")
  40.                 myString.AppendLine("<td>")
  41.                 myString.AppendLine(commodity.ToString())
  42.                 myString.AppendLine("</td>")
  43.                 myString.AppendLine("<td>")
  44.                 myString.AppendLine(supplier.ToString())
  45.                 myString.AppendLine("</td>")
  46.                 myString.AppendLine("</tr>")
  47.  
  48.             End While
  49.  
  50.             myString.AppendLine("</table>")
  51.  
  52.             Dim strOutput As String = myString.ToString()
  53.             lblOutput.Text = strOutput
  54.  
  55.         Else
  56.  
  57.             lblOutput.Text = "No results for your search<br/>"
  58.  
  59.         End If
contracts.aspx
Expand|Select|Wrap|Line Numbers
  1. <asp:Label ID="lblOutput" runat="server"></asp:Label>
  2.  
  3.      <a href="contracts.aspx?letter=A">A</a> -
  4. <a href="contracts.aspx?letter=B">B</a> -
  5. <a href="contracts.aspx?letter=C">C</a> -
  6. etc etc
  7.  
My xml
CONTRACTS
-CONTRACT
--NAME
--SUPPLIER
--COMMODITIES
---COMMODITY
----COMMODITYNAME
Mar 25 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: mono | last post by:
Hello, I have a collection of XML strings and want to find those that match a given XPath expression. I see lots of stuff to support evaluating the same expression against different nodes in...
2
by: Ben Fidge | last post by:
Is it possible to sort the result of an XPath "select" query? I need to return a subset of a large xml file, and XPath is the way to go. I'd like to sort this subset afterwards? This seems...
4
by: moital | last post by:
i am looking for a c# way to sort an xmlDocument notes the input of the function is a xml as string, and a path of the to be sorted nodes and the attribute to sort by as any one implement...
4
by: viditmittal | last post by:
hi, i have a xml file <root> <Type /> <Description /> <Category /> <employee > <id>3</id>
0
by: LockyBoy | last post by:
Hi Have created an xml document with an xmltextwriter with a namespace, so it can be validated against an xsd schema. Later in my application, I want to extract values from the xml and am using...
5
by: Ben Fidge | last post by:
Hi, I've had an ASP project dumped on me written in VBScript. I'm actually a C#/ASP.NET developer and am struggling trying to find a way to sort the result of a XPath query executed using...
1
by: Loretta | last post by:
Given an XML node List with the individual nodes looking like the following, what kind of XPATH statement can I use to sort the nodes by element F8 and inside F8 by element F3? ...
3
by: Goran Djuranovic | last post by:
Hi All, Does anyone know how to retreive deepest XPath value from XML document by using VB.NET? For example, if I had an XML file like this: <Root> <Customer> <Name>MyName</Name> </Customer>...
1
by: Terry Olsen | last post by:
When I let the GridView automatically generate columns, I can allow sorting. But when I turn autogenerate columns off, it won't allow sorting (column names are not hyperlinks). Is there a way I...
1
by: Lou | last post by:
How do I create an xpath expression to return a sorted nodelist? I need the sort to be by the attribute value. Example, If I have an xml list of names with "lastName" as an attribute <name...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.