declare function local:getRelated20s($item) { let $nextItem :=$item/following-sibling::*[local-name()!="record_20"and local-name()!="record_30"][1] for $related in $item/following-sibling::*[local-name()="record_20"] where if ($nextItem) then $related << $nextItem else true() return $related }; declare function local:getRelated30s($item) { let $nextItem := $item/following-sibling::*[local-name()!="record_30"][1] for $related in $item/following-sibling::*[local-name()="record_30"] where if($nextItem) then $related << $nextItem else true() return $related }; declare function local:getRelated11($item) { let $nextItem := $item/following-sibling::*[local-name()!="record_10"][1] for $related in $item/following-sibling::*[local-name()="record_11"] where if($nextItem) then $related << $nextItem else true() return $related }; { for $record_10 in doc('G:/Projects/knowledge/proprietaryInputFile_trailer.xml')/root/record_10 return {$record_10/customer_id/text()} {$record_10/lastname/text()} {$record_10/firstname/text()} { for $record_20 in local:getRelated20s($record_10) return {$record_20/account_id/text()} { for $record_30 in local:getRelated30s($record_20) return {$record_30/value/text()} } } { for $record11 in local:getRelated11($record_10) return {$record11/total_20/text()} } } (: Stylus Studio meta-information - (c) 2004-2007. Progress Software Corporation. All rights reserved. :)