An attribute is extra data written directly inside an element's opening tag, in the form name="value". While the element holds the main data, the attribute describes it in more detail – for example a gift's identifier or a parameter's unit. In product feeds attributes are less common than elements, but some formats rely on them.
What is an XML attribute
If an element is a drawer with a label, an attribute is a small note stuck on the label. It doesn't hold the main data but says something extra about it. It is written inside the opening tag: <GIFT ID="DAR-01">. Before the = sign is the attribute name, after it the value in quotation marks.
One element can have several attributes, but each with a different name. An attribute cannot contain further nested data – it is always just a short piece of text.
What it looks like in a feed
<!-- Heureka: a gift with its own ID in an attribute -->
<GIFT ID="DAR-01">Free head torch</GIFT>
<!-- Ceneo: the main offer data is in attributes -->
<o id="STAN-204" price="129.90" url="https://www.sortyra.com/horal-trek-2-tent">
<name>Horal Trek 2 tent</name>
</o>In the first line the gift is the element's text and its code is an attribute; the Polish comparison site Ceneo stores the code, price and link directly in attributes of the o tag.
What it is used for
- The feed header –
version="2.0"andxmlns:g="…"in the Google feed wrapper are attributes. - Additions to a value – a gift code, a parameter's unit, the language of a text.
- Whole offers – some comparison sites and availability feeds (for example
<item id="STAN-204">in Heureka's availability feed) carry the identifier in an attribute.
What to watch out for
- The value always in quotation marks.
id=STAN-204without quotes is an error and the file won't be read. - Special characters. You can't use CDATA in an attribute, so
&and"must be written as&and"– typically in links with parameters. - Data hidden in an attribute. If a supplier puts the price in an attribute, you won't find it in the element's text when mapping – you need to pick the attribute itself.
- The same attribute twice in one element is not allowed in XML.
How Sortyra helps
When loading an XML source, Sortyra also takes attributes as separate fields (marked with @, for example @id), so in Mapping you assign them just like elements. Parameters written as <param name="Colour">red</param> are recognised as a name–value pair.
Related terms
- Element (tag)
- XML
- CDATA
- Field mapping
- More detail: Feed types
Frequently asked questions
What is the difference between an element and an attribute?
An element has an opening and a closing tag and can contain text as well as other elements. An attribute is a short piece of data in the form name="value" inside the opening tag.
Why does Google talk about attributes when the feed has elements?
That is Google's name for product properties, regardless of how they are written. In an XML feed for Google, these "attributes" are written as elements, for example <g:price>.