Wednesday, 11 May 2016

Xquery to find duplciate data in the xml elements

Sample XML :
 <?xml version="1.0" encoding="UTF-8" ?>
<VehicleStatusData>
   <VehicleStatus>
      <position>
         <latitude>90.73</latitude>
         <longitude>100.73</longitude>
      </position>
      <status>
         <VehicleStatusEntry name="DOOR_POSITION_FRONT_LEFT" value="value12"/>
         <VehicleStatusEntry name="DOOR_POSITION_FRONT_LEFT" value="value14"/>
         <VehicleStatusEntry name="DOOR_POSITION_REAR_LEFT" value="value16"/>
      </status>
   </VehicleStatus>
</VehicleStatusData>

 Xquery to return duplicate data :
(:: pragma bea:global-element-parameter parameter="$vehicleStatusData1" element="VehicleStatusData" location="TeleWCDDIVHS.xsd" ::)

declare namespace xf = "http://tempuri.org/HelloWorld/sss/";

declare function xf:sss($vehicleStatusData1 as element(VehicleStatusData))
    {
        <DuplicateNames>
            {
             
                for $id in distinct-values($vehicleStatusData1/VehicleStatus/status/VehicleStatusEntry/@name)
                        where count($vehicleStatusData1/VehicleStatus/status/VehicleStatusEntry[@name eq $id]) gt 1
                        return $id
               
                 
            }
        </DuplicateNames>
};

declare variable $vehicleStatusData1 as element(VehicleStatusData) external;

xf:sss($vehicleStatusData1)