2014-03-06 32 views
0

我有類似結構的兩個個XML的結構如下:的XQuery找到,如果屬性不匹配

<output> 
<Erec Spec="1234"> 
    <Property Key="Id">12324</Property> 
    <Property Key="Price">9000.000000</Property> 
    <Property Key="Version">5</Property> 
    <Property Key="Catalog">2</Property> 
    <Property Key="ColorCode">991</Property> 
    <Property Key="ColorDesc">Red</Property> 
    <Property Key="ColorDesc">Blue</Property> 
    <Property Key="CrossSells">false</Property> 
    <Property Key="Currency">USD</Property> 
</Erec> 
... 
.... 
</output> 

現在我試圖使用XQuery比較兩個文件中,以1:1的對比,如果找不到缺少的'Key',或者如果該節點的值不匹配,則需要查找xml是否保持良好。

for $old in doc('reference.xml')/output/Erec 
    for $new in doc('comparison.xml')/output/Erec 
     return if (data($old/@Spec) = data($new/@Spec)) 
     (:Trying to find if both have same element 'Property' with same attribute value 'Key' but different node value:) 
     (:How to find if any of the attribute 'Key' is present in $propsOld but missing in $propsNew :) 
       then for $propsOld in $old/Property 
        for $propsNew in $new/Property 
        return if (data($propsOld/@Key) = data($propsNew/@Key)) 
          then if ($propsOld/text() != $propsNew/text()) 
           then concat("Attribute value mismatch - ",($old/@Spec)," -- ",$propsOld/@Key," -- ",$propsOld/text(),"|", $propsNew/text(),'&#xA;') 
           else() 
          else() 
       else() 

這是我能夠想到的xQuery,它發現相同的屬性但節點中的值不同。 1)但我無法找到某些屬性(Key)是否丟失。 2)某些Erec具有重複性的Key,例如'ColorCode',它也會在我現有的輸出中彈出錯誤,它的匹配ColorDesc在一個文檔中的值爲Red,在其他文檔中的ColorDesc值爲Blue。我怎樣才能解決這個問題 ?

這可以用xslt來完成嗎?

+0

「*可以這樣使用XSLT也可以做*?」我想是的 - 看,例如:http: //sackoverflow.com/questions/21127051/xslt-compare-two-similarxml-files/21137381#21137381和http://stackoverflow.com/questions/21466270/compare-data-of-2-xml-files-and-輸出差異/ 21471770#21471770 –

+0

謝謝@ michael.hor257k! – Tirtha

+0

請使用一些xQuery幫助 – Tirtha

回答

0

這個貌似工作..對一些改進的意見會有很大的幫助..

for $old in doc('reference.xml')/output/Erec 
    for $new in doc('comparison.xml')/output/Erec 
    return if (data($old/@Spec) = data($new/@Spec)) 
      then for $propsOld in $old/Property/@Key 
       return if(count($new/Property[@Key=$propsOld]) = 0) 
         then concat(" --- ",$propsOld, " Property doesn't exist ",$new/@Spec,'&#xA;') 
         else(
         if(count($new/Property[@Key=$propsOld]) = 1) 
         then if($propsOld/../text() != $new/Property[@Key=$propsOld]/text()) 
           then concat("Same Attribute, value mismatch - ",($old/@Spec)," -- ",$propsOld," -- ",$propsOld/../text(),"|", $new/Property[@Key=$propsOld]/text(),'&#xA;') 
           else() 
         else() 
         )  
      else() 
相關問題