Constructing XPath Query

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

  1. Guest

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

  2. * 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).
    --
    Björn Höhrmann · mailto: · http://bjoern.hoehrmann.de
    Am Badedeich 7 · Telefon: +49(0)160/4415681 · http://www.bjoernsworld.de
    25899 Dagebüll · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
    Bjoern Hoehrmann, Feb 4, 2013
    #2
    1. Advertising

  3. On 2/4/2013 2:54 PM, Bjoern Hoehrmann wrote:
    > /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."
    Joe Kesselman, Feb 5, 2013
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mohammad S Khan

    JDBC - Constructing a query realtime

    Mohammad S Khan, Oct 31, 2003, in forum: Java
    Replies:
    0
    Views:
    395
    Mohammad S Khan
    Oct 31, 2003
  2. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    1,956
    jan V
    Jul 29, 2005
  3. Alastair Cameron
    Replies:
    1
    Views:
    7,369
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  4. Anna
    Replies:
    0
    Views:
    507
  5. David Gordon

    xpath query query

    David Gordon, May 18, 2005, in forum: XML
    Replies:
    2
    Views:
    780
    David Gordon
    May 18, 2005
Loading...

Share This Page