What is the best way to represent image nodes in a product (element) XML schema?
I have an XML schema representing a product in a DB and I am trying to find the best way to store product image links as XML nodes. There will be a primary image and then alternating images, each with a sequence (display order). Will this be a suitable format, or are there better approaches:
<item>
<id></id>
<images>
<image width="" height="" href="" alt="" sequence="1" />
<image width="" height="" href="" alt="" sequence="2" />
<image width="" height="" href="" alt="" sequence="3" />
<image width="" height="" href="" alt="" sequence="4" />
</images>
</item>
Obviously there will be more nodes than this, but I am not showing them all. I decided that my main image would always be the first in the sequence. The problem I'm running into is that each of these images will have thumbnails, medium and large images, so I think this needs to be split.
Since XML elements have a natural order (that is, the order in which they appear in the XML file), it is probably redundant to include the attribute sequence
. You can still talk about the order of the elements, and there is still a "first" for the main product image.
So maybe:
<images>
<imageset>
<image size="thumbnail" width="" height="" href="" alt="" />
<image size="medium" width="" height="" href="" alt="" />
<image size="large" width="" height="" href="" alt="" />
</imageset>
<imageset>
<image size="thumbnail" width="" height="" href="" alt="" />
<image size="medium" width="" height="" href="" alt="" />
<image size="large" width="" height="" href="" alt="" />
</imageset>
</images>
source to share
To expand on Greg's design a bit: it might be advisable to size the image with the name of the element rather than making size
an attribute, i.e .:
<imageset>
<thumbnail width=""... />
<medium width="".../>
<large width="".../>
</imageset>
There are two reasons for this.
First, the name imageset
already tells you that its children will be images, so naming the children image
is redundant. It's just as easy to use the XPath pattern imageset/*
as it is to use imageset/image
, and a little easier to write imageset/medium
than write imageset/image[@size='medium']
.
The more important reason is that this construct allows your schema to indicate that an element imageset
should contain exactly one of each image type. (Or indicates that one or more image types are optional.)
source to share