473,503 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

extracting distinct attributes from a document

I've an xml document that looks a bit like this

<Vendors>
<Vendor Stationery="Fred" />
<Vendor Stationery="bert" />
<Vendor Stationery="bert" />
</Vendors>

I want to extract a list of the distinct values of Stationery. I've found
some things on the web that suggest XPath statements to do this (in XSLT),
but I get the error 'The Expression passed to this should result in a node
set with my attempts. Here is my code

String XPathString = "Vendor/[not(@InvoiceStationery =
preceding-sibling::Vendor/@InvoiceStationery]@InvoiceStationery";

XmlNodeList Stationery = xmlVendors.SelectNodes(XPathString);

I can select fine with Vendor/@InvoiceStationery, but with the duplications.
I've tried several variations but I am unsure if it is the XPath or
something else. My GUESS is that the error means that the return is a
scalar value and not nodes, but I don't find it the clearest description
ever!

Iain
Nov 12 '05 #1
4 5281
Iain wrote:

<Vendors>
<Vendor Stationery="Fred" />
<Vendor Stationery="bert" />
<Vendor Stationery="bert" />
</Vendors>

I want to extract a list of the distinct values of Stationery. I've found
some things on the web that suggest XPath statements to do this (in XSLT),
but I get the error 'The Expression passed to this should result in a node
set with my attempts. Here is my code

String XPathString = "Vendor/[not(@InvoiceStationery =
preceding-sibling::Vendor/@InvoiceStationery]@InvoiceStationery";


Well,
1) in your XML there is no InvoiceStationery attribute.
2) several syntax errors, try
String XPathString = "Vendor/[not(@Stationery =
preceding-sibling::Vendor/@Stationery)]/@Stationery";

Beware, that's goind to be deadly slow on big XML documents.
Much scalable solution is to leverage EXSLT.NET [1] set:distinct
extension function.
[1] http://workspaces.gotdotnet.com/exslt
--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Nov 12 '05 #2
Thanks, Oleg. In real life I had the attribute InvoiceStationery, but
shortened it for the example. For simplicity I've now set the attribute
name to be Stationery and provided a 3 line sample ...

But. Your code copied as is does not work. It does the same thing.

I'm doing this with an XmlDocument - that should be OK, should it not?

XmlDocument xx = new XmlDocument();
xx.LoadXml("<Vendors><Vendor Stationery=\"Fred\" /> <Vendor
Stationery=\"bert\" /> <Vendor Stationery=\"bert\" /></Vendors>");

String XPathString = "Vendor/[not(@Stationery =
preceding-sibling::Vendor/@Stationery)]/@Stationery";

XmlNodeList test = xx.SelectNodes(XPathString);

Thanks

iain
"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:#X**************@TK2MSFTNGP09.phx.gbl...

Well,
1) in your XML there is no InvoiceStationery attribute.
2) several syntax errors, try
String XPathString = "Vendor/[not(@Stationery =
preceding-sibling::Vendor/@Stationery)]/@Stationery";

Beware, that's goind to be deadly slow on big XML documents.
Much scalable solution is to leverage EXSLT.NET [1] set:distinct
extension function.
[1] http://workspaces.gotdotnet.com/exslt
--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com

Nov 12 '05 #3
Iain wrote:
Thanks, Oleg. In real life I had the attribute InvoiceStationery, but
shortened it for the example. For simplicity I've now set the attribute
name to be Stationery and provided a 3 line sample ...

But. Your code copied as is does not work. It does the same thing.

I'm doing this with an XmlDocument - that should be OK, should it not?

XmlDocument xx = new XmlDocument();
xx.LoadXml("<Vendors><Vendor Stationery=\"Fred\" /> <Vendor
Stationery=\"bert\" /> <Vendor Stationery=\"bert\" /></Vendors>");

String XPathString = "Vendor/[not(@Stationery =
preceding-sibling::Vendor/@Stationery)]/@Stationery";


Syntax error again. It should be
Vendor[not(@Stationery =
preceding-sibling::Vendor/@Stationery)]/@Stationery"

And as this expression starts with Vendor, it must be executed on
Vendors element:
xx.DocumentElement.SelectNodes(XPathString);

Then it gives me 2 distinct Stationery attributes.
--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Nov 12 '05 #4
Thanks very much. I'm still struggling with XPath - not really getting my
mind round it.

In the meantime, I just selected all the nodes, copied the values to an
array, sorted it and skipped the duplicates (incidentally, I was amazed at
how easy this was to do. Try doing this in C++ in roughly 30 seconds!). I
feel ashamed, but it works and may even be more efficient!

But I'm glad to know how I should have done it!

Iain

"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:ui**************@TK2MSFTNGP12.phx.gbl...
Iain wrote:
Thanks, Oleg. In real life I had the attribute InvoiceStationery, but
shortened it for the example. For simplicity I've now set the attribute
name to be Stationery and provided a 3 line sample ...

But. Your code copied as is does not work. It does the same thing.

I'm doing this with an XmlDocument - that should be OK, should it not?

XmlDocument xx = new XmlDocument();
xx.LoadXml("<Vendors><Vendor Stationery=\"Fred\" /> <Vendor
Stationery=\"bert\" /> <Vendor Stationery=\"bert\" /></Vendors>");

String XPathString = "Vendor/[not(@Stationery =
preceding-sibling::Vendor/@Stationery)]/@Stationery";


Syntax error again. It should be
Vendor[not(@Stationery =
preceding-sibling::Vendor/@Stationery)]/@Stationery"

And as this expression starts with Vendor, it must be executed on
Vendors element:
xx.DocumentElement.SelectNodes(XPathString);

Then it gives me 2 distinct Stationery attributes.
--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com

Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
10839
by: Kelvin | last post by:
Okay so this is baking my noodle. I want to select all the attritbutes/fields from a table but then to excluded any row in which a single attributes data has been duplicated. I.E. Here's my...
2
2623
by: Chris Belcher | last post by:
First some background... The database tracks Action Items assigned to a group of 20 or so managers. Once the assignment is created it is then emailed to each of the managers that are included in...
3
3331
by: news | last post by:
I am trying to get at the source of a web page. Looking at the innerHTML element is only part of the story. In IE, right-clicking on various different parts of the page gives me different results...
2
1968
by: Thomas R. Hummel | last post by:
Hello, I am currently working on a monthly load process with a datamart. I originally designed the tables in a normalized fashion with the idea that I would denormalize as needed once I got an...
2
2490
by: Kevin K | last post by:
Hi, I'm having a problem with extracting text from a Word document using StreamReader. As I'm developing a web application, I do NOT want the server to make calls to Word. I want to simply...
2
2543
by: aglaforge | last post by:
I'm attempting to write a quick piece of Javascript code that will validate if the end user of the javascript has the necessary VML attributes set in their HTML. The problem in IE is that...
1
3325
by: Damien | last post by:
I have seen examples of selecting distinct elements by name, but my issue is that I need to get distinct attributes by name. My XML looks like: <root> <file> <rows> <row att1="1" /> <row...
4
2754
by: Debbiedo | last post by:
My software program outputs an XML Driving Directions file that I need to input into an Access table (although if need be I can import a dbf or xls) so that I can relate one of the fields...
2
4307
by: rds80 | last post by:
In the xml document below, I would like to retrieve the distinct attributes for the element '<Bal>'. However, I haven't had any success. Here is what I have so far: <TRANS> <TRAN...
0
7086
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
7332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
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
7462
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
5578
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
4673
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...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
382
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.