如何对XML Schema Complex类型进行限制?

cho*_*bo2 3 xml xsd

我正在阅读w3cschools(http://www.w3schools.com/schema/schema_complex.asp)上的教程,但他们似乎没有提到如何在复杂类型上添加限制.

比如我有这个架构.

<xs:element name="employee">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
Run Code Online (Sandbox Code Playgroud)

现在我想确保名字长度不超过10个字符.我该怎么做呢?

我试图为firstname添加简单类型,但它说我不能这样做,因为我使用的是复杂类型.

那么我如何在文件中加上这样的限制,以便我给模式的人不要尝试将firstname设为100个字符.

Pab*_*ruz 5

您可以使用XSD 进行一些限制:

假设您想要firstName不超过10个字符.您将使用以下内容:

<xs:element name="employee">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
            <xs:maxLength value="10"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
Run Code Online (Sandbox Code Playgroud)

对于更复杂的约束,我猜你必须做一些基于代码的检查.