javaxmlspring-bootsitemap

How to remove ns2 from a sitemap xml?


I am trying to generate a proper sitemap in a Spring Boot with Java but it creates "ns2" that I don't want. Is it possible to remove?

My generated xml is as following:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:sitemapindex xmlns:ns2="http://www.sitemaps.org/schemas/sitemap/0.9">
    <sitemap>
        <loc>http://localhost:8080/api/listings/1.xml</loc>
    </sitemap>
    <sitemap>
        <loc>http://localhost:8080/api/listings/2.xml</loc>
    </sitemap>
</ns2:sitemapindex>

But I want it to look like the following one:

<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <sitemap>
        <loc>http://localhost:8080/api/listings/1.xml</loc>
    </sitemap>
    <sitemap>
        <loc>http://localhost:8080/api/listings/2.xml</loc>
    </sitemap>
</sitemapindex>

Is it achievable?

I have the following classes:

XmlUrl.java:

import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;
import lombok.Data;

@Data
@XmlAccessorType(value = XmlAccessType.NONE)
@XmlRootElement(name = "sitemap")
public class XmlUrl {
    @XmlElement
    private String loc;
}

XmlUrlSet.java:

import jakarta.xml.bind.annotation.*;
import lombok.Data;

import java.util.ArrayList;
import java.util.Collection;

@Data
@XmlAccessorType(value = XmlAccessType.NONE)
@XmlRootElement(name = "sitemapindex", namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")
public class XmlUrlSet {
    @XmlElements({@XmlElement(name = "sitemap", type = XmlUrl.class)})
    private Collection<XmlUrl> xmlUrls = new ArrayList<XmlUrl>();
}

package-info.java is located next to the SpringBootApplication.java:

@XmlSchema(
        namespace = "http://www.sitemaps.org/schemas/sitemap/0.9",
        elementFormDefault = XmlNsForm.QUALIFIED,
        xmlns = @XmlNs(namespaceURI = "http://www.sitemaps.org/schemas/sitemap/0.9", prefix = ""))
package com.example.test;

import jakarta.xml.bind.annotation.XmlNsForm;
import jakarta.xml.bind.annotation.XmlNs;
import jakarta.xml.bind.annotation.XmlSchema;

SitemapService.java:

@Service
public class SitemapService {

    public XmlUrlSet getAllSitemaps() {
        XmlUrlSet xmlUrlSet = new XmlUrlSet();
        Collection<XmlUrl> xmlUrls = new ArrayList<XmlUrl>();

        XmlUrl xmlUrl1 = new XmlUrl();
        xmlUrl1.setLoc("http://localhost:8080/api/listings/1.xml");
        xmlUrls.add(xmlUrl1);

        XmlUrl xmlUrl2 = new XmlUrl();
        xmlUrl2.setLoc("http://localhost:8080/api/listings/2.xml");
        xmlUrls.add(xmlUrl2);

        xmlUrlSet.setXmlUrls(xmlUrls);
        return xmlUrlSet;
    }
}

Solution

  • The default namespace is correctly defined in package-info.java with an empty prefix.

    @XmlSchema(
            namespace = "http://www.sitemaps.org/schemas/sitemap/0.9",
            elementFormDefault = XmlNsForm.QUALIFIED,
            xmlns = @XmlNs(namespaceURI = "http://www.sitemaps.org/schemas/sitemap/0.9", prefix = ""))
    package com.example.test;
    

    But @XmlRootElement(name = "sitemapindex", namespace = "http://www.sitemaps.org/schemas/sitemap/0.9") on XmlUrlSet is overwriting that definition and also applying the namespace to that element only leaving all descendants unqualified.
    Removing namespace from @XmlRootelement should give the expected result.