Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xpath and sets 1

Status
Not open for further replies.

gacaccia

Technical User
Joined
May 15, 2002
Messages
258
Location
US
hi all,

i'm trying to make a simple xpath statement that will return all elements that match two criteria. here's an example of the xml document structure.

Code:
<utilities>
   <utility uName="u1">
       <product pName="p1">
            <version>1.0</version>
            <version>1.2</version>
       <product pName="p2">
   </utility>
</utilities>

what i want to be able to do is select utilities that are for a given product and at a given version. i'm using vb.net and loading the document as an xmldocument and then populating a nodelist using the selectnodes method. i can do this with something like "utilities/utility [product [@pName='p1']/version[.='1.0']]". however, for design reasons, i can't use this approach. instead, what i'd like to be able to do is say "//utility [//product [@pName='p1'] and //version [.='1.0']]".

the problem is, the the above statement returns all utilities that are either 'p1' OR '1.0', instead of returning utilities that match both. any way to get it to only return utilities that match all criteria?

thanks,

glenn
 
I believe that '//' means: descendant-axis;
'//product' means: 'any descendant named product'.
As you don't state where to start, all descendants from the root are parsed.
If you change it to './/' you'll get all descendants from self (the context-node).
//utility [.//product [@pName='p1'] and .//version [.='1.0']]
would do the trick.
Still, I wonder if your design isn't a bit limited if it can't handle all x-path-expressions.
 
thanks for the response. works perfect. since you mentioned the design motivation behind this, i thought i'd explain what i'm trying to do.

a while back i made a vb.net application as an xml document interface for a specific xml document that holds information on utilities for various programs. it consisted of various controls including a few listview controls, a treeview control, and some comboboxes, all linked together in various ways data-wise.

i recently started work on another project that also makes use of xml for the backend and found myself copying large sections of code from the prior project. it struck me that it'd be nice if i could generalize my code into custom xml controls so that i could just set some paramaters and the controls would do the rest.

one of the issues is how to pass criteria to a control to filter what it displays. if there's only one criteria, it's easy to have the control accept any xpath string from another control, but if the control needs to be able to accept anywhere from zero to unlimited criteria, and if the criteria does not necesarrily have a desired order to it, i need some way for a control to be able to pass in criteria to a linked control without worrying about order.

to that end, i thought that using the ".//element-name" would work well, though it does assume that a given element only appears at one level of the document tree.

in the example i gave at first, the base criteria would be for all utilities, "//utility". if multiple criteria needed to be passed from another control, it could be easily tagged onto the end of the base criteria.

glenn

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top