[英]XQuery find if attributes not matching
我有兩個結構相似的xml,結構如下:
<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比較,並進行一對一比較,如果沒有找到缺少的“關鍵字”,或者該節點的值不是,則需要查找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(),'
' )
else()
else()
else()
這是我能夠想到的xQuery,它查找節點中的屬性是否相同但值是否不同。 1)但是我無法找到某些屬性(鍵)是否丟失。 2)一些Erec具有重復的鍵,例如'ColorCode',它也在我的現有輸出中錯誤地彈出,它在一個文檔中將ColorDesc與值Red匹配,而在另一文檔中將ColorDesc值Blue匹配。 我怎樣才能解決這個問題 ?
也可以用xslt完成嗎?
這看起來像在工作..一些改進方面的建議會很有幫助。
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,'
' )
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(),'
' )
else()
else()
)
else()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.