tags:

views:

72

answers:

2

I have an XML schema that represents a product in a DB, and I am trying to figure out the best way to store the product image references as XML nodes. There will be a primary image, and then alternate images, each of them with sequences (display order). Would this be an appropriate 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'm not showing them all. I figured my primary image would always be the first in the sequence. The problem I'm having is that each of these images will have a thumbnail, medium and large images, so I'm thinking this needs to be broken down further.

+2  A: 

Since XML elements have a natural order (that is, the order in which they appear in the XML file), it's probably redundant to include the sequence attribute. You can still talk about the order of the elements and there is still a "first" one for the primary product image.

So perhaps:

<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>
Greg Hewgill
+1  A: 

To extend Greg's design a little: it might be appropriate to make the image size the element name 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 child elements are going to be images, so naming the children image is redundant. It's just as easy to use an XPath pattern of imageset/* as it is to use imageset/image, and it's marginally easier to write imageset/medium than it is to write imageset/image[@size='medium'].

A more important reason is that this design allows your schema to specify that an imageset element must contain exactly one of each type of image. (Or that specifies that one or more image types is optional.)

Robert Rossney