XQuery查找属性是否不匹配

时间:2014-03-06 01:14:21

标签: xml xslt xslt-1.0 xquery

我有两个类似结构的xmls,结构如下:

<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是否保持良好,如果没有找出哪个是缺少的'Key',或者如果值为节点不匹配。

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有像'ColorCode'这样的重复键,它在我现有的输出中也错误地弹出,其匹配的ColorDesc值在一个doc中为Red,在其他文档中为ColorDesc值Blue。我该如何解决这个问题?

这可以用xslt完成吗?

1 个答案:

答案 0 :(得分: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()