For stateCt
File: Data.xml <!-- Order input docuCt --> <order num="00299432" date="2006-09-15" cust="0221A"> <item dept="A" num="1" quantity="1" color="navy"/> <item dept="B" num="2" quantity="1"/> <item dept="B" num="3" quantity="2"/> <item dept="C" num="4" quantity="1" color="white"/> <item dept="C" num="4" quantity="1" color="gray"/> <item dept="A" num="1" quantity="1" color="black"/> </order> File: Query.xquery <title>Order Report</title>, (for $item in doc("Data.xml")//item order by $item/@num return $item) Output: <?xml version="1.0" encoding="UTF-8"?> <title>Order Report</title> <item dept="A" num="1" quantity="1" color="navy"/> <item dept="A" num="1" quantity="1" color="black"/> <item dept="B" num="2" quantity="1"/> <item dept="B" num="3" quantity="2"/> <item dept="C" num="4" quantity="1" color="white"/> <item dept="C" num="4" quantity="1" color="gray"/>