Constructing XPath Query

Discussion in 'XML' started by simonverona87, Feb 4, 2013.

  1. 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
     
    simonverona87, Feb 4, 2013
    #1
    1. Advertisements

  2. * wrote in comp.text.xml:
    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).
     
    Bjoern Hoehrmann, Feb 4, 2013
    #2
    1. Advertisements

  3. 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."
     
    Joe Kesselman, Feb 5, 2013
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.