简体   繁体   中英

XQuery find if attributes not matching

I have two xmls of similar structure, the structure is as follows :

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

Now I am trying to use xQuery compare among two files, with 1-to-1 comparison, need to find if the xml holds good if not find out which are the 'Key' that are missing, or if value for that node is not matching.

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

This is the xQuery I was able to come up with, it finds if same attribute but different value in the nodes. 1) But I am not able to find if some attributes (Key) is missing. 2) Some Erec have repetitive Key like 'ColorCode', which is also wrongly popping up in my existing output, its matching ColorDesc with value Red in one doc to ColorDesc value Blue in other document. How can I fix this ?

Can this be done with xslt also ?

This looks like working .. some suggestions on improvement would be of great help..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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