如何写查询flwor xquery?
How to write an inquiry flwor xquery?
我有5辆车,每辆车有5个车主,我需要return那些国家没有出现在其他车车主中的车主(可以出现在同一辆车的车主中):
<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>
我想获取几个车里没有出现的国家列表,然后获取那些国家的车主,但是不知道怎么获取。
您可以按车主所在的国家/地区对其进行分组,然后向上查看该国家/地区是否出现在不止一辆汽车下方:
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>
我有5辆车,每辆车有5个车主,我需要return那些国家没有出现在其他车车主中的车主(可以出现在同一辆车的车主中):
<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>
我想获取几个车里没有出现的国家列表,然后获取那些国家的车主,但是不知道怎么获取。
您可以按车主所在的国家/地区对其进行分组,然后向上查看该国家/地区是否出现在不止一辆汽车下方:
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>