1

我有以下 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>
4

1 回答 1

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>

你可以在这里试试http://basex.org/products/live-demo/

于 2012-10-05T08:51:07.430 回答