【问题标题】:XML Schema: Element with restricted content and attribute with defined valuesXML Schema:内容受限的元素和具有定义值的属性
【发布时间】:2016-05-02 14:45:50
【问题描述】:

所以我有一个元素phone

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

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

<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>

【问题讨论】:

    标签: xml xsd


    【解决方案1】:

    您想要一个“内容简单的复杂类型”。示例见

    XSD : How to use both value and attribute

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      • 1970-01-01
      • 2013-01-10
      相关资源
      最近更新 更多