How to set restrictions on the type of XML Schema?

I read w3cschools tutorials ( http://www.w3schools.com/schema/schema_complex.asp ), but they don't seem to mention how you could add restrictions on complex types.

Like, for example, I have this circuit.

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

Now I want to make sure that the first name is no more than 10 characters. How to do it?

I tried to introduce a simple type for the first name, but it says that I cannot do this because I am using a complex type.

So, how can I put such restrictions in a file so that the people I give the scheme do not try to make the first name 100 characters.

+3
source share
2 answers

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

, .

+5
<xs:element name="employee">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:maxLength value="10"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

?

+3

Source: https://habr.com/ru/post/1743749/


All Articles