1

I am trying to generate a sitemap.xml using JAXB, and the location attribute is being ignored (I want to generate the xsi:schemaLocation attribute on my root element).

I want to generate an xml as follows:

<?xml version="1.0" encoding="UTF-8" ?>
<ns3:urlset 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ns3="http://www.sitemaps.org/schemas/sitemap/0.9"
    xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
        http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
    <urls>
        <loc>http://domain.com</loc>
    </urls>
    <urls>
        <loc>http://domain.com/test</loc>
    </urls>
</ns3:urlset>

I have seen other questions: @xmlSchema annotation use with jaxb and How to generate the correct sitemap namespace using JAXB and Spring @ResponseBody in controller?

But neither of these solve the problem I am experiencing with the annotation.

I have the following package-info:

@javax.xml.bind.annotation.XmlSchema(
    namespace = "http://www.sitemaps.org/schemas/sitemap/0.9",
    xmlns = @javax.xml.bind.annotation.XmlNs( prefix = "xsi", namespaceURI="http://www.w3.org/2001/XMLSchema-instance" ),
    location = "http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd",
    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.UNQUALIFIED
)
package com.domain.site.sitemap

However, my unit test:

@Test public void createXmlObject(){
    List urls = [ "test1", "test2", "test3" ]
    Sitemap map = new Sitemap( urls )
    JAXBContext jaxbContext = JAXBContext.newInstance( Sitemap )
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller()
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true)
    jaxbMarshaller.marshal(map, System.out)
}

generates a root element as follows:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns3:urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns3="http://www.sitemaps.org/schemas/sitemap/0.9">

If I update the test to explicitly set the location using jaxbMarshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "location..") then it will get populated - but I want to understand why it is not working with the annotation.

Community
  • 1
  • 1
rhinds
  • 9,976
  • 13
  • 68
  • 111

1 Answers1

1

According to specification:

Note to implementor [...]

However, the schema generator is allowed to use a different value in the schemaLocation attribute (including not generating such attribute), for example so that the user can specify a local copy of the resource through the command line interface.

So it's up to the implementation provider if location is generated or not.

S. Pauk
  • 5,208
  • 4
  • 31
  • 41
  • Thanks - I did spot that in the docs, but as my test is just using plain JAXB (JAXBContenxt creates a javax.xml.bind.Marshaller) I was hoping they would have implemented it - I assumes this means not though? – rhinds Mar 24 '15 at 10:06
  • 1
    Seems like `no`. Tried to drill into the sources but it was taking too much time. – S. Pauk Mar 24 '15 at 14:21