我有两个相似结构的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比较,并进行1-1的比较,需要查找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与一个doc中的value Red匹配到另一个文档中的colordesc value 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()