XML/XQuery/distinct values

Материал из Web эксперт
Перейти к: навигация, поиск

Distinctness on a combination of values

File: Data.xml

<order>
  <car model="A">
    <id>1</id>
    <name language="en">name 1</name>
  </car>
  <car model="B">
    <id>2</id>
    <name language="en">name 2</name>
  </car>
  <car model="B">
    <id>3</id>
    <name language="en">name 3</name>
  </car>
</order>

File: Query.xquery
let $prods := doc("Data.xml")//car
for $d in distinct-values($prods/@model),
    $n in distinct-values($prods[@model = $d]/id)
return <result model="{$d}" id="{$n}"/>

Output:
<?xml version="1.0" encoding="UTF-8"?>
<result id="1" model="A"/>
<result id="2" model="B"/>
<result id="3" model="B"/>