4 Replies Latest reply: Sep 4, 2013 11:26 PM by 803630 RSS

    Xquery to compare lists

    803630

      Hi,

       

      I have an XML like this

       

      <orders>

      <order>

      <orderid>11</orderid>

      </status>

      <order>

      <order>

      <orderid>12</orderid>

      </status>

      <order>

      <order>

      <orderid>13</orderid>

      </status>

      <order>

      </orders>

       

      I have to populate the status field based on values of orderid present in another xml and mark the status as Added or Rejected.

       

      <selectedorders>

      <order>

      <orderid>11</orderid>

      <order>

      <order>

      <orderid>12</orderid>

      <order>

      </orders>

       

      ie I need

       

      <orders>

      <order>

      <orderid>11</orderid>

      <status>Added</status>

      <order>

      <order>

      <orderid>12</orderid>

      <status>Added</status>

      <order>

      <order>

      <orderid>13</orderid>

      <status>Removed</status>

      <order>

      </orders>

       

      Thanks for you help.

        • 1. Re: Xquery to compare lists
          991448

          Here is your xquery:-

          xquery version "1.0" encoding "Cp1252";

          (:: pragma  parameter="$anyType1" type="xs:anyType" ::)

          (:: pragma  type="xs:anyType" ::)

           

           

          declare namespace xf = "http://tempuri.org/OSB%20Project%201/XQ/Test3/";

           

          declare function xf:Test3($anyType1 as element(*))

              as element(*) {

           

              let $xml1 := <orders>

            <order>

            <orderid>11</orderid>

            <status/>

            </order>

            <order>

            <orderid>12</orderid>

            <status/>

            </order>

            <order>

            <orderid>13</orderid>

            <status/>

            </order>

            </orders>

           

           

          let $xml2:= <selectedorders>

          <order>

          <orderid>11</orderid>

          </order>

          <order>

          <orderid>12</orderid>

          </order>

          </selectedorders>

           

           

          let $orders := fn:data($xml2//orderid)

          let $out := <orders>{for $order in $xml1/order

            let $orderNumber := $order/orderid/text()

              return

            <order>

            <orderid>{$orderNumber}</orderid>

            <status>{if($orderNumber = $orders) then "Added" else "Removed"}</status>

            </order>} </orders>

           

           

                  return $out

          };

           

           

           

           

           

           

          declare variable $anyType1 as element(*) external;

           

           

          xf:Test3($anyType1)

          • 2. Re: Xquery to compare lists
            803630

            Thanks for replyiong,

             

            But did you notice that you are not looping all the orders under <selectedoffers>

            • 3. Re: Xquery to compare lists
              991448

              I am collecting all the orders <slectedorders> in the sequence let $orders := fn:data($xml2//orderid) then checking if the order in <Orders> exist in the sequence and update accordingly.. There is no need to loop in <selectedorders> . Exceute this xquery and let us know if you have any issues.

              • 4. Re: Xquery to compare lists
                803630

                you are absolute genius.....great help man..thanks