XML 架构:具有受限内容的元素和具有已定义值的属性

XML Schema: Element with restricted content and attribute with defined values

所以我有一个元素phone

 <phone carrier="A">9991234567</phone>

具有具有已定义值的 carrier 属性。我已经使用 complexType 定义了它。现在,手机的内容也需要有一个限制(正则表达式)。

<xs:element name="phone" type="phoneType" />

<xs:complexType name="phoneType">
    <xs:attribute name="carrier" type="carrierType" />
</xs:complexType>

<xs:simpleType name="carrierType">
    <xs:restriction base="xs:string" >
        <xs:enumeration value="A" />
        <xs:enumeration value="B" />
    </xs:restriction>
</xs:simpleType>

你想要一个 "complex type with simple content"。有关示例,请参见

但在您的情况下,基本类型不是 xs:string,而是来自字符串的一些限制。所以定义你的限制类型,例如

<xs:simpleType name="phoneNr">
  <xs:restriction base="xs:string">
   <xs:pattern value="[0-9]+"/>
  </xs:restriction>
</xs:simpleType>

然后将 carrierType 定义为 phoneNr 的扩展(扩展即属性)。