Cathy Claires
Cathy Claires

Reputation: 79

query processing using xquery

i am totally new to xquery and I am having problem getting the right stuff out of my xml codes.

Here is a sample of my xml file that i am working on:

<root>
  <name id = "a">
    <first> Mary </first>
    <last> Britton </last>
    <company> ABC </company>
    <phone> 203-942-0485 </phone>
  </name>

  <paper pid = "b">
    <pname> Hello </pname>
    <author> a </author>
  </paper>

  <publish>
     <pbid> b </pbid>
     <location>
         <place> new york </place>
         <sales> 100 </sales>
     </location>
     <location>
         <place> los angeles </place>
         <sales> 200 </sales>
     </location>
  </publish>

I am trying to get the author's company and phone number for those who has published in new york with at least 50 sales.

Right now, I can only get the location information, not even the publish information. Any hint or help on how to think through this problem? Thanks in advance!

Upvotes: 1

Views: 63

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243459

This pure XPath 2.0 expression:

/*/name[for $auid in @id,
                  $p in /*/paper[author eq $auid],
                  $pid in $p/@pid
               return
                  sum(/*/publish[pbid[1] eq $pid]
                              /location[place eq 'new york']
                                    /sales
                     )
                  ge 50
           ]
            /(company | phone)

When evaluated against the following XML document (the provided one with extraneous white-space eliminated, in order to avoid having to construct a unnecessarily complicated XPath expression that would use normalize-space() almost everywhere):

<root>
    <name id = "a">
        <first> Mary </first>
        <last> Britton </last>
        <company> ABC </company>
        <phone> 203-942-0485 </phone>
    </name>
    <paper pid = "b">
        <pname> Hello </pname>
        <author>a</author>
    </paper>
    <publish>
        <pbid>b</pbid>
        <location>
            <place>new york</place>
            <sales> 100 </sales>
        </location>
        <location>
            <place> los angeles </place>
            <sales> 200 </sales>
        </location>
    </publish>
</root>

produces the wanted, correct result:

<company> ABC </company>
<phone> 203-942-0485 </phone>

Upvotes: 1

Related Questions