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 - 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
2 4043
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?
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 - 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 -
<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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
by: viditmittal |
last post by:
hi,
i have a xml file
<root>
<Type />
<Description />
<Category />
<employee >
<id>3</id>
|
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...
|
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...
|
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?
...
|
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>...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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...
| |