XML product feed code structure with product data elements

A spreadsheet is fine when you have twenty products. Once you have hundreds, with sizes, colours and prices in more than one currency, most stores end up with an XML feed. Google's own guidance says XML is better suited to multiple variants, nested attributes and automated exports than a flat file, though it takes more technical know-how [1].

This guide explains how a Google-style XML feed is put together, the mistakes that break it, and how to generate one automatically so it never goes out of date.

Which formats the platforms accept

Google Merchant Center accepts product data as tab-delimited text or as XML in either RSS 2.0 or Atom 1.0, and files can be up to 4 GB [1]. Meta's catalogue feeds accept CSV, TSV, RSS XML, Atom XML or Google Sheets [2]. Because both understand the same RSS 2.0 structure, it's the format most feed tools generate.

What a Google RSS feed looks like

An RSS feed is a <channel> containing one <item> per product. Google adds its own namespace on top of standard RSS so it can carry product fields like price and availability:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">
  <channel>
    <title>Your Store</title>
    <link>https://www.yourstore.com</link>
    <description>Product feed for Your Store</description>
    <item>
      <title>Nike Air Max 90, White, Size 10</title>
      <link>https://www.yourstore.com/nike-air-max-90-white</link>
      <description>Leather and mesh upper with a visible Air unit in the heel.</description>
      <g:id>NIKE-AM90-WHT-10</g:id>
      <g:condition>new</g:condition>
      <g:price>129.99 USD</g:price>
      <g:availability>in_stock</g:availability>
      <g:image_link>https://www.yourstore.com/images/nike-am90.jpg</g:image_link>
      <g:brand>Nike</g:brand>
      <g:gtin>012345678905</g:gtin>
    </item>
  </channel>
</rss>

Two details from Google's RSS specification trip people up [3]:

The fields that matter most

FieldElementWhat to know
IDg:idUnique and stable. Changing it makes the platform treat the item as a new product.
TitletitleUp to 150 characters on Google; shoppers usually see the first 70 or so [4].
DescriptiondescriptionUp to 5,000 characters; key details in the first 160 to 500 [5].
LinklinkThe product's own page, not a category page.
Imageg:image_linkAt least 500 × 500 pixels; about 1500 × 1500 recommended [6].
Priceg:priceNumber, space, ISO currency code: 29.99 USD.
Availabilityg:availabilityMust match what the product page shows.
GTINg:gtinRecommended for any product that has one [7].

Variants: one item per size and colour

Each variant is its own <item> with its own ID, and the variants share a g:item_group_id so the platform knows they belong together:

<item>
  <g:id>SHIRT-BLU-S</g:id>
  <g:item_group_id>SHIRT-001</g:item_group_id>
  <title>Classic Cotton Shirt, Blue, Small</title>
  <g:color>Blue</g:color>
  <g:size>S</g:size>
</item>
<item>
  <g:id>SHIRT-BLU-M</g:id>
  <g:item_group_id>SHIRT-001</g:item_group_id>
  <title>Classic Cotton Shirt, Blue, Medium</title>
  <g:color>Blue</g:color>
  <g:size>M</g:size>
</item>

Mistakes that break XML feeds

Unescaped special characters

This is the classic one. A product called "Fish & Chips Tray" will break a feed that writes it out as-is. The XML specification says the ampersand and the less-than sign must be escaped as &amp; and &lt; when they appear in text [8]. CDATA sections are the alternative for longer blocks of text that contain these characters. Generating the feed with a proper XML library, rather than gluing strings together, avoids the problem entirely.

Wrong or mixed encoding

Use UTF-8 and declare it in the first line. Every XML processor is required to accept UTF-8 [8], so it's the safe default, and it handles accented characters and currency symbols correctly.

HTML in titles and descriptions

Google asks you not to use HTML tags in titles, and Meta wants descriptions as plain text [2][4]. Strip formatting out before it goes into the feed.

IDs that change

If an ID changes, the platform sees a brand-new product and the old one's history is lost. Base IDs on something permanent, like a SKU or a database ID.

Files the platform can't fetch

For Google scheduled fetches, the file has to be smaller than 4 GB, the URL must point at the feed file itself rather than an HTML page, and the "Googlebot" and "AdsBot-Google" user agents must be allowed to reach it [9].

Automating it

The point of XML is that a machine can produce it, so let one. The usual setup is to generate the feed from your product database and serve it at a URL that the platforms fetch on a schedule. Google's scheduled fetches run daily, weekly or monthly [9]; Meta's can run as often as hourly [10]. Whatever schedule you pick, refresh at least every 30 days, because Google removes products that haven't been updated in that time [11].

Before each change goes live, check that the XML is well-formed, that every item has its required fields, and that image and product links return a working page.

This is the job ShopFeed does. It builds an RSS 2.0 feed with the Google namespace from your product catalogue, escapes everything properly, and serves it at a fixed URL. When you edit a product, the cached copy of the feed is cleared, so the next fetch picks up the change.

Summary

Sources

  1. Google Merchant Center Help, "File formats for product data". support.google.com
  2. Meta for Developers, "Catalog Reference" and "Schedule Data Feed Uploads". developers.facebook.com
  3. Google Merchant Center Help, "RSS 2.0 specification". support.google.com
  4. Google Merchant Center Help, "Title [title]". support.google.com
  5. Google Merchant Center Help, "Description [description]". support.google.com
  6. Google Merchant Center Help, "Image link [image_link]". support.google.com
  7. Google Merchant Center Help, "GTIN [gtin]". support.google.com
  8. W3C, "Extensible Markup Language (XML) 1.0", sections 2.2, 2.4 and 2.7. w3.org
  9. Google Merchant Center Help, "Update your product data source on a schedule". support.google.com
  10. Meta for Developers, "Schedule Data Feed Uploads". developers.facebook.com
  11. Google Merchant Center Help, "Expiration date [expiration_date]". support.google.com