XML Tutorial/XML Schema/length

Материал из Web эксперт
Перейти к: навигация, поиск

A fixed number of units can be specified using the length facet

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru"
  elementFormDefault="qualified">
  <xs:simpleType name="USA_SSN">
    <xs:restriction base="xs:string">
      <xs:length value="11" />
    </xs:restriction>
  </xs:simpleType>
</xs:schema>
Any of the following values would therefore, be valid for an ssn element that uses the USA_SSN datatype: 
<ssn>123-45-6789</ssn>
<ssn>abcdefghijk</ssn>
<ssn>$   2345.42</ssn>


length Defines the number of units of length using a nonnegative integer

<xsd:simpleType name="isbnType">
  <xsd:restriction base="string">
   <xsd:length value="10" fixed="true"/>
  </xsd:restriction>
</xsd:simpleType>


Restricting a type derived from the built-in string type to have a one-character length

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.wbex.ru" 
  xmlns="http://www.wbex.ru"
  elementFormDefault="qualified">
  <xs:simpleType name="Char">
    <xs:restriction base="xs:string">
      <xs:length value="1" />
    </xs:restriction>
  </xs:simpleType>
  
</xs:schema>


Restrict the length of the code string and the characters

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.wbex.ru" 
  xmlns="http://www.wbex.ru"
  elementFormDefault="qualified">
  <xs:simpleType name="iso3166_CountryCode">
    <xs:restriction base="xs:string">
      <xs:length value="3" />
      <xs:pattern value="[A-Za-z0-9]{3}" />
    </xs:restriction>
  </xs:simpleType>
</xs:schema>


Restrict the length of the text node

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru"
  elementFormDefault="qualified">
  <xs:complexType name="tokenWithLangAndNote">
    <xs:simpleContent>
      <xs:extension base="xs:token">
        <xs:attribute name="lang" type="xs:language" />
        <xs:attribute name="note" type="xs:token" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  
  <xs:element name="title">
    <xs:complexType>
      <xs:simpleContent>
        <xs:restriction base="tokenWithLangAndNote">
          <xs:maxLength value="255" />
          <xs:attribute name="lang" type="xs:language" />
          <xs:attributename =" note " type="xs:token" />
        </xs:restriction>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>
</xs:schema>


To specify the exact length of an element

If you specify the length, you cannot specify the maximum or minimum (or vice versa).
The values for xsd:length, xsd:minLength, and xsd:maxLength must all be non-negative integers.
If the element is based on a binary type, the length limits the number of octets of binary data. 
If the element is derived by list, the length determines the number of list items.

File: Schema.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru"
  elementFormDefault="qualified">
  
  <xsd:element name="animal_code">
    <xsd:simpleType>
      <xsd:restriction base="xsd:string">
        <xsd:length value="4" />
      </xsd:restriction>
    </xsd:simpleType>
  </xsd:element>
</xsd:schema>
 
File: Data.xml
<?xml version="1.0"?>
<animal_code xmlns="http://www.wbex.ru">TIGR</animal_code>