<?php
declare(strict_types=1);
function renderSitemap(array $urls): string {
    $xml=new XMLWriter(); $xml->openMemory(); $xml->startDocument('1.0','UTF-8');
    $xml->startElement('urlset'); $xml->writeAttribute('xmlns','http://www.sitemaps.org/schemas/sitemap/0.9');
    foreach ($urls as $item) {
        $loc=(string)($item['loc']??'');
        if (!filter_var($loc,FILTER_VALIDATE_URL) || !in_array(parse_url($loc,PHP_URL_SCHEME),['http','https'],true)) continue;
        $xml->startElement('url'); $xml->writeElement('loc',$loc);
        if (!empty($item['lastmod'])) $xml->writeElement('lastmod',(string)$item['lastmod']);
        $xml->endElement();
    }
    $xml->endElement(); $xml->endDocument(); return $xml->outputMemory();
}
