w3pop.com :: ÍøÂçѧԺ :: Schema (XSD) :: XSD ¼òµ¥ÔªËØ
XML Schemas define the elements of your XML files.
XML Schemas¶¨ÒåÁËXMLÎļþµÄÔªËØ¡£
A simple element is an XML element that contains only text. It cannot contain any other elements or attributes.
¼òµ¥ÔªËØÊÇÖ»°üº¬Îı¾µÄXMLÔªËØ¡£Ëü²»¿ÉÒÔÔÙ°üº¬ÆäËüÔªËØ»òÊôÐÔ¡£
A simple element is an XML element that can contain only text. It cannot contain any other elements or attributes.
¼òµ¥ÔªËØÊÇÖ»°üº¬Îı¾µÄXMLÔªËØ¡£Ëü²»¿ÉÒÔÔÙ°üº¬ÆäËüÔªËØ»òÊôÐÔ¡£
However, the "only text" restriction is quite misleading. The text can be of many different types. It can be one of the types included in the XML Schema definition (boolean, string, date, etc.), or it can be a custom type that you can define yourself.
È»¶ø£¬“Ö»°üº¬Îı¾”Õâ¸öÏÞ¶¨Ìõ¼þÊǷdz£ÈÝÒ×ÒýÆðÎó½âµÄ¡£Îı¾¿ÉÒÔ°üº¬¶àÖÖ²»Í¬ÀàÐÍ¡£Ëü¿ÉÒÔÊÇXML Schema¶¨ÒåÖеÄÎı¾ÀàÐÍÖ®Ò»£¨Âß¼Öµ¡¢×Ö·û´®¡¢ÈÕÆÚ£¬µÈµÈ£©£¬Ò²¿ÉÒÔÊÇ×Ô¶¨ÒåÎı¾ÀàÐÍ¡£
You can also add restrictions (facets) to a data type in order to limit its content, or you can require the data to match a specific pattern.
Äã¿ÉÒÔͨ¹ýΪÊý¾ÝÀàÐÍÌí¼ÓÏÞ¶¨Ìõ¼þÀ´ÏÞÖÆËüµÄÄÚÈÝ£»»òÕߣ¬Äã¿ÉÒÔÒªÇóÊý¾ÝÓëÖ¸¶¨µÄÑùʽÏàÆ¥Åä¡£
The syntax for defining a simple element is:
¶¨ÒåÒ»¸ö¼òµ¥ÔªËصÄÓï·¨ÈçÏ£º
<xs:element name="xxx" type="yyy"/> |
where xxx is the name of the element and yyy is the data type of the element.
ÉÏÊö°¸ÀýÖУ¬ xxxÊÇÔªËØÃû³Æ£¬yyyÊÇÔªËØµÄÊý¾ÝÀàÐÍ¡£
XML Schema has a lot of built-in data types. The most common types are:
XML Schema±¾Éí°üº¬¶àÖÖÄÚÖõÄÊý¾ÝÀàÐÍ¡£×î³£¼ûµÄÀàÐÍÈçÏ£º
Here are some XML elements:
ÏÂÃæÁоÙÁËһЩXMLÔªËØ£º
<lastname>Refsnes</lastname> |
And here are the corresponding simple element definitions:
ÏÂÃæÁоÙÁËÏà¶Ô¼òµ¥µÄÔªËØ¶¨Ò壺
<xs:element name="lastname" type="xs:string"/> |
Simple elements may have a default value OR a fixed value specified.
¼òµ¥ÔªËØ¿ÉÒÔ°üº¬Ö¸¶¨µÄĬÈÏÖµ»ò¹Ì¶¨Öµ¡£
A default value is automatically assigned to the element when no other value is specified.
µ±Î´Ö¸¶¨ÆäËüֵʱ£¬ÊôÐÔ»á×Ô¶¯ÉèÖÃĬÈÏÖµ¡£
In the following example the default value is "red":
ÏÂÊö°¸ÀýµÄĬÈÏÖµÊÇ"red"£º
<xs:element name="color" type="xs:string" default="red"/> |
A fixed value is also automatically assigned to the element, and you cannot specify another value.
¹Ì¶¨ÖµÊÇÒ²ÊÇϵͳ×Ô¶¯·ÖÅɸøÊôÐԵġ£²¢ÇÒ£¬Ò»µ©Ö¸¶¨Á˹̶¨Öµ£¬Äã¾Í²»ÄÜÖ¸¶¨ÆäËüÖµÁË¡£
In the following example the fixed value is "red":
ÏÂÊö°¸ÀýµÄ¹Ì¶¨ÖµÊÇ"red"£º
<xs:element name="color" type="xs:string" fixed="red"/> |