?
Solved

xpath query for grouping

Posted on 2003-03-17
11
Medium Priority
?
264 Views
Last Modified: 2012-08-14
I'm trying to do this...

    Set nodList = mdoc.selectNodes( _
      "computers/computer[not(domainname=preceding-sibling:domainname)]/domainname")
    For Each nod In nodList
        MsgBox nod.nodeName & " " & nod.Text
    Next

just to get a list of domain names out of the tree, but its just not working.

mdoc is a domdocument with <computers><computer><computername>abc</computername><domainname>xyz</domainname>... etc

Any ideas? thanks
0
Comment
Question by:xassets
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 14

Expert Comment

by:avner
ID: 8150385
Take alook at the Munchian grouping :

http://www.jenitennison.com/xslt/grouping/muenchian.html
0
 
LVL 9

Expert Comment

by:sparkplug
ID: 8150437
Hi,

I think your problem is that you are missing a forward slash and double colon. e.g.

"/computers/computer[not(domainname=preceding-sibling::domainname)]/domainname"

>S'Plug<
0
 
LVL 9

Expert Comment

by:sparkplug
ID: 8150451
Also missing a node - should actually be: "/computers/computer[not(domainname=preceding-sibling::computer/domainname)]/domainname"

>S'Plug<
0
Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

 
LVL 4

Author Comment

by:xassets
ID: 8150518
Thanks, but its still not working.

If I change it to

"computers/computer[not(domainname='xyz')]/domainname"

Then it does indeed return all domains except xyz (not distinct, obviously). It works with or without the front slash

The second colon gets rejected as a syntax error (expected token ')', found ':'). If I take it out, theres no syntax error but it selects no nodes.

Have tried many combinations including:

computers/computer[not(domainname=preceding-sibling:computers/computer/domainname)]/domainname

/computers/computer[not(domainname=preceding-sibling:domainname)]/domainname

computers/computer[not(.=preceding-sibling:computers/computer/domainname)]/domainname

... sadly, I am allergic to xsl, any ideas?

0
 
LVL 4

Author Comment

by:xassets
ID: 8150542
Also, would there be an easy way of getting the distinct list to be case insensitive? e.g. so domains xyz and XYZ return only one value?

Thanks
0
 
LVL 4

Author Comment

by:xassets
ID: 8150738
Also, would there be an easy way of getting the distinct list to be case insensitive? e.g. so domains xyz and XYZ return only one value?

Thanks
0
 
LVL 9

Accepted Solution

by:
sparkplug earned 80 total points
ID: 8150761
The preceding-sibling axis is only available in MSXML4. Once this is installed you should create your DOM object as follows:

Set mdoc = CreateObject("MSXML2.DOMDocument.4.0")

To do a case insensitive compare, you need to use ms:string-compare() which a microsoft extension to XPath.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/htm/xpath_functions_097y.asp

Here's the complete vbs code:

Set mdoc = CreateObject("MSXML2.DOMDocument.4.0")
mdoc.loadXML("<computers><computer><computername>abc</computername><domainname>xyz</domainname></computer><computer><computername>abc</computername><domainname>XYZ</domainname></computer></computers>")
mdoc.setProperty "SelectionNamespaces", "xmlns:ms='urn:schemas-microsoft-com:xslt'"
Set nodList = mdoc.selectNodes("/computers/computer[not(ms:string-compare(domainname, preceding-sibling::computer/domainname, 'en-GB', 'i') = 0)]/domainname")
For Each nod In nodList
   MsgBox nod.nodeName & " " & nod.Text
Next

>S'Plug<
0
 
LVL 4

Author Comment

by:xassets
ID: 8150786
Thanks, have some points, but we are tied to msxml3 so if you have any ideas for doing this in msxml3 I'd be grateful. At present we're having to let vb do a bubblesort and distinct loop which is not ideal.

0
 
LVL 9

Expert Comment

by:sparkplug
ID: 8150948
Actually, preceding-sibling does work in MSXML3. You need to explicitly specify the selection language. e.g mdoc.setProperty "SelectionLanguage", "XPath". Don't ask me why. Unfortunately the ms:string-compare() function does not work with MSXML3.

I guess your options are either to initially iterate through each node and apply LCase to the value or to use XSLT with some included script to do the case-insensitive compare and an output a new XML DOM containing the distinct list of nodes. The use of the latter option depends on your required output. I guess its not going to be a series of message boxes. I could give you an example of this however this will take time and require more points...

>S'Plug<
0
 
LVL 4

Author Comment

by:xassets
ID: 8151143
After adding the mdoc.setProperty statement, this one finally worked and it even let me use 2 colons...

computers/computer[not(domainname=preceding::domainname)]/domainname

well done.
0
 
LVL 4

Author Comment

by:xassets
ID: 8153483
In case anyone finds this in google etc, I managed to get the case insensitivity by using the xpath translate function e.g. :

"computers/computer[translate(domainname,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='" & LCase$(sDomain) & "']/computername"

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question