Constructing XPath Query

simonverona87

Usenet Archive
Joined
Feb 4, 2013
Messages
1
Reaction score
0
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
 

Bjoern Hoehrmann

Usenet Archive
Joined
Aug 15, 2000
Messages
719
Reaction score
0
* 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).
 

Joe Kesselman

Usenet Archive
Joined
Feb 3, 2006
Messages
1,229
Reaction score
0
/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.
 

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
548,049
Messages
3,104,246
Members
248,133
Latest member
DujonT

Latest Threads

Top