Использование :
let $vals := /*/Exhibit/@title
return
for $t in $vals[index-of($vals, .)[1]]
return
element Exhibit
{ attribute Title {$t},
let $gvals := /*/Exhibit[@title eq $t]/Guide/@gname
return
for $gname in $gvals[index-of($gvals, .)[1]]
return
element Guide {attribute gname {$gname}}
}
При применении к этому документу XML (предоставленному, несколько более сложному) :
<Exhibits>
<Exhibit title = "Renaissance" sdate= "2011-09-05" edate= "2012-02-05" sarea = "2400">
<Guide gname = "John Smith" >
<Lang sname ="English"/>
<Lang sname ="German"/>
</Guide>
<Guide gname = "Weber Schneider">
<Lang sname = "German"/>
</Guide>
<Guide gname = "Robert Lundstrom">
<Lang sname = "Swedish"/>
</Guide>
<Guide gname = "Paula Munoz">
<Lang sname = "Spanish"/>
<Lang sname = "German"/>
<Lang sname = "English"/>
</Guide>
</Exhibit>
<Exhibit title = "Renaissance" sdate= "2011-10-06" edate= "2011-11-26" sarea = "2000">
<Guide gname = "John Smith">
<Lang sname ="English"/>
<Lang sname ="German"/>
</Guide>
<Guide gname = "Paula Munoz">
<Lang sname = "Spanish"/>
<Lang sname = "German"/>
<Lang sname = "English"/>
</Guide>
</Exhibit>
<Exhibit title = "Middle Age" sdate= "2011-10-06" edate= "2011-11-26" sarea = "2000">
<Guide gname = "John Smith">
<Lang sname ="English"/>
<Lang sname ="German"/>
</Guide>
<Guide gname = "Paula Munoz">
<Lang sname = "Spanish"/>
<Lang sname = "German"/>
<Lang sname = "English"/>
</Guide>
</Exhibit>
<Exhibit title = "Middle Age" sdate= "2011-09-05" edate= "2012-02-05" sarea = "2400">
<Guide gname = "John Smith" >
<Lang sname ="English"/>
<Lang sname ="German"/>
</Guide>
<Guide gname = "Weber Schneider">
<Lang sname = "German"/>
</Guide>
<Guide gname = "Robert Lundstrom">
<Lang sname = "Swedish"/>
</Guide>
<Guide gname = "Paula Munoz">
<Lang sname = "Spanish"/>
<Lang sname = "German"/>
<Lang sname = "English"/>
</Guide>
</Exhibit>
</Exhibits>
желаемый, правильный результат получается :
<Exhibit Title="Renaissance">
<Guide gname="John Smith"/>
<Guide gname="Weber Schneider"/>
<Guide gname="Robert Lundstrom"/>
<Guide gname="Paula Munoz"/>
</Exhibit>
<Exhibit Title="Middle Age">
<Guide gname="John Smith"/>
<Guide gname="Paula Munoz"/>
<Guide gname="Weber Schneider"/>
<Guide gname="Robert Lundstrom"/>
</Exhibit>
Объяснение :
Мы используем тот факт, что для последовательности атомарных значений$vals
:
$vals[index-of($vals, .)[1]]
создает набор элементов со всеми различными значениями, содержащимися в $vals
.