2014-04-03 37 views
0

我有這兩個XML文件如何使用xquery比較不同xml文件中的值?

cars.xml

<cars> 
    <manufacturer> 
     <model modelID="1"> 
      </model> 
     <model modelID="2"> 
      </model> 
     <model modelID="3"> 
      </model> 
     </manufacturer> 
    <manufacturer> 
     <model modelID="4"> 
      </model> 
     <model modelID="5"> 
      </model> 
     <model modelID="6"> 
      </model> 
     </manufacturer> 
    </cars> 

和一個名爲price.xml:

<price> 
     <model modelID="1"> 
      <price>1000</price> 
      </model> 
     <model modelID="2"> 
      <price>3000</price> 
      </model> 
     <model modelID="3"> 
      <price>2000</price> 
      </model> 
     <model modelID="4"> 
      <price>2000</price> 
      </model> 
     <model modelID="5"> 
      <price>100</price> 
      </model> 
     <model modelID="6"> 
      <price>5000</price> 
      </model> 

</price> 

,我想執行的查詢是,對於每一個製造商cars.xml,我想返回它最昂貴的模型的modelID,但我無法弄清楚。

我已經試過是這樣的:

for $manf in doc("cars.xml")//manufacturer 
let $p := doc("price.xml") 
where $manf/model/@modelID = $p/model/@modelID 
     and $p/model/price = (for $m in doc("cars.xml")//manufacturer 
          return max(for $pr in doc("price.xml") 
             where $m/model/@modelID = $pr/model/@modelID 
             return data($pr/model/price))) 
return data($manf/model/@modelID) 

我不知道我是不是任何接近正確的,但基本上是我需要做的是爲每一個製造商,利用其modelID的莫名其妙找到哪種型號最昂貴。

回答

1
let $p := doc("price.xml") 
for $manf in doc("cars.xml")//manufacturer 
let $models := $p//model[@modelID = $manf/model/@modelID] 
let $max := max($models/price) 
return $models[price = $max] 
+1

你可以在現場http://try.zorba.io/queries/xquery/y8iT3K0nSVTHqh0wvHx7pzk3xpQ%3D嘗試 – wcandillon