( تعداد نمایش : 166 )

بررسی انواع داده در XSD

در این بخش به بررسی انواع داده در XSD  ، خواهیم پرداخت .
تعیین نوع داده  یکی از ویژگی های مهم XSD بوده  که با استفاده از آن میتوان عناصر و یا خصلت ها را با فرمت صحیح و دلخواه اعتبار سنجی نمود . تکنولوژی  XSD ، دامنه گسترده و وسیعی از  انواع داده را حمایت و در عین حال  به کاربر امکان تعریف نوع داده دلخواه را نیز خواهد داد .

انواع داده ساده یا استاندارد
تکنولوژی XSD ، یش از چهل و چهار نوع نوع داده ساده یا استاندارد را حمایت که  بسیاری از آنان بصورت از پیش تعریف شده در XSD تعبیه  و برخی دیگر نیز از تعاریف فوق ، مشتق شده اند . بدین ترتیب کاربران بسادگی  قادر به استفاده از آنان  در توصیف عناصر و یا خصلت های اسناد مورد نظر خود خواهند بود  .
جدول زیر ، نحوه تعریف نوع داده یک عنصر را نشان می دهد ( در بخش های قبل مقالات از آن استفاده شده است ) .

تعریف یک نوع داده در XSD

.
.
.
<XSD:Element Name=”ID”      Type=”XSD:String” MinOccurs=”1″ MaxOccurs=”1″/>
.
.
.

جدول زیر ، انواع داده مهم حمایت شده در XSD  را نشان می دهد .

انواع داده حمایت شده در XSD

نوع داده توضیحات و مثال
anyuri Uniform Resource Locator
Example : http://www.srco.ir
base64Binary MIME STYLE base64 encoded binary data
hexBinary Hexadecimal encoded binary data
boolean

TRUE(1) Or FALSE(0)

byte Numbers between -128 To 127
dateTime Date in a subset of  the ISO8601 format
Time and Timezone are optional
Example: 2003-04-15T10:40:15.000-05:00
date Date in a subset of the ISO8601 format
Example: 2003-04-15
decimal Positive or negative arbitrary precision decimal value
Examples: -5.34,0,5.34,5000.00
double double precision 64-bit floating point
duration Duration of time specified in years , months , days , hours , minutes and seconds format , as defined in the ISO8601 standards extended format PnYnMnDTnHnMnS.nY is then number of years , nM is the number of months and so on .The P is required but the other items are optional
Example:To specify duration of one year and two months you would specify : P1Y2M
float Single precision 32-bit floating point
gDay Day in Gregorian format
Example: –31 every 31th day,regardless of month
gMonth Month in Gregorian format
Example: –06– every may
gMonthDay Month and day in Gregorian format
Example: –07-31 every july 31th
gYear Year in Gregorian format
Example: 2000
gYearMonth Year and month in Gregorian format
Example:2000-02
Int Integer.Sign is optional
Range: -2147483648 to 2147483647
Integer The standard mathematical concept of integer numbers
Range: An infinite set of negative or positive numbers
Language Any valid XML language value as defined by RFC 1766
Example: en-US
Long

Integer
Range: -9223372036854775808 to
9223372036854775807

Name

XML 1.0 name type

NCName

XML namespace ncname (An XML name without the prefix and colon)

negativeInteger

Range : Negative infinity to -1

nonNegativeInteger

Range : 0 to infinity

positiveInteger

Range : 1 to infinity

nonPositiveInteger

Range : Negative infinity to 0

normalizedString

String of character data. Newline,tab and carriage-return characters are converted to spaces before schema processing.

Short

Range : -32768 to 32767

String

String of character data .(characters that match CHAR from XML 1.0)

Time

Time in hh:mm:ss.sss-TimeZone format . Time zone is optional . The time zone is based on the number of hours ahead or behind coordinated universal time (as define in ISO8601)
Example : 11:45:33.20-05:00 where 05:00 means 5 hours behind universal time .

Token

string of character data . Like normalizedstring the newline,tab and carriage-return characters are converted to spaces before schema processing . In addition , adjacent space characters are collapsed to a single space and leading and trailing spaces are removed .

unsignedByte

Unsigned byte .
Range : 0 to 255

unsignedInt

Unsigned integer .
Range : 0 to 4294967295

unsignedlong

Unsigned long integer .
Range : 0 to 18446744073709551615

unsignedShort

Unsigned short integer.
Range : 0 to 65535

QName

XML namespace QName.

NOTATION

XML 1.0 NOTATION attribute type. to retain compatibility between XSD and XML DTD 1.0s. these should only be used with attributes.

NMTOKEN

Name token value. string consisting of one word in a set of letters , digits and other characters in any combination .to retain compatibility between XSD and XML DTD 1.0s. these should only be used with attributes.

NMTOKENS

List of name tokens separated by whitespace. to retain compatibility between XSD and XML DTD 1.0s. these should only be used with attributes.

ID

The ID value must be unique throughtout all elements in the XML document. This attribute is refrenced by other attributes such as idref and idrefs. to retain compatibility between XSD and XML DTD 1.0s. these should only be used with attributes.

IDREF

References the value in an ID attribute within the XML document. to retain compatibility between XSD and XML DTD 1.0s. these should only be used with attributes.

IDREFS

References multiple ID type values separated by whitespace. to retain compatibility between XSD and XML DTD 1.0s. these should only be used with attributes.

ENTITY

XML 1.0 ENTITY attribute type. to retain compatibility between XSD and XML DTD 1.0s. these should only be used with attributes.

ENTITIES

XML 1.0 ENTITIES attribute type. ENTITIES contain a set of ENTITY values.  to retain compatibility between XSD and XML DTD 1.0s. these should only be used with attributes.


