ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: Schema (XSD) :: XSD ÊôÐÔ

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

Schema (XSD)
Schema (XSD) ½éÉ..
ΪºÎʹÓà XML Sch..
ÈçºÎ¶¨ÖÆ XSD
XSD - <schema..
XSD ¼òµ¥ÔªËØ
XSD ÊôÐÔ
XSD Ô¼ÊøÃæ
XSD ¸´ºÏÔªËØ
XSD ¸´ºÏ¿ÕÔªËØ
XSD ¸´ºÏ´¿ÔªËØ
XSD ¸´ºÏ´¿Îı¾
XSD »ìºÏÄÚÈݵĸ´..
XSD ָʾÆ÷¸´ºÏÀà..
XSD <any> ..
XSD <anyAttri..
XSD ÔªËØÌæ´ú
XSD ʵÀý
XSD ×Ö·û´®Êý¾ÝÀà..
XSD ÈÕÆÚÊý¾ÝÀàÐÍ
XSD СÊýÊý¾ÝÀàÐÍ

XSD ÊôÐÔ


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-29 ÐÞ¸Ä:2007-08-27 ä¯ÀÀ:5894 :: ::

All attributes are declared as simple types.
ËùÓеÄÊôÐÔ¶¼ÊÇÒÔ¼òµ¥ÀàÐÍÀ´ÉùÃ÷µÄ¡£


What is an Attribute?
ÊôÐÔÊÇʲô£¿

Simple elements cannot have attributes. If an element has attributes, it is considered to be of a complex type. But the attribute itself is always declared as a simple type.
¼òµ¥ÔªËز»°üº¬ÊôÐÔ¡£µ±ÔªËذüº¬ÊôÐÔʱ£¬Ëü»á±»Åжϳɸ´ºÏÔªËØ¡£µ«ÊôÐÔ±¾Éí×ÜÊÇÒÔ¼òµ¥ÀàÐÍÀ´ÉùÃ÷µÄ¡£


How to Define an Attribute?
ÈçºÎ¶¨ÒåÊôÐÔ£¿

The syntax for defining an attribute is:
¶¨ÒåÒ»¸öÊôÐÔµÄÓï·¨ÈçÏ£º

<xs:attribute name="xxx" type="yyy"/>

where xxx is the name of the attribute and yyy specifies the data type of the attribute.
ÉÏÊö°¸ÀýÖУ¬xxxÊÇÊôÐÔÃû³Æ£¬yyyÖ¸¶¨ÁËÊôÐÔµÄÊý¾ÝÀàÐÍ¡£

XML Schema has a lot of built-in data types. The most common types are:
XML Schema±¾Éí°üº¬¶àÖÖÊý¾ÝÀàÐÍ¡£×î³£¼ûµÄÊý¾ÝÀàÐÍÈçÏ£º

  • xs:string
    xs:×Ö·û´®
  • xs:decimal
    xs:СÊý
  • xs:integer
    xs:ÕûÊý
  • xs:boolean
    xs:Âß¼­Öµ
  • xs:date
    xs:ÈÕÆÚ
  • xs:time
    xs:ʱ¼ä

Example
°¸Àý

Here is an XML element with an attribute:
ÏÂÃæÁоÙÁËÒ»¸ö°üº¬ÊôÐÔµÄXMLÔªËØ£º

<lastname lang="EN">Smith</lastname>

And here is the corresponding attribute definition:
ÏÂÃæÁоÙÁËÏàÓ¦µÄÊôÐÔ¶¨Ò壺

<xs:attribute name="lang" type="xs:string"/>

Default and Fixed Values for Attributes
ÊôÐÔµÄĬÈÏÖµºÍ¹Ì¶¨Öµ

Attributes may have a default value OR a fixed value specified.
ÊôÐÔ¿ÉÒÔ°üº¬Ö¸¶¨µÄĬÈÏÖµ»ò¹Ì¶¨Öµ¡£

A default value is automatically assigned to the attribute when no other value is specified.
µ±Î´Ö¸¶¨ÆäËüֵʱ£¬ÊôÐÔ»á×Ô¶¯ÉèÖÃĬÈÏÖµ¡£

In the following example the default value is "EN":
ÏÂÊö°¸ÀýÖеÄĬÈÏÖµÊÇ"EN"£º

<xs:attribute name="lang" type="xs:string" default="EN"/>

A fixed value is also automatically assigned to the attribute, and you cannot specify another value.
¹Ì¶¨ÖµÊÇÒ²ÊÇϵͳ×Ô¶¯·ÖÅɸøÊôÐԵġ£²¢ÇÒ£¬Ò»µ©Ö¸¶¨Á˹̶¨Öµ£¬Äã¾Í²»ÄÜÖ¸¶¨ÆäËüÖµÁË¡£

In the following example the fixed value is "EN":
ÏÂÊö°¸ÀýÖеĹ̶¨ÖµÊÇ"EN"£º

<xs:attribute name="lang" type="xs:string" fixed="EN"/>

Optional and Required Attributes
¿ÉÑ¡ÊôÐԺͱØÒªÊôÐÔ

Attributes are optional by default. To specify that the attribute is required, use the "use" attribute:
ÊôÐÔÊÇĬÈÏΪ¿ÉÑ¡µÄ¡£ÒªÖ¸Ã÷ÊôÐÔÊDZØÒªµÄ£¬¾Í±ØÐëʹÓÓuse”ÊôÐÔ£º

<xs:attribute name="lang" type="xs:string" use="required"/>

Restrictions on Content
¶ÔÄÚÈݵÄÔ¼Êø

When an XML element or attribute has a data type defined, it puts restrictions on the element's or attribute's content.
µ±XMLÔªËØ»òÊôÐÔ°üº¬ÁËÒѶ¨ÒåµÄÊý¾ÝÀàÐÍ£¬ÔªËØ»òÊôÐÔµÄÄÚÈݾͻáÊܵ½Ô¼Êø¡£

If an XML element is of type "xs:date" and contains a string like "Hello World", the element will not validate.
Èç¹ûÒ»¸öXMLÔªËØÊÇ"xs:date"ÀàÐÍ£¬²¢°üº¬Ò»¸öÖîÈç"Hello World"ÕâÑùµÄ×Ö·û´®£¬ÄÇô¸ÃÔªËØ½«Ê§Ð§¡£

With XML Schemas, you can also add your own restrictions to your XML elements and attributes. These restrictions are called facets. You can read more about facets in the next chapter.
ÄãÒ²¿ÉÒÔʹÓÃXML Schema¸øXMLÔªËØºÍÊôÐÔÌí¼ÓÔ¼ÊøÏÞÖÆ¡£ÕâÐ©Ô¼Êø³ÆÎª“Ô¼ÊøÃæ£¨facet£©”¡£ÔÚÏÂÃæÒ»ÕÂÀÄã¿ÉÒÔѧϰ¸ü¶à¹ØÓÚ“Ô¼ÊøÃæ”µÄÄÚÈÝ¡£

ÆÀÂÛ (0) All