Constructing XPath Query

S

simonverona87

Hi,

I am a little rusty on using XPath - not having had used it in a couple of years

I have an xmldocument - coming from a restful web service and being processed in vb.net .

The xml resembles the following layout :-

<result>
<requests>
<request>
<form name="id">1234</form>
<form name="customer"> ABC </form>
</request>
<request>
<form name="id">1235 </form>
<form name="customer"> DEF</form>
</request>
....
....
</requests>
</result>

I want to be able to count how many requests there are for specific customers (perhaps more than one at once) and thought I could do this by using an xpath query to return a nodelist which selects the nodes which are of the "form" element with the "name" attribute equal to "customer" then limiting the nodesselected where the customer name is one of the customers im lookingford (eg ABC and DEF).

Once I have the nodelist ill be able to count the nodes in the result.

I just cant form the xpath query though! The closest ive got is /result/requests/request/form[@name='customer'] which selects all the customer nodes, but I cant work out how to then limit the nodes selected to be for only certain customers.....

Can anybody point me in the right direction?

Thanks
Simon
 
B

Bjoern Hoehrmann

* (e-mail address removed) wrote in comp.text.xml:
The xml resembles the following layout :-

<result>
<requests>
<request>
<form name="id">1234</form>
<form name="customer"> ABC </form>
</request>
<request>
<form name="id">1235 </form>
<form name="customer"> DEF</form>
</request>
...
...
</requests>
</result>
I just cant form the xpath query though! The closest ive got is
/result/requests/request/form[@name='customer'] which selects all the
customer nodes, but I cant work out how to then limit the nodes selected
to be for only certain customers.....

Something like

/result/requests/request[form[@name='customer' and . = 'DEF']]

would select `request` elements that have a `form` child that has a
`name` attribute with the value identical to 'customer' and a string
value identical to 'DEF'. You might want to put normalize-space() a-
round the `.` (which stands for the "context node", the `form` element
in your case).
 
J

Joe Kesselman

/result/requests/request[form[@name='customer' and . = 'DEF']]

Or, if there's one form per request:
/result/requests/request/form[@name='customer' and . = 'DEF']
or
/result/requests/request/form[@name='customer'][. = 'DEF']
or...

In XPath, as in most expression languages, there are usually multiple
ways to accomplish the same result. Which is most efficient may depend
on the quirks of a particular processor.

--
Joe Kesselman,
http://www.love-song-productions.com/people/keshlam/index.html

{} ASCII Ribbon Campaign | "may'ron DaroQbe'chugh vaj bIrIQbej" --
/\ Stamp out HTML mail! | "Put down the squeezebox & nobody gets hurt."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,828
Messages
2,569,736
Members
45,520
Latest member
JudithMorf

Latest Threads

Top