ایجاد انواع داده دلخواه
همانگونه که در بخش های قبل این مقاله اشاره گردید ، ، در  XSD  این امکان وجود خواهد داشت  که کاربر با استفاده از انواع داده استاندارد (جدول فوق) و اعمال محدودیت های دلخواه بر روی آنان  ، اقدام به تعریف انواع داده ساده (Simple) دلخواه خود نموده و از آنها در توصیف جاری و یا حتی سایر توصیف ها استفاده نماید .
با توجه به اینکه نحوه تعریف انواع داده دلخواه در مقالات قبل به تفصیل بیان شده است ، در این بخش از مقاله ، از تکرار آن خودداری نموده و صرفا” به تشریح حالات مختلف خلاصه سازی یا محدود سازی اشاره خواهد شد .

پارمترهای محدود سازی (Facets)
خلاصه سازی و یا محدود سازی بستگی مستقیم به نوع داده پایه ای دارد که میخواهیم محدود سازی را بر روی آن اعمال نمائیم . در خصوص داده هائی از نوع رشته‏ ای، پارمترهای مختلف محدود سازی به شرح ذیل می باشند :

  • enumeration
  • length
  • minlength
  • maxlength
  • pattern
  • whitespace

جدول زیر نحوه استفاده از پارامتر الگو(pattern) و اعمال  کنترل های مختلف بمنظور تشریح  الگو را نشان می دهد :

Regular Expression Explanation

Example(s)

\d Digit 1,2,3, etc.
[a-z] Lower case ASCII characters a,b,c, etc.
[A-Z] Upper case ASCII characters A,B,C, etc.
* Wildcard A*Z=ABZ,AbnmZ,ADDFZ, etc.
? Single placeholder A?Z=ABZ,AbZ,ACZ, etc.
+ Inclusive of at least the specified values, but more are allowed . A+Z=AZ,ABZ,ABCZ, etc.
(Value1|Value2) Or (A|Z)+Q=AQ,ZQ,ABQ,ZBQ, etc.
[abcde] Another way to specify or , but with single character only. [abc]=a,b, or c
[^0-9] Any non-digit character A,B,C,a,b,c, etc.
{integer} The number of occurrences that there must be of the previous value . az{2} = azz
\d{3} = 123,456,789,444, etc.
(az){2} = azaz

مثال یک : در مثال زیر نحوه استفاده از پارامتر الگو ، نشان داده شده است :

مثال  یک : نحوه استفاده از پارامتر الگو

<XSD:simpleType name=”StudentGPA”   minOccurs=”1″   maxOccurs=”1″>
<XSD:restriction   base=”XSD:string”>
<XSD:length  value=”4″ />
<XSD:pattern value=”/d{1} . /d{2}” />
</XSD:restriction>
</XSD:simpleType>

مثال دوم : در مثال زیر نحوه استفاده از پارامتر الگو ، نشان داده شده است :

مثال  دو : نحوه استفاده از پارامتر الگو

<XSD:simpleType name=”StudentGPA”   minOccurs=”1″   maxOccurs=”1″>
<XSD:restriction   base=”XSD:string”>
<XSD:enumeration Value = “2.0″ />
<XSD:enumeration Value = “2.5″ />
<XSD:enumeration Value = “3.0″ />
<XSD:enumeration Value = “3.5″ />
<XSD:enumeration Value = “4.0″ />
</XSD:restriction>
</XSD:simpleType>

نکته : در مثال دوم، ارزش های مختلفی  که یک سند برای عنصر StudentGPA می تواند داشته باشد ۲٫۰ یا ۲٫۵ یا ۳٫۰ یا ۳٫۵ یا ۴٫۰ است . اما در مثال اول طول رشته باید چهار حرف بوده و الگوی آن نیز معادل الگوی تعریف شده در pattern باشد . حال سئوال اینجا است که در محدود سازی یک نوع داده ، جایگاه واقعی استفاده از  and و یا or چگونه است ؟. جواب این سئوال پیچیده نیست ، در واقع ، در مواردیکه از خصلت های enumoration و pattern استفاده می گردد ، واقعیت or حکمفرما بوده  و  در صورت استفاده از سایر خصلت ها ، واقعیت and حکم فرما خواهد بود.

در خصوص داده های عددی ، پارامترهای محدود سازی(Facets) به شرح ذیل می باشد :

  • enumeration
  • fractionDigits
  • maxExclusive
  • minExclusive
  • maxInclusive
  • minInclusive
  • pattern
  • totalDigits
  • whiteSpace

مثال : در مثال زیر نحوه استفاده از پارامترهای محدودسازی در خصوص داده های عددی را نشان می دهد :

مثال : نحوه استفاده از پارامترهای محدودسازی در خصوص داده های عددی

<XSD:simpleType name=”courseNumber”   minOccurs=”0″   maxOccurs=”10″>
<XSD:restriction   base=”XSD:integer”>
<XSD:minInclusive  value=”1000″ />
<XSD:maxInclusive value=”3000″ />
</XSD:restriction>
</XSD:simpleType>

در این مثال نوع داده دلخواهی جهت توصیف عنصر شماره درس ایجاد شده است که طبق آن هر دانشجو میتواند تا ۱۰ درس را ثبت نام نموده و شماره هر درس نیز بین ۱۰۰۰ تا ۳۰۰۰ باید باشد . بنابر این در صورتیکه در یک سند XML  که از توصیف فوق استفاده نموده وعنصر course دارای مقدار ۴۰۰۰ و بصورت زیر باشد،پارسر XSD خطائی مبنی بر ایجاد مشکل در نوع داده ارائه و سند اعتبار سنجی نخواهد شد .

<course>4000</course>

دیدگاه خود را بیان کنید.

باید وارد سایت شده باشید برای دیدگاه دادن