Having trouble putting together Image Sitemap XSD according google specs

I currently have a sitemap configuration for our website, however I’m trying to add the images according google sitemap specification.

Here’s how it looks like today:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">     <url>         <loc>https://www.example.com/</loc>     </url>     <url>         <loc>https://www.example.com/category/something</loc>     </url>     <url>         <loc>https://www.example.com/category/something2</loc>     </url> </urlset> 

And how it should look like:

<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"         xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">   <url>     <loc>http://example.com/</loc>     <image:image>       <image:loc>http://example.com/image.jpg</image:loc>     </image:image>     <image:image>       <image:loc>http://example.com/photo.jpg</image:loc>     </image:image>   </url>  </urlset>  

According to google’s documentation (https://www.google.com/schemas/sitemap-image/1.1/) I need to use the core sitemap for the core elements (the one I’m still using) and use a the image one for image elements.

That’s the tricky part for me – it looks very simple to do it, but I’m not that skilled to achieve that.

I managed to do that in my XSD file (I’m using jabx plugin):

<xsd:schema         xmlns:xsd="http://www.w3.org/2001/XMLSchema"         xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"         xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"         targetNamespace="http://www.sitemaps.org/schemas/sitemap/0.9" elementFormDefault="qualified">  <xsd:import namespace="http://www.google.com/schemas/sitemap-image/1.1" schemaLocation="image.xsd"/>  <xsd:element name="urlset">         <xsd:complexType>             <xsd:sequence>                 <xsd:element name="url" type="tUrl" maxOccurs="unbounded"/>             </xsd:sequence>         </xsd:complexType>     </xsd:element>      <xsd:complexType name="tUrl">         <xsd:sequence>             <xsd:element name="loc" type="tLoc"/>             <xsd:element name="lastmod" type="tLastmod" minOccurs="0"/>             <xsd:element name="changefreq" type="tChangeFreq" minOccurs="0"/>             <xsd:element name="priority" type="tPriority" minOccurs="0"/>             <xsd:element maxOccurs="unbounded" ref="image:image"/>         </xsd:sequence>     </xsd:complexType>  <!-- and goes on --> 

This makes it work, but it scrambles the namespaces – the image elements don’t have the namespace image, and the core elements receive a random namespace ns2:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ns2:urlset xmlns="http://www.google.com/schemas/sitemap-image/1.1"              xmlns:ns2="http://www.sitemaps.org/schemas/sitemap/0.9">     <ns2:url>         <ns2:loc>https://www.example.com/</ns2:loc>         <ns2:changefreq>daily</ns2:changefreq>         <ns2:priority>0.7</ns2:priority>     </ns2:url>     <ns2:url>         <ns2:loc>https://www.example.com/category/example1</ns2:loc>         <ns2:changefreq>daily</ns2:changefreq>         <ns2:priority>0.7</ns2:priority>         <image>             <loc>https://www.example.com/images/ẗest.jpg</loc>             <caption>test</caption>             <title>test</title>         </image>         <image>             <loc>https://www.example.com/images/ẗest2.jpg</loc>             <caption>test2</caption>             <title>test2</title>         </image>     </ns2:url> </ns2:urlset> 

I’m running out of ideas. What am I doing wrong?

Add Comment
1 Answer(s)

Not sure if there was another way of doing this, but I fixed the prefix by creating a namespace mapper:

public class DefaultNamespacePrefixMapper extends NamespacePrefixMapper {      private Map<String, String> namespaceMap = new HashMap<>();      /**      * Create mappings.      */     public DefaultNamespacePrefixMapper() {         namespaceMap.put("http://www.sitemaps.org/schemas/sitemap/0.9", "");         namespaceMap.put("http://www.google.com/schemas/sitemap-image/1.1", "image");     }      @Override     public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {         return namespaceMap.getOrDefault(namespaceUri, suggestion);     } } 

And when using the marshaller, I simply set:

Marshaller m = context.createMarshaller(); DefaultNamespacePrefixMapper mapper = new DefaultNamespacePrefixMapper(); m.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper", mapper); 

My XML looks correct exactly the same way as google documentation.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.