2012-10-05 39 views
1

我有2個以下的xml文件(fruit.xml和toy.xml)。我想用xquery檢索日本國家生產的水果和玩具。從xml文件抓取數據

預期效果應該像下面這些玩具需要在水果前先列出。

<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

以下工程..

<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/

+0

試試就可以了測試,是的,它的工作原理。從未想過我們可以用這樣的方式聲明2 – setiasetia