如何在xml架构中设计多个选择元素?

时间:2013-07-26 20:01:25

标签: xml xsd

例如,我想创建一个包含3个选项的元素TitleMr.Mrs.Miss,以便用户只能选择其中之一。我怎样才能做到这一点? 这是这样的:

<xs:complexType name="Title">
    <xs:sequence>
        <xs:choice maxOccurs="unbounded" minOccurs="0">
        Mr.
        </xs:choice>
        <xs:choice maxOccurs="unbounded" minOccurs="0">
        Mrs.
        </xs:choice>
        <xs:choice maxOccurs="unbounded" minOccurs="0">
        Miss
        </xs:choice>
   </xs:sequence>
</xs:complexType>

1 个答案:

答案 0 :(得分:2)

尝试使用xs:enumeration元素。例如,此架构限制为具有单个元素“Title”且其中包含“Mr”或“Ms”的文档:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XMLSchema1" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Title" type="Title"/>
  <xs:simpleType name="Title">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Mr"/>
      <xs:enumeration value="Ms"/>
    </xs:restriction>  
  </xs:simpleType>
</xs:schema>