1

I have an XQUERY 3.0 as follows on an XML file with 3 Purchase Order recs with 5 items:

 for $PurchaseOrder in doc("C:\Users\thebluephantom\Desktop\order.xml")//PurchaseOrder
   order by xs:integer($PurchaseOrder/Item/Quantity) ascending
   return $PurchaseOrder/Items/Item/Quantity

that does not return the results as I want. It has a typical nested structure e.g.

<PurchaseOrders>  
  <PurchaseOrder PurchaseOrderNumber="99503" OrderDate="1999-10-20">  
    <Address Type="Shipping">  
      <Name>Ellen Adams</Name>  
      ... 
    </Address>  
      <Address Type="Billing">  
        <Name>Tai Yee</Name>  
        <Street>8 Oak Avenue</Street>  
        ...
    </Address>  
    <DeliveryNotes>Please leave packages in shed by driveway.  </DeliveryNotes>  
    <Items>  
      <Item PartNumber="872-AA">  
      <ProductName>Lawnmower</ProductName>  
      <Quantity>100</Quantity>  
      <USPrice>148.95</USPrice>  
    <Comment>Confirm this is electric</Comment>  
  </Item>  
    <Item PartNumber="926-AA">  
      <ProductName>Baby Monitor</ProductName>  
      <Quantity>20</Quantity>  
      <USPrice>39.98</USPrice>  
      <ShipDate>1999-05-21</ShipDate>  
  </Item>  
</Items>  
 </PurchaseOrder>  
 <PurchaseOrder PurchaseOrderNumber="99505" OrderDate="1999-10-22">  
   ...

I get the result on 3 POs and 5 Order Items:

<Quantity>100</Quantity>
<Quantity>20</Quantity>
<Quantity>1</Quantity>
<Quantity>1000</Quantity>
<Quantity>1</Quantity>

Looking at a number of tutorials on groupings etc. I am not able to find out if this is in fact possible.

thebluephantom
  • 16,458
  • 8
  • 40
  • 83

1 Answers1

1

The XPath in your Order By is missing the Items axis, so it's selecting nothing and you are getting your results in document order. If you correct the XPath, then there will be a sequence of values selected in the xs:integer() invocation and will throw an error.

You could rewrite it like this:

for $quantity in 
         doc("C:\Users\thebluephantom\Desktop\order.xml")/PurchaseOrders/PurchaseOrder/Items/Item/Quantity
order by xs:integer($quantity) ascending
return $quantity
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147