【发布时间】:2014-03-06 01:14:21
【问题描述】:
我有两个类似结构的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 来完成吗?
【问题讨论】:
-
"这也可以用 xslt 完成吗?" 我相信是这样 - 例如:stackoverflow.com/questions/21127051/… 和 stackoverflow.com/questions/21466270/…
-
谢谢@michael.hor257k !
-
请提供一些 xQuery 帮助