67

問題は次のとおりです。

次の XML スニペットがあります。

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

問題は、属性と制限の両方を同時に追加できないことです。属性形式には、分、時間、および秒の値のみを指定できます。時間には制限パターンがあります\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>

timeType の複合型を作成すると、属性を追加できますが、制限は追加できません。単純型を作成すると、制限を追加できますが、属性は追加できません。この問題を回避する方法はありますか。これは非常に奇妙な制限ではありませんか?

4

2 に答える 2

123

属性を追加するには、拡張によって派生する必要があり、ファセットを追加するには、制限によって派生する必要があります。したがって、これは要素の子コンテンツに対して 2 つの手順で行う必要があります。属性はインラインで定義できます。

<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>
于 2009-03-09T14:11:26.857 に答える