Topic
  • 1 reply
  • Latest Post - ‏2012-10-17T14:27:21Z by SystemAdmin
SystemAdmin
SystemAdmin
746 Posts

Pinned topic need help with xquery

‏2012-10-17T10:31:40Z |
Hi guys, I need some help with xquery:

xml structure:

<root>
<country>
<name>USA</name>
<persons>
<person>
<name>fred</name>
<numbers>
<number>30</number>
<number>15</number>
<number>89</number>
</numbers>
</person
<person>
<name>ed</name>
<numbers>
<number>45</number>
<number>46</number>
<number>47</number>
</numbers>
</person
</persons>
</country>
<country>
<name>Brasil</name>
<persons>
<person>
<name>max</name>
<numbers>
<number>30</number>
<number>15</number>
<number>89</number>
</numbers>
</person
<person>
<name>charlie</name>
<numbers>
<number>45</number>
<number>46</number>
<number>47</number>
</numbers>
</person
</persons>
</country>
</root>

I want to get the parents of a number. f.e number 15 in root/country(Brasil)/person(max)/number(15).
Based on the number 15 I want to get my parents max and Brasil.

Maybe somebody can help me...

thanks
Updated on 2012-10-17T14:27:21Z at 2012-10-17T14:27:21Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: need help with xquery

    ‏2012-10-17T14:27:21Z  
    xuser wrote:
    I want to get the parents of a number. f.e number 15 in root/country(Brasil)/person(max)/number(15).
    Based on the number 15 I want to get my parents max and Brasil.
    There seems two have two person satisfying the "based on the number 15" alone. So if you want to get only one return from that of Brasil, you should have more criteria than that to narrowing the query.

    Maybe this query (use more xpath kind of thinking than xquery) can help you to get the general idea?
    
    (: suppose $doc hold the loaded xml document :) 
    
    for $node in $doc/root/country/persons/person[numbers/number=
    '15'] 
    
    return <person country=
    "{$node/parent::*/preceding-sibling::name}">
    {$node/name/text()
    }</person>