【问题标题】:XQuery find if attributes not matchingXQuery 查找属性是否不匹配
【发布时间】: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(),'&#xA;' ) 
                                 else()
                            else()
                else()

这是我能够提出的 xQuery,它会在节点中查找相同的属性但不同的值。 1)但我无法找到是否缺少某些属性(键)。 2) 一些 Erec 具有重复的键,例如“ColorCode”,它也在我现有的输出中错误地弹出,它在一个文档中将 ColorDesc 与值 Red 匹配到另一个文档中的 ColorDesc 值 Blue。我该如何解决这个问题?

这也可以用 xslt 来完成吗?

【问题讨论】:

标签: xml xslt xslt-1.0 xquery


【解决方案1】:

这看起来可行..一些改进建议会很有帮助..

    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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    • 2016-10-13
    • 2020-06-20
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 2015-01-20
    相关资源
    最近更新 更多