Thanks guys. It did cross my mind to use either an existing Sitemap or Wayfinder snippet, but I wanted to be able to create something clean and simple by hand. Order by uri didn't seem to make any difference either. But as today is a new day and my brain is running on full steam I have come up with this, which seems to do the trick:
function listSections($id){
global $modx;
$c = $modx->newQuery('modResource');
$c->where(array(
'parent' => $id,
'hidemenu:!=' => '1', // only show menu visible
'isfolder:=' => '1' // only show folders
));
$c->sortby('menuindex','ASC');
$docs = $modx->getCollection('modResource',$c);
foreach ($docs as $doc) {
$section .= '<section>';
$section .= '<id>'.$doc->get('id').'</id>';
$section .= '<name>'.$doc->get('pagetitle').'</name>';
$section .= listSections($doc->get('id')) ? '<subsections>'.listSections($doc->get('id')).'</subsections>' : '';
$section .= '</section>';
}
return $section;
}
echo '<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<sections>'.listSections(2).'</sections>';
By using a function I can call it on each node to find more children if they exist.