views:

177

answers:

0

I need an XML schema for Albums/Photos/Tags where Photos & Tags have many-many relationships. I'd like to have a generalized xml schema solution for the following:

  • 1 container (Album) of PhotoType elements, and
  • 1 container (Tags) of TagType elements.
  • 1 instance document with only a single occurance of Photo/Tag element data

But how should I have the PhotoType reference Tags and TagType reference Photos?

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio - 30 Day Trial Edition 7.1.6.1440 (http://www.liquid-technologies.com)--&gt;
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;

<!-- PhotoType, Album Element  -->
  <xs:complexType name="PhotoType">
    <xs:sequence minOccurs="0">
      <xs:element maxOccurs="unbounded" name="Tags" type="TagREFContainer" />
    </xs:sequence>
    <xs:attribute name="id" type="xs:ID" use="required" />
  </xs:complexType>

  <xs:element name="Album" type="PhotoContainer" />

<!-- Tag Type/Tags Element  -->
  <xs:complexType name="TagType">
    <xs:sequence>
      <xs:element name="Photos">
        <xs:complexType>
          <xs:complexContent mixed="false">
            <xs:extension base="PhotoREFContainer" />
          </xs:complexContent>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="id" type="xs:ID" use="required" />
    <xs:attribute name="Label" use="required" />
  </xs:complexType>

  <xs:element name="Tags" type="TagContainer" />

Q: Is the use of Photo/Tag Container types good practice?

  <xs:complexType name="PhotoContainer">
    <xs:sequence>
      <xs:element maxOccurs="unbounded" name="Photo" type="PhotoType" />
    </xs:sequence>
    <xs:attribute name="id" type="xs:ID" use="required" />
  </xs:complexType>
  <xs:complexType name="TagContainer">
    <xs:sequence>
      <xs:element maxOccurs="unbounded" name="Tag" type="TagType" />
    </xs:sequence>
    <xs:attribute name="id" type="xs:ID" use="required" />
  </xs:complexType>

Q: Can I use type="xs:IDREF" to avoid duplicating Photo/Tag elements in my instance document?

  <xs:complexType name="PhotoREFContainer">
    <xs:sequence>
      <xs:element maxOccurs="unbounded" name="PhotoREF">
        <xs:complexType>
          <xs:attribute name="idref" type="xs:IDREF" />
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="TagREFContainer">
    <xs:sequence>
      <xs:element maxOccurs="unbounded" name="TagREF">
        <xs:complexType>
          <xs:attribute name="idref" type="xs:IDREF" />
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

Q: If this is good practice, is there a generalized way to model ElementContainer & ElementREFContainer types which allow me to: - limit the Container to a certain Type of Element (i.e. Photo or Tag)? - choose whether the Container should contain either Elements or ElementREFs?

Q: Is there a better way to do this?

Many thanks.