There is no such thing as a the parent of a space yet, since XWiki only has documents represented in the database. However, by convention the space is usually represented by its homepage, SpaceName.WebHome
. So, you should check the parent on this document.
#set ($spaces = $xwiki.spaces)
#set ($hiddenSpaces = ["XWiki", "Admin", "Panels", "Blog", "Main"])
#foreach ($space in $spaces)
#set ($spaceHome = $xwiki.getDocument("${space}.WebHome"))
#set ($spaceParent = $spaceHome.parent)
... and the rest of the code ...
#end
But this uses slightly deprecated methods. You should use entity references instead of strings:
#set ($spaceHome = $xwiki.getDocument($services.model.createDocumentReference($doc.documentReference.wikiReference.name, $space, '', 'default')))