我有2 xml以下的文件(fruit.xml和toy.xml)。我想用xquery来检索日本国家生产的水果和玩具。
预期的结果应该像下面的玩具需要先在水果之前列出。
<JapanProduct>
<Prod type="Toy">Digimon<Prod>
<Prod type="Toy">Doll<Prod>
<Prod type="Fruit">Peach<Prod>
</JapanProduct>
下面是我编写的xquery代码,目前卡在这里。我可以按上述方式生成预期的结果吗?
for $f in doc("Fruit.xml")//Produce
for $t in doc("Toy.xml")//Produce
where $f[Country="Japan"] and $t[Country="Japan"]
return element JapanProduct {attribute Prod{if ($f[country=Japan"])then }
Fruit.XML
<fruitsOrigin>
<Produce>
<Country>Australia</Country>
<Prod>Kiwi</Prod>
<Size>M</Size>
</Produce>
<Produce>
<Country>China</Country>
<Prod>Pear</Prod>
<Size>M</Size>
</Produce>
<Produce>
<Country>Japan</Country>
<Prod>Peach</Prod>
<Size>L</Size>
</Produce>
</fruitsOrigin>
Toy.XML
<ToyMaker>
<Produce>
<Country>Australia</Country>
<Prod>Lego</Prod>
<cost>$15</cost>
</Produce>
<Produce>
<Country>Japan</Country>
<Prod>Doll</Prod>
<cost>$20</cost>
</Produce>
<Produce>
<Country>Japan</Country>
<Prod>Digimon</Prod>
<cost>$17</cost>
</Produce>
</ToyMaker>
答案 0 :(得分:0)
以下作品..
<JapanProduct>
{
for $t in doc("Toy.xml")//Produce
where $t[Country='Japan']
order by $t
return
<Prod type="Toy">{$t/Prod/string()}</Prod>
}
{
for $f in doc("Fruit.xml")//Produce
where $f[Country='Japan']
order by $f
return
<Prod type="Fruit">{$f/Prod/string()}</Prod>
}
</JapanProduct>