1

I want to rearrange the Order tags in the input xml given below such that the nodes follow this sequence

BUY_ONLY, BOGO, GET_ONLY

This is my sample Orders.xml The Order tags have to be re arranged based on the OrderType tag inside them

     (: XQuery Module :)
              declare namespace functx = "http://www.functx.com";
                for $order in doc("order.xml")/Orders/Order
                let $buyOnly := $order[OrderType/text()="BUY_ONLY"]
                let $getOnly := $order[OrderType/text()="GET_ONLY"]
                let $bogo := $order[OrderType/text()="BOGO"]
                return 
                 <Orders>
                 { 
                if(exists($buyOnly) and exists($getOnly) and exists($bogo)) then
                   (
                    $buyOnly,
                    $getOnly,
                    $bogo
                   )
                else ()    
               }
              </Orders>

Can I rearrange the Order nodes without using FLWOR expressions?

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
Beginner
  • 335
  • 2
  • 6
  • 17

1 Answers1

1

Here is how you can produce without any FLWOR statements:

<Orders>
{ 
 (
   doc("order.xml")/Orders/Order[OrderType/text()="BUY_ONLY"],
   doc("order.xml")/Orders/Order[OrderType/text()="GET_ONLY"],
   doc("order.xml")/Orders/Order[OrderType/text()="BOGO"]
 )   
}
</Orders>

A version with a more simplified FLWOR:

<Orders>
{ 
  let $order := doc("order.xml")/Orders/Order
  return
  (
    $order[OrderType/text()="BUY_ONLY"],
    $order[OrderType/text()="GET_ONLY"],
    $order[OrderType/text()="BOGO"]
  )   
}
</Orders>
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147