在Xml Schema中将属性添加到simpletype或限制为complextype

Ikk*_*kke 64 xsd restriction

问题如下:

我有以下XML片段:

<time format="minutes">11:60</time>
Run Code Online (Sandbox Code Playgroud)

问题是我无法同时添加属性和限制.属性格式只能包含分钟,小时和秒.时间有限制模式\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>
Run Code Online (Sandbox Code Playgroud)

如果我创建一个复杂类型的timeType,我可以添加一个属性,但不能添加限制,如果我创建一个简单类型,我可以添加限制但不添加属性.有没有办法解决这个问题.这不是一个非常奇怪的限制,或者是它?

Ric*_*ard 117

要添加必须通过扩展派生的属性,要添加必须通过限制派生的构面.因此,必须分两步完成元素的子内容.该属性可以内联定义:

<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>
Run Code Online (Sandbox Code Playgroud)