XML/XQuery/order by
Содержание
Inadvertent resorting in document inventory
File: Data.xml
<order>
<car model="A">
<id>0001</id>
<name language="en">name 1</name>
<colorChoices>navy black</colorChoices>
</car>
<car model="B">
<id>0002</id>
<name language="en">name 2</name>
</car>
<car model="B">
<id>0003</id>
<name language="en">name 3</name>
</car>
<car model="C">
<id>0004</id>
<name language="en">name 4</name>
</car>
</order>
File: Query.xquery
let $sortedProds := for $prod in doc("Data.xml")//car
order by $prod/id
return $prod
for $prodName in $sortedProds/name
return <li>{string($prodName)}</li>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<li>name 1</li>
<li>name 2</li>
<li>name 3</li>
<li>name 4</li>
order by clause demo
File: Data.xml
<order>
<car model="A">
<name language="en">name 1</name>
</car>
<car model="B">
<name language="en">name 2</name>
</car>
<car model="B">
<name language="en">name 3</name>
</car>
</order>
File: Query.xquery
for $prod in doc("Data.xml")/order/car
where $prod/@model = "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>
order by stateCt
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>
</car>
</order>
File: Query.xquery
for $prod in doc("Data.xml")//car
where $prod/@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>
The order by clause
File: Data.xml
<inventory>
<car model="A" id="0001" />
<car model="B" id="0002" />
<car model="B" id="0003" />
<car model="C" id="0004" />
<car model="C" id="0004" />
<car model="A" id="0001" />
</inventory>
File: Query.xquery
for $car in doc("Data.xml")//car
order by $car/@id
return $car
Output:
<?xml version="1.0" encoding="UTF-8"?>
<car model="A" id="0001"/>
<car model="A" id="0001"/>
<car model="B" id="0002"/>
<car model="B" id="0003"/>
<car model="C" id="0004"/>
<car model="C" id="0004"/>
Using an inventory comparison
File: Data.xml
<order>
<car model="A">
<id>0001</id>
<name language="en">name 1</name>
</car>
<car model="B">
<id>0002</id>
<name language="en">name 2</name>
</car>
<car model="B">
<id>0003</id>
<name language="en">name 3</name>
</car>
</order>
File: Query.xquery
let $prods := doc("Data.xml")//car
for $prod in $prods
where $prod << $prods[@model = $prod/@model][last()]
return $prod
Output:
<?xml version="1.0" encoding="UTF-8"?>
<car model="B">
<id>0002</id>
<name language="en">name 2</name>
</car>
Using multiple inventorying specifications
File: Data.xml
<inventory>
<car model="A" id="0001"/>
<car model="B" id="0002"/>
<car model="B" id="0003"/>
<car model="C" id="0004"/>
<car model="C" id="0004"/>
<car model="A" id="0001"/>
</inventory>
File: Query.xquery
for $car in doc("Data.xml")//car
order by $car/@model, $car/@id
return $car
Output:
<?xml version="1.0" encoding="UTF-8"?>
<car model="A" id="0001"/>
<car model="A" id="0001"/>
<car model="B" id="0002"/>
<car model="B" id="0003"/>
<car model="C" id="0004"/>
<car model="C" id="0004"/>
without inadvertent resorting
File: Data.xml
<order>
<car model="A">
<id>0001</id>
<name language="en">name 1</name>
</car>
<car model="B">
<id>0002</id>
<name language="en">name 2</name>
</car>
<car model="B">
<id>0003</id>
<name language="en">name 3</name>
</car>
<car model="C">
<id>0004</id>
<name language="en">name 4</name>
</car>
</order>
File: Query.xquery
for $prod in doc("Data.xml")//car
order by $prod/id
return <li>{string($prod/name)}</li>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<li>name 1</li>
<li>name 2</li>
<li>name 3</li>
<li>name 4</li>