for and doc() function
File: Data.xml <order> <car dept="A"> <number>1</number> <name language="en">name 1</name> </car> <car dept="B"> <number>2</number> <name language="en">name 2</name> </car> <car dept="B"> <number>3</number> <name language="en">name 3</name> </car> <car dept="C"> <number>4</number> <name language="en">name 4</name> <colorChoices>white gray</colorChoices> <desc>Our <i>favorite</i> shirt!</desc> </car> </order> File: Query.xquery for $prod in doc("Data.xml")//car[@dept = "B"] order by $prod/name return $prod/name Output: <?xml version="1.0" encoding="UTF-8"?> <name language="en">name 2</name> <name language="en">name 3</name>