Total members 11895 |It is currently Sun Dec 22, 2024 6:05 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





To put restrictions on the XML element values or XML attributes.XML elements restriction are called Facets.
1. Enumerations :
Only allow a set of values for your XML element.
Code:
<xs:element name="CustomColors">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:enumeration value="Red"/>
    <xs:enumeration value="Blue"/>
    <xs:enumeration value="Black"/>
  </xs:restriction>
</xs:simpleType>
</xs:element>

This code defines an element "CustomColors" which is of a simpleType. A simpleType is a type that can only contain atomic values, meaning that the value of the element can only be a string, number, date, or boolean.
The code defines a restriction on the base type "xs:string" using the xs:restriction element. The restriction element allows you to limit the possible values that the element can take. Inside the restriction element, there are three xs:enumeration elements, each with a value attribute. The enumeration element is used to define a list of possible values for the element, in this case, "Red", "Blue", and "Black" is the only possible values for the "CustomColors" element. This means that the "CustomColors" element can only take one of these values and no other.




CustomColors values must have a value of these, (Red, Blue, & Black).


2. Patterns :
You limit your value to a series of letters and numbers. For example, is the UUID.

Example on small letters patterns XSD element:

Code:
<xs:element name="small_letters">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:pattern value="[a-z]"/>
  </xs:restriction>
</xs:simpleType>
</xs:element>

This (XSD) code defines an element "small_letters" which is of a simpleType. A simpleType is a type that can only contain atomic values, meaning that the value of the element can only be a string, number, date, or boolean. The code defines a restriction on the base type "xs:string" using the xs:restriction element. The restriction element allows you to limit the possible values that the element can take. Inside the restriction element, there is an xs:pattern element, which contains a value attribute. The xs:pattern element is used to specify a regular expression pattern that the element's value must match. In this case, the regular expression "[a-z]" means that the element can only contain lowercase letters. This means that the element "small_letters" can only take the lowercase letters as its value, any other value will be considered invalid.


Example on numbers only XSD element:
Code:
<xs:element name="numbers_only">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:pattern value="[0-9]"/>
  </xs:restriction>
</xs:simpleType>
</xs:element>


The regular expression "[0-9]" means that the element can only contain numeric values, not lowercase letters.

Universally Unique Identifier (UUID) XSD example:
Code:
     <element name="UUID" minOccurs="1">
               <simpleType>
                  <restriction base="ID">
                     <pattern
                        value="[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}">
                     </pattern>
                  </restriction>
               </simpleType>
            </element>


3. Numbers Range :
You can define the value range of an element. In the following example, the colorindex element can take value between (0-120).
Code:
<xs:element name="colorindex">
<xs:simpleType>
  <xs:restriction base="xs:integer">
    <xs:minInclusive value="0"/>
    <xs:maxInclusive value="120"/>
  </xs:restriction>
</xs:simpleType>
</xs:element>


This code defines an element called "colorindex" with a simple type restriction. The element is of type "xs:integer" and has a minimum value of "0" and a maximum value of "120". This means that the "colorindex" element can only contain an integer value between 0 and 120, inclusive.




4. Whitespaces :


You can leave the white spaces with no changes. preserve means no change.

Code:
<xs:element name="StudentName">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:whiteSpace value="preserve"/>
  </xs:restriction>
</xs:simpleType>
</xs:element>

This code defines an element called "StudentName" with a simple type restriction. The element is of type "xs:string" and has a restriction on white space, specified as "preserve". This means that any white space characters such as spaces, tabs, or line breaks in the value of the "StudentName" element will be preserved when the XML document is parsed rather than being normalized or removed. You can use replace value instead of the preserve. This replaces all space characters like (tabs, line feeds, and carriage return )with spaces.


Code:
<xs:element name="StudentName">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:whiteSpace value="replace"/>
  </xs:restriction>
</xs:simpleType>
</xs:element>

However, in the code above, the restriction on white space is specified as "replace". This means that any white space characters in the value of the "StudentName" element will be replaced by a single space when the XML document is parsed. This can be useful if you want to ensure that the value of the "StudentName" element does not contain any extra white space characters but still want to preserve any spaces that are a part of the value itself. You can use collapse to remove large spaces from one space.

Code:
<xs:element name="StudentName">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:whiteSpace value="collapse
"/>
  </xs:restriction>
</xs:simpleType>
</xs:element>


In the code above, the XSD code, the restriction on white space is specified as "collapse". This means that any consecutive white space characters in the value of the "StudentName" element will be collapsed into a single space when the XML document is parsed. This can be useful if you want to ensure that the value of the "StudentName" element does not contain any extra white space characters and do not want to preserve any spaces that are a part of the value itself.


5. Values lengths:
You can set the length of the element value; your value must equal the length restriction.

Code:
<xs:element name="username">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:length value="8"/>
  </xs:restriction>
</xs:simpleType>
</xs:element>

So, that code defines an element called "username" with a simple type restriction. The element is of type "xs:string" and has a restriction on its length, specified as "8". This means that the value of the "username" element can only be a string that has a length of 8 characters. You can define It in another way, minLength and manLength values.

Code:
<xs:element name="password">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:minLength value="5"/>
    <xs:maxLength value="8"/>
  </xs:restriction>
</xs:simpleType>
</xs:element> 


This code is different from the previous XSD code shown. The previous code only defines one restriction on the length of the "username" element, which is exactly eight characters. In this code, there are two restrictions on the length of the "password" element: a minimum length of 5 characters and a maximum length of 8 characters. This means that the value of the "password" element must be at least five characters long but can't be more than eight characters long.



_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : lesson4:XSD Restrictions
 restrictions on the values of each case of a switch     -  



Topic Tags

XSD Elements
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com