定义元素的内容类型以及可枚举属性

时间:2008-11-05 10:59:20

标签: xml schema

我无法定义元素的内容类型(“phonenumber”),同时定义具有枚举限制的属性(“location”)。

这就是我被困的地方:

<xs:element name="phonenumbers">
    <xs:complexType>
        <xs:sequence maxOccurs="unbounded">
            <xs:element name="phonenumber">
                <!--
                Try #1:
                Define the type of the attribute "location" and the
                content-type of the element "phonenumber". But without
                enumeration restrictions on "location".
                -->

                <xs:complexType>
                    <xs:simpleContent>
                        <xs:extension base="xs:integer">
                            <xs:attribute name="location" type="xs:string"/>                                                        
                        </xs:extension>                                                    
                    </xs:simpleContent>
                </xs:complexType>

                <!--
                Try #2:
                Enumerables in attribute "location" with no content-type
                of the element "phonenumber", thus being unable to put
                anything in it.
                -->

                <xs:complexType>                                                
                    <xs:attribute name="location">
                        <xs:simpleType>
                            <xs:restriction base="xs:string">
                                <xs:enumeration value="Home"/>
                                <xs:enumeration value="Work"/>
                                <xs:enumeration value="Mobile"/>
                            </xs:restriction>
                        </xs:simpleType>
                    </xs:attribute>
                </xs:complexType>

            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

1 个答案:

答案 0 :(得分:2)

怎么样?
<xs:element name="phonenumbers">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="phonenumber" minOccurs="0" maxOccurs="unbounded">
        <xs:complexType>
          <xs:simpleContent>
            <xs:extension base="xs:integer">
              <xs:attribute name="location" use="required">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:enumeration value="Home"/>
                    <xs:enumeration value="Work"/>
                    <xs:enumeration value="Mobile"/>
                  </xs:restriction>
                </xs:simpleType>
              </xs:attribute>
            </xs:extension>
          </xs:simpleContent>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>