I've done something like that in building a menu based on search results.
In your case you could generate the sitemap but only show an item after a check if it exists in your search result.
More in detail:
You can create a basic php array from the sitemap as well from the search result with page id's.
Then, showing the sitemap you could check for every item with php function "in_array" if the page id exists in the search array and only then echo the item.
Worked perfect for me in a travelsite I built where people first choose a sort of trip.
Based on the choice the site is searched for that type of trip (multiselectfield in every page) and after that a two level menu is created based on parts of the world and countries. So: part of the world - country - trip, more or less the same as your main - sub - page.
Hope this gives you a direction.