Xquery grouping items where items relating to each other
I am using eXist Xquery. I am stuck with this issue.
I have a collection of 20 xml with content as per like below:
1.123.15.200.xml
<element name="userID">P111</element>
<element name="folderUID">A1</element>
<element name="imageUID">IMG15234</element>
1.123.15.205.xml
<element name="userID">P115</element>
<element name="folderUID">A34</element>
<element name="imageUID">IMG15238</element>
and etc...
A user can have multiple folders, and a folder can have multiple Images. 1 xml is equal to 1 image data.
for $x in distinct-values(collection('db/mapdb')//element[@name="userID"])
return
<group>
{
// This part is where I have issue, i have no idea what to write in within th开发者_开发知识库is portion?
}
</group>
Output:
<group>
<user>P111</user>
<folder id="A1">
<image>IMG15238</image>
<image>IMG16234</image>
<image>IMG23523</image>
</folder>
<folder id="A82">
<image>IMG132238</image>
<image>IMG42344</image>
<image>IMG242523</image>
</folder>
</group>
Thanks you.
let $col := collection('db/mapdb')//*[element]
for $user in distinct-values($col/element[@name eq 'userID']),
return
<group>
<user>{$user}</user>
{
for $fold in distinct-values($col[element[@name eq 'userID'] eq $user]
/element[@name eq 'folderUID'])
return
<folder id="{$fold}">
{
for $img in $col[element[@name eq 'userID'] eq $user]
[element[@name eq 'folderUID'] eq $fold]
/element[@name eq 'imageUID']
return
<image>{string($img)}</image>
}
</folder>
}
</group>
精彩评论