Home Tutorial Xml User-derived Simple Types

 
 

User-derived Simple Types
Posted on: November 20, 2010 at 12:00 AM
Advertisement
As we use built-in simple types, we can also use our defined simple type by derive a new simple type using the element.
User-derived Simple Types

As we use built-in simple types, we can also use our defined simple type by derive a new simple type using the element.

In below example, "mysimpletype" is your type defined in schema and now this type can also be used as a type in myelm "element".
<?xml version="1.0"?>
 <
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
       <
xs:simpleType name="mysimpletype">
            <
xs:restriction base="xs:string">
                 <
xs:length value="10"/>
            <
/xs:restriction>
       <
/xs:simpleType>

       <
xs:element name="myelm">
            <
xs:complexType>
                 <
xs:sequence>
                      <
xs:element name="mst" type="mysimpletype"/>
                 <
/xs:sequence>
            <
/xs:complexType>
       <
/xs:element>
</xs:schema>


Applying Facets

XML schema offers various data types like string, integer etc but you can do more with datatypes i.e. you can restrict values that are acceptable. Restrictions on XML elements are called facets.

Controlling Length

If you want to restrict the length of a value in the element then use length, maxLength, and minLength constraints.

For example:
The following  element "username" must have the value of 10 characters.
<xs:element name="username">
      <xs:simpleType>
            <xs:restriction base="xs:string">
                   <xs:length value="10"/>
            </xs:restriction>
      </xs:simpleType>
</xs:element>

The following element "username" must have value of minimum 4 characters to maximum 10 characters long.
<xs:element name="username">
    <xs:simpleType>
          <xs:restriction base="xs:string">
                <xs:minLength value="4"/>
                <
xs:maxLength value="10"/>
          </xs:restriction>
    </xs:simpleType>
</xs:element>


Specifying Patterns

We can use pattern constraint to specify which characters and numbers are allowed.
For example:

To allow the value in format like "123-11-1233" the pattern restriction can be implemented as below:
<xs:element name="mynumber">
    <xs:simpleType>
         <
xs:restriction base="xs:string">
              <
xs:length value="11"/>
              <
xs:pattern value="\d{3}\-\d{2}\-\d{4}"/>
         <
/xs:restriction>
    <
/xs:simpleType>
</xs:element>

Working with Numbers

minInclusive and maxInclusive
In the following example, "mynumber" element can have value between 100 to 200 only.
<xs:element name="mynumber">
     <xs:simpleType>
           <xs:restriction base="xs:integer">
                  <xs:minInclusive value="100"/>
                  <
xs:maxInclusive value="200"/>
           </xs:restriction>
     </xs:simpleType>
</xs:element>

Enumerations
If you want to an XML element to have only the value from a set of values, we can use "enumeration" constraint.

For example:
In the following example, mobile element can have value only from the set Nokia, Samsung, Motorola, LG.
<xs:element name="mobile">
      <xs:simpleType>
             <xs:restriction base="xs:string">
                      <xs:enumeration value="Nokia"/>
                      <xs:enumeration value="Samsung"/>
                      <xs:enumeration value="Motorola"/>
                      <xs:enumeration value="LG"/>
             </xs:restriction>
      </xs:simpleType>
</xs:element>


Whitespace-handling

XML schema provides whiteSpace constraint to handle white space characters.

If whiteSpace constraint is set to "preserve", the XML processor does not remove any white space characters.
<xs:element name="mystring">
      <xs:simpleType>
             <xs:restriction base="xs:string">
                    <xs:whiteSpace value="preserve"/>
             </xs:restriction>
      </xs:simpleType>
</xs:element>

If whiteSpace constraint is set to "replace", the XML processor replaces all whitespace characters with spaces.
<xs:element name="mystring">
      <xs:simpleType>
             <xs:restriction base="xs:string">
                    <xs:whiteSpace value="replace"/>
             </xs:restriction>
      </xs:simpleType>
</xs:element>


If whiteSpace constraint is set to "preserve", the XML processor removes all whitespace characters.
<xs:element name="mystring">
      <xs:simpleType>
             <xs:restriction base="xs:string">
                    <xs:whiteSpace value="collapse"/>
             </xs:restriction>
      </xs:simpleType>
</xs:element>

Advertisement

Related Tags for User-derived Simple Types:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 20, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com