繁体   English   中英

XQuery查找属性是否不匹配

[英]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(),'&#xA;' ) 
                                 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,'&#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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM