Extending the Sitemap.xml

Extending Google Sitemap with Images and Language Versions

XperienCentral exposes three interfaces that make it possible to integrate images and content item language versions into Google Sitemap:

  • SEOGoogleSitemapFilterProvider
  • SEOGoogleSitemapImageProvider
  • SEOGoogleSitemapImageEnrichProvider

The following are examples of how to use each interface. See also the XperienCentral Javadoc for complete information on these interfaces.

SEOGoogleSitemapFilterProvider

This implementation excludes modular content items with a certain property from the sitemap. The rest of the code is a default service component.

public class SitemapRedirectFilterProviderServiceImpl extends SimpleServiceComponent implements SEOGoogleSitemapFilterProvider {
   // Logger for this class
   private static final Logger LOG = Logger.getLogger(SitemapRedirectFilterProviderServiceImpl.class.getName());

   @Override
   public boolean includeInGoogleSitemap(Website website, ContentItemVersion<?> contentItemVersion) {
      if(contentItemVersion instanceof ModularMediaItemVersion) {
         ModularTemplateInstance modularTemplateInstance = ((ModularMediaItemVersion) contentItemVersion).getModularTemplateInstance();
         if(modularTemplateInstance != null) {
            TemplateProperty redirectProperty = modularTemplateInstance.getPropertyByIdentifier("redirect_url");
            if(redirectProperty != null) {
               String redirect = redirectProperty.getStringValue();
               return StringUtils.isEmpty(redirect);
            }
         }
      }

      return true;
   } 
} 

SEOGoogleSitemapImageProvider

This implementation adds photos rendered in a media collection element to the sitemap.

public class SitemapImageProviderServiceImpl extends SimpleServiceComponent implements SEOGoogleSitemapImageProvider {
   @Override
   public List<SEOGoogleSitemapImage> getImages(ContentItemVersion<?> contentItemVersion) {
      List<SEOGoogleSitemapImage> images = new ArrayList<>();

      Element[] elements = new Element[0];
      if(contentItemVersion instanceof PageVersion) {
         elements = ((PageVersion) contentItemVersion).getElementHolderElements();
      } else if (contentItemVersion instanceof MediaItemVersion) {
         ElementHolder elementHolder = ((MediaItemVersion) contentItemVersion).getElementHolder();
            if(elementHolder != null) {
               elements = elementHolder.getElements();
            }
      }

      for (Element element : elements) {
         if (element instanceof MediaCollectionElement) {
            images.addAll(getImagesFromMediaCollectionElement((MediaCollectionElement) element));
         }
      }

      return images;
   }

   private List<SEOGoogleSitemapImage> getImagesFromMediaCollectionElement(MediaCollectionElement element) {
      List<SEOGoogleSitemapImage> images = new ArrayList<>();

      Presentation presentation = element.getPresentation();
      if(presentation != null && "mediaCollectionElementPhotoGallery".equals(presentation.getIdentifier())) {
         for(MediaItemVersion mediaItemVersion : element.getMediaItemVersions()) {
            if(mediaItemVersion instanceof ModularMediaItemImageVersion) {
               images.add(createSEOGoogleSitemapImageBean((ModularMediaItemImageVersion) mediaItemVersion));
            }
         }
      }

      return images;
   }

   private SEOGoogleSitemapImage createSEOGoogleSitemapImageBean(ModularMediaItemImageVersion mediaItemVersion) {
      if(mediaItemVersion.getImage() != null) {
         SEOGoogleSitemapImage seoGoogleSitemapImage = createSEOGoogleSitemapImageBean(mediaItemVersion.getImage());
         seoGoogleSitemapImage.setTitle(mediaItemVersion.getTitle());

         ModularTemplateInstance modularTemplateInstance = mediaItemVersion.getModularTemplateInstance();
         if (modularTemplateInstance != null) {
            seoGoogleSitemapImage.setCaption(modularTemplateInstance.getStringValue("caption"));
         }

         return seoGoogleSitemapImage;
     }

     return null;
  }

  private SEOGoogleSitemapImage createSEOGoogleSitemapImageBean(Image image) {
     SEOGoogleSitemapImage seoGoogleSitemapImage = new SEOGoogleSitemapImage();
     seoGoogleSitemapImage.setLoc(image.getAbsoluteUrl());

     return seoGoogleSitemapImage;
  }
} 

SEOGoogleSitemapImageEnrichProvider

This implementation adds a custom caption for the sitemap.

public class SitemapImageProviderServiceImpl extends SimpleServiceComponent implements SEOGoogleSitemapImageEnrichProvider {
   @Override
   public SEOGoogleSitemapImage enrichImage(SEOGoogleSitemapImage seoGoogleSitemapImage, ImageElement imageElement) {
      MediaItem mediaItem = imageElement.getMediaItem();
      if(mediaItem != null) {
         MediaItemVersion mediaItemVersion = mediaItem.getCurrent();
            if(mediaItemVersion instanceof ModularMediaItemImageVersion) {
               seoGoogleSitemapImage = createSEOGoogleSitemapImageBean((ModularMediaItemImageVersion) mediaItemVersion);
            }
         }

         return seoGoogleSitemapImage;
   }

   private SEOGoogleSitemapImage createSEOGoogleSitemapImageBean(ModularMediaItemImageVersion mediaItemVersion) {
      if(mediaItemVersion.getImage() != null) {
         SEOGoogleSitemapImage seoGoogleSitemapImage = createSEOGoogleSitemapImageBean(mediaItemVersion.getImage());
         seoGoogleSitemapImage.setTitle(mediaItemVersion.getTitle());

         ModularTemplateInstance modularTemplateInstance = mediaItemVersion.getModularTemplateInstance();
         if (modularTemplateInstance != null) {
            seoGoogleSitemapImage.setCaption(modularTemplateInstance.getStringValue("caption"));
         }

         return seoGoogleSitemapImage;
      }

      return null;
   }

   private SEOGoogleSitemapImage createSEOGoogleSitemapImageBean(Image image) {
      SEOGoogleSitemapImage seoGoogleSitemapImage = new SEOGoogleSitemapImage();
      seoGoogleSitemapImage.setLoc(image.getAbsoluteUrl());

      return seoGoogleSitemapImage;
   }
} 




Back to top