2

我有5辆车,每辆车有5个车主,我需要返回那些国家没有出现在其他车主中的车主(可以出现在同一辆车的车主中):

<root>
  <car>
   <id>1</id>
   <owner>
            <name>George Smith</name>
            <nation>USA</nation>
   </owner>
   <owner>
            <name>Carolina Herre</name>
            <nation>USA</nation>
   </owner>
   <owner>
            <name>Martha Belar</name>
            <nation>Denmark</nation>
   </owner>
   <owner>
            <name>Fernando Izza</name>
            <nation>Italy</nation>
   </owner>
   <owner>
            <name>George Smith</name>
            <nation>Italy</nation>
   </owner>
 </car>
 <car>
   <id>2</id>
   <owner>
            <name>George Gelar</name>
            <nation>USA</nation>
   </owner>
   <owner>
            <name>Gema Bio</name>
             <nation>Spain</nation>
    </owner>
    <owner>
             <name>Peter Vdf</name>
            <nation>Denmark</nation>
    </owner>
    <owner>
            <name>Felipe Rodriguez</name>
            <nation>Denmark</nation>
    </owner>
    <owner>
            <name>George Smith</name>
            <nation>USA</nation>
    </owner>
</car>
<car>
...
</car>
<car>
...
</car>
<car>
...
</car>
</root>

也就是说,给定 xml,输出将是:

<owner>
    <name>Fernando Izza</name>
    <nation>Italy</nation>
    <carID>1</carID>
</owner>
<owner>
    <name>George Smith</name>
    <nation>Italy</nation>
    <carID>1</carID>
</owner>
<owner>
    <name>Gema Bio</name>
    <nation>Spain</nation>
    <carID>2</carID>
</owner>

我试图获取没有出现在几辆汽车中的国家列表,然后获取来自这些国家的那些车主,但我不知道如何获取。

4

1 回答 1

1

您可以按国家/地区对车主进行分组,然后向上查看国家是否出现在不止一辆车的下方:

for $owner in doc("cars.xml")//car/owner
group by $nation := $owner/nation
where count($owner/..) eq 1
for $o in $owner
return <owner>{
  $o/*,
  <carID>{$o/../id/text()}</carID>
}</owner>
于 2017-11-09T14:05:11.777 回答