# XML Namespaces Used by SunSitemap for Validation

> Discover the XML namespaces SunSitemap uses for validation. Learn how default Sitemaps and XML Schema Instance namespaces ensure accurate sitemap.xsd and siteindex.xsd schema adherence.

- Repository: [Mehmet Selcuk Batal/php-sitemap-generator](https://github.com/msbatal/php-sitemap-generator)
- Tags: api-reference
- Published: 2026-03-07

---

**SunSitemap declares the default Sitemaps namespace (`http://www.sitemaps.org/schemas/sitemap/0.9`) and the XML Schema Instance namespace (`http://www.w3.org/2001/XMLSchema-instance`) in every generated document to ensure strict validation against the official sitemap.xsd and siteindex.xsd schemas.**

The `msbatal/php-sitemap-generator` library generates standards-compliant XML sitemaps used by search engines to crawl websites efficiently. Understanding the XML namespaces used by SunSitemap for validation ensures your sitemap files conform to the Sitemaps.org protocol and pass strict XML schema validation checks.

## Default and XSI Namespace Declarations

SunSitemap generates two distinct XML document types depending on the number of URLs added. Both document types declare identical namespace prefixes but reference different schema definition files via the `xsi:schemaLocation` attribute.

### The urlset Root Element (Lines 94-99)

For standard sitemap files containing URL entries, SunSitemap constructs a `<urlset>` root element in [`SunSitemap.php`](https://github.com/msbatal/php-sitemap-generator/blob/main/SunSitemap.php) (lines 94-99) with the following namespace declarations:

```xml
<urlset
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
    xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

```

The **default namespace** (`xmlns`) defines core Sitemap elements including `<url>`, `<loc>`, `<lastmod>`, `<changefreq>`, and `<priority>`. The **xsi namespace** enables schema validation by referencing the official `sitemap.xsd` file.

### The sitemapindex Root Element (Lines 220-225)

When the URL count exceeds the configured limit and multiple sitemap files are generated, SunSitemap creates a sitemap index document using a `<sitemapindex>` root element (lines 220-225 of [`SunSitemap.php`](https://github.com/msbatal/php-sitemap-generator/blob/main/SunSitemap.php)):

```xml
<sitemapindex
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd"
    xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

```

Notice that while the namespace URIs remain identical, the `xsi:schemaLocation` attribute points to `siteindex.xsd` instead of `sitemap.xsd` to validate the index structure rather than individual URL entries.

## How createSitemap() Injects Namespaces

The **`createSitemap()`** method automatically constructs the appropriate XML header based on whether the script generates a single sitemap file or requires a sitemap index. You do not manually specify these namespaces; SunSitemap inserts them during the XML generation process to guarantee validation compliance.

When `createSitemap()` executes, it:

1. Determines whether to build a `<urlset>` or `<sitemapindex>` document
2. Injects the default namespace URI `http://www.sitemaps.org/schemas/sitemap/0.9`
3. Adds the `xmlns:xsi` declaration for schema instance validation
4. Sets the correct `xsi:schemaLocation` attribute pointing to either `sitemap.xsd` or `siteindex.xsd`

## Practical Usage Example

The following PHP implementation demonstrates how SunSitemap automatically handles XML namespaces during sitemap generation:

```php
<?php
require_once 'SunSitemap.php';

// Initialize the generator
$sitemap = new SunSitemap('https://example.com', 'sitemap/');

// Add URLs to the sitemap
$sitemap->addUrl('page1.html', '2024-01-01', 'monthly', '0.8')
        ->addUrl('page2.html', '2024-01-02', 'weekly', '0.5');

// Generate files with automatic namespace injection
$sitemap->createSitemap()
        ->updateRobots();
?>

```

Running this code produces XML files containing the validation namespaces at the root element, ensuring immediate compatibility with Google Search Console and other search engine validators without manual XML manipulation.

## Summary

- **SunSitemap.php** declares two XML namespaces in lines 94-99 and 220-225 to support schema validation.
- The **default namespace** (`http://www.sitemaps.org/schemas/sitemap/0.9`) defines all core Sitemap protocol elements.
- The **xsi namespace** (`http://www.w3.org/2001/XMLSchema-instance`) enables `xsi:schemaLocation` attributes that reference official XSD files.
- Standard sitemaps validate against `sitemap.xsd`, while sitemap indexes validate against `siteindex.xsd`.
- The **`createSitemap()`** method automatically injects these declarations; no manual namespace configuration is required.

## Frequently Asked Questions

### Can I customize the XML namespaces in SunSitemap?

No, the namespace URIs are hardcoded in [`SunSitemap.php`](https://github.com/msbatal/php-sitemap-generator/blob/main/SunSitemap.php) to ensure compliance with the official Sitemaps.org protocol. Modifying these would break validation against the standard schemas used by Google, Bing, and other search engines.

### Why does SunSitemap use two different schemaLocation values?

The `xsi:schemaLocation` attribute points to `sitemap.xsd` for `<urlset>` documents (individual URL listings) and `siteindex.xsd` for `<sitemapindex>` documents (lists of sitemap files). This distinction ensures the XML structure matches the appropriate schema definition for each document type.

### Are these namespaces required for search engine submission?

While search engines may parse sitemaps without strict schema validation, declaring these namespaces ensures the XML passes formal validation tests and prevents parsing errors. The `msbatal/php-sitemap-generator` repository includes these namespaces by default to guarantee maximum compatibility.

### Where are the namespace declarations located in the source code?

In [`SunSitemap.php`](https://github.com/msbatal/php-sitemap-generator/blob/main/SunSitemap.php), the `<urlset>` namespace declarations appear at lines 94-99, while the `<sitemapindex>` declarations are found at lines 220-225. Both sections construct the root element string that includes the `xmlns`, `xmlns:xsi`, and `xsi:schemaLocation` attributes.