w3pop.com :: ÍøÂçѧԺ :: Schema (XSD) :: XSD ÊôÐÔ
All attributes are declared as simple types.
ËùÓеÄÊôÐÔ¶¼ÊÇÒÔ¼òµ¥ÀàÐÍÀ´ÉùÃ÷µÄ¡£
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.
¼òµ¥ÔªËز»°üº¬ÊôÐÔ¡£µ±ÔªËذüº¬ÊôÐÔʱ£¬Ëü»á±»Åжϳɸ´ºÏÔªËØ¡£µ«ÊôÐÔ±¾Éí×ÜÊÇÒÔ¼òµ¥ÀàÐÍÀ´ÉùÃ÷µÄ¡£
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±¾Éí°üº¬¶àÖÖÊý¾ÝÀàÐÍ¡£×î³£¼ûµÄÊý¾ÝÀàÐÍÈçÏ£º
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"/> |
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"/> |
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"/> |
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