views:

2113

answers:

1

The problem is as follows:

I have to following XML snippet:

<time format="minutes">11:60</time>

The problem is that I can't add both the attribute and the restriction at the same time. The attribute format can only have the values minutes, hours and seconds. The time has the restrictionpattern \d{2}:\d{2}

<xs:element name="time" type="timeType"/>
...
<xs:simpleType name="formatType">
<xs:restriction base="xs:string">
 <xs:enumeration value="minutes"/>
 <xs:enumeration value="hours"/>
 <xs:enumeration value="seconds"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="timeType">
 <xs:attribute name="format">
  <xs:simpleType>
   <xs:restriction base="formatType"/>
  </xs:simpleType>
 </xs:attribute>
</xs:complexType>

if I make a complex type of timeType, i can add a attribute, but not the restriction, and if we make a simple type, we can add a restriction bu not add the attribute. Is there any way to get around this problem. It this is not a very strange restriction, or is it?

+4  A: 

To add attributes you have to derive by extension, to add facets you have to derive by restriction. Therefore this has to be done in two steps for the element's child content. The attribute can be defined inline:

<xsd:simpleType name="timeValueType">
  <xsd:restriction base="xsd:token">
    <xsd:pattern value="\d{2}:\d{2}"/>
  </xsd:restriction>
</xsd:simpleType>

<xsd:complexType name="timeType">
  <xsd:simpleContent>
    <xsd:extension base="timeValueType">
      <xsd:attribute name="format">
        <xsd:simpleType>
          <xsd:restriction base="xsd:token">
            <xsd:enumeration value="seconds"/>
            <xsd:enumeration value="minutes"/>
            <xsd:enumeration value="hours"/>
          </xsd:restriction>
        </xsd:simpleType>
      </xsd:attribute>
    </xsd:extension>
  </xsd:simpleContent>
</xsd:complexType>
Richard