473,419 Members | 1,544 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,419 software developers and data experts.

Xpath for unique value

Hi

I have the following xml structure

CONTRACTS
--CONTRACT
---SUPPLIERS
----SUPPLIER
-----SUPPLIERID
-----SUPPLIERNAME

I need to get all unique values for suppliers so I dont get the same supplier more than once. I'm using the following xpath but it's not working:

Xpath = CONTRACTS/CONTRACT/SUPPLIERS/SUPPLIER[not(SUPPLIERID=preceding-sibling::SUPPLIER/SUPPLIERID)]")

And how can I sort by SUPPLIERNAME?

Many thanks!
Mar 22 '10 #1
4 3263
jkmyoung
2,057 Expert 2GB
Just use preceding instead. Xpath = CONTRACTS/CONTRACT/SUPPLIERS/SUPPLIER[not(SUPPLIERID=preceding::SUPPLIER/SUPPLIERID)]")

Are you using xsl? Perhaps:
<xsl:sort select="SUPPLIERNAME"/>

This assumes you don't have any other types of SUPPLIER nodes, and if you did you should be using namespaces anyways.
Mar 22 '10 #2
This works, thank you very much.

I'm not using xsl. Is there another way?

Thanks again.
Mar 23 '10 #3
jkmyoung
2,057 Expert 2GB
What are you using? ......
Mar 23 '10 #4
Hi, I managed to solve the problem with the sorting. This is my code:

My xml is -----> REVISED

Expand|Select|Wrap|Line Numbers
  1. CONTRACTS
  2. --CONTRACT
  3. ---SUPPLIER
  4. ---COMMODITIES
  5. ----COMMODITY
  6. -----COMODDITYNAME
  7.  
  8.  
  9.         Dim xdoc As New XPathDocument("testxml.xml")
  10.         Dim nav As XPathNavigator = xdoc.CreateNavigator()
  11.         Dim expr As XPathExpression
  12.  
  13.  
  14.         expr = nav.Compile("/pf:CONTRACTS/pf:CONTRACT[not(pf:SUPPLIER=preceding::pf:SUPPLIER)]/pf:SUPPLIER")
  15.  
  16.         Dim namespaceManager As XmlNamespaceManager = New XmlNamespaceManager(nav.NameTable)
  17.         namespaceManager.AddNamespace("pf", "http://namespace.ac.uk")
  18.         expr.AddSort(".", XmlSortOrder.Ascending, XmlCaseOrder.None, String.Empty, XmlDataType.Text)
  19.         expr.SetContext(namespaceManager)
  20.         Dim nodes As XPathNodeIterator = nav.Select(expr)
  21.  
  22.         If nodes.Count > 0 Then
  23.  
  24.             Dim tr As String = Nothing
  25.  
  26.             myString.AppendLine("<table width='300px' border='0' cellpadding='0' cellspacing='0' border='0' class='datatable1'>")
  27.             myString.AppendLine("<th>Supplier name</th>")
  28.  
  29.             While nodes.MoveNext()
  30.  
  31.                 Dim supplier As XPathNavigator = nodes.Current.SelectSingleNode(".", namespaceManager)
  32.  
  33.                 myString.AppendLine("<tr><td>" & supplier.ToString())
  34.                 myString.AppendLine("</td></tr>")
  35.  
  36.             End While
  37.  
  38.             myString.AppendLine("</table>")
  39.  
  40.             Dim strOutput As String = myString.ToString()
  41.             lblOutput.Text = strOutput
  42.  
  43.  
  44.         Else
  45.  
  46.             lblOutput.Text = "No results for your search"
  47.  
  48.         End If
Mar 24 '10 #5

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

Similar topics

1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
1
by: Patrick Reilly | last post by:
I am trying to implement a better method than I already have to document database schemas with XML, and use XSLT to both generate database DDL statements (CREATE TABLE, etc) and to transform to...
14
by: inquirydog | last post by:
Hi- One frusterating thing for me with xsl is that I don't know how to make xslt throw some sort of exception when a value-of path does not exist. For instance, suppose I have the following...
3
by: Tjerk Wolterink | last post by:
Hello i have xml code like this: <page:page xmlns:page="namespacefor page"> <page:section> <page:header> <b>Hello</b>There </page:header> <page:content> --- HTML CODE like: <i>Y</i>es i...
3
by: David Elliott | last post by:
I am in the process of creating an application to scrape databases in order to create typed DataSets. One of the last pieces that I need to do is to remap database fields to something more...
1
by: Hollywood | last post by:
I have the following XSD created in VS.NET 2003: <?xml version="1.0" encoding="utf-8" ?> <xs:schema id="ReferralSchama" targetNamespace="http://test.org/Referral"...
2
by: Jo Goos | last post by:
Hello, I would like to count the unique values of a specific element in an XPath statement. Let's say I have the next XML document ... <CLUB> <MEMBER> <NAME>Fred</NAME>...
2
by: Bilal | last post by:
Hello, I'm stuck on this problem for quite some time and hope somebody would be able to guide me. Basically, I need to populate a large number of "template" XML files which have all...
0
by: Torsten Munkelt | last post by:
Hi, I want to write an XML-schema saying that this document <root> <edge type="special"> <target type="one"/> </edge> <edge type="special"> <target type="one"/>
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.