XML Schema element ÔªËØ

¶¨ÒåºÍÓ÷¨

element ÔªËØ¶¨ÒåÒ»¸öÔªËØ¡£

ÔªËØÐÅÏ¢

³öÏÖ´ÎÊý Ôڼܹ¹Öж¨ÒåµÄÔªËØµÄÊýÄ¿¡£
¸¸ÔªËØ schema¡¢choice¡¢all¡¢sequence
ÄÚÈÝ simpleType¡¢complexType¡¢key¡¢keyref¡¢unique

Óï·¨

<element
id=ID 
name=NCName
ref=QName
type=QName
substitutionGroup=QName
default=string
fixed=string
form=qualified|unqualified
maxOccurs=nonNegativeInteger|unbounded
minOccurs=nonNegativeInteger
nillable=true|false
abstract=true|false 
block=(#all|list of (extension|restriction))
final=(#all|list of (extension|restriction))
any attributes
>

annotation?,((simpleType|complexType)?,(unique|key|keyref)*))

</element>

£¨? ·ûºÅÉùÃ÷ÔÚ element ÔªËØÖУ¬¸ÃÔªËØ¿É³öÏÖÁã´Î»òÒ»´Î£¬* ·ûºÅÉùÃ÷ÔªËØ¿É³öÏÖÁã´Î»ò¶à´Î¡££©

ÊôÐÔ

id

¿ÉÑ¡¡£¹æ¶¨¸ÃÔªËØµÄΨһµÄ ID¡£

name

¿ÉÑ¡¡£¹æ¶¨ÔªËصÄÃû³Æ¡£Èç¹û¸¸ÔªËØÊÇ schema ÔªËØ£¬Ôò´ËÊôÐÔÊDZØÐèµÄ¡£

ref

¿ÉÑ¡¡£¶ÔÁíÒ»¸öÔªËØµÄÒýÓá£ref ÊôÐԿɰüº¬Ò»¸öÃüÃû¿Õ¼äǰ׺¡£Èç¹û¸¸ÔªËØÊÇ schema ÔªËØ£¬Ôò²»ÊÇʹÓøÃÊôÐÔ¡£

type

¿ÉÑ¡¡£¹æ¶¨ÄÚ½¨Êý¾ÝÀàÐ͵ÄÃû³Æ£¬»òÕ߹涨 simpleType »ò complexType ÔªËØµÄÃû³Æ¡£

substitutionGroup

¿ÉÑ¡¡£¹æ¶¨¿ÉÓÃÀ´Ìæ´ú¸ÃÔªËØµÄÔªËØµÄÃû³Æ¡£ ¸ÃÔªËØ±ØÐë¾ßÓÐÏàͬµÄÀàÐÍ»ò´ÓÖ¸¶¨ÔªËØÀàÐÍÅÉÉúµÄÀàÐÍ¡£

Èç¹û¸¸ÔªËز»ÊÇ schema ÔªËØ£¬Ôò²»¿ÉÒÔʹÓøÃÊôÐÔ¡£

default

¿ÉÑ¡¡£ÎªÔªËع涨ĬÈÏÖµ£¨½öµ±ÔªËØÄÚÈÝÊǼòµ¥ÀàÐÍ»ò textOnly ʱʹÓã©¡£

fixed

¿ÉÑ¡¡£ÎªÔªËع涨¹Ì¶¨Öµ£¨½öµ±ÔªËØÄÚÈÝÊǼòµ¥ÀàÐÍ»ò textOnly ʱʹÓã©¡£

form

¿ÉÑ¡¡£¸ÃÔªËØµÄÐÎʽ¡£ ĬÈÏÖµÊǰüº¬¸ÃÊôÐ﵀ schema ÔªËØµÄ elementFormDefault ÊôÐÔµÄÖµ¡£ ¸ÃÖµ±ØÐëÊÇÏÂÁÐ×Ö·û´®Ö®Ò»£º ¡°qualified¡± »ò ¡°unqualified¡±¡£

Èç¹û¸¸ÔªËØÊÇ schema ÔªËØ£¬Ôò²»ÄÜʹÓøÃÊôÐÔ¡£

  • Èç¹û¸ÃÖµÊÇ ¡°unqualified¡±£¬ÔòÎÞÐëͨ¹ýÃüÃû¿Õ¼äǰ׺ÏÞ¶¨¸ÃÔªËØ¡£
  • Èç¹û¸ÃÖµÊÇ ¡°qualified¡±£¬Ôò±ØÐëͨ¹ýÃüÃû¿Õ¼äǰ׺ÏÞ¶¨¸ÃÔªËØ¡£

maxOccurs

¿ÉÑ¡¡£¹æ¶¨ element ÔªËØÔÚ¸¸ÔªËØÖпɳöÏÖµÄ×î´ó´ÎÊý¡£¸ÃÖµ¿ÉÒÔÊÇ´óÓÚ»òµÈÓÚÁãµÄÕûÊý¡£Èô²»Ïë¶Ô×î´ó´ÎÊýÉèÖÃÈκÎÏÞÖÆ£¬ÇëʹÓÃ×Ö·û´® "unbounded"¡£ ĬÈÏֵΪ 1¡£

Èç¹û¸¸ÔªËØÊÇ schema ÔªËØ£¬Ôò²»ÄÜʹÓøÃÊôÐÔ¡£

minOccurs

¿ÉÑ¡¡£¹æ¶¨ element ÔªËØÔÚ¸¸ÔªËØÖпɳöÏÖµÄ×îС´ÎÊý¡£¸ÃÖµ¿ÉÒÔÊÇ´óÓÚ»òµÈÓÚÁãµÄÕûÊý¡£Ä¬ÈÏֵΪ 1¡£

Èç¹û¸¸ÔªËØÊÇ schema ÔªËØ£¬Ôò²»ÄÜʹÓøÃÊôÐÔ¡£

nillable

¿ÉÑ¡¡£Ö¸Ê¾ÊÇ·ñ¿ÉÒÔ½«ÏÔʽµÄÁãÖµ·ÖÅ䏸¸ÃÔªËØ¡£´ËÏîÓ¦ÓÃÓÚÔªËØÄÚÈݲ¢ÇÒ²»ÊǸÃÔªËØµÄÊôÐÔ¡£ ĬÈÏֵΪ false¡£

Èç¹û nillable Ϊ true£¬½«Ê¹¸ÃÔªËØµÄʵÀý¿ÉÒÔ½« nil ÊôÐÔÉèÖÃΪ true¡£ nil ÊôÐÔ±»¶¨ÒåΪʵÀýµÄ XML ¼Ü¹¹ÃüÃû¿Õ¼äµÄÒ»²¿·Ö¡£

ÀýÈ磬϶ζ¨ÒåÁ˵¥¸öÔªËØ£¬Í¬Ê±½« nillable ÉèÖÃΪ true¡£

<xs:element name="myDate" type="xs:date" nillable="true"/>

϶ÎʹÓøÃÔªËØ²¢¾ßÓÐÏÔʽÁãÖµ£¨nil ÊôÐÔÉèÖÃΪ true£©¡£

<myDate xsi:nil="true"></myDate>

abstract

¿ÉÑ¡¡£Ö¸Ê¾ÔªËØÊÇ·ñ¿ÉÒÔÔÚʵÀýÎĵµÖÐʹÓá£Èç¹û¸ÃֵΪ true£¬ÔòÔªËØ²»ÄܳöÏÖÔÚʵÀýÎĵµÖС£ Ïà·´£¬substitutionGroup ÊôÐÔ°üº¬¸ÃÔªËØµÄÏÞ¶¨Ãû (QName) µÄÆäËûÔªËØ±ØÐë³öÏÖÔÚ¸ÃÔªËØµÄλÖ᣶à¸öÔªËØ¿ÉÒÔÔÚÆä substitutionGroup ÊôÐÔÖÐÒýÓøÃÔªËØ¡£Ä¬ÈÏÖµÊÇ false¡£

block

¿ÉÑ¡¡£ÅÉÉúµÄÀàÐÍ¡£ block ÊôÐÔ·ÀÖ¹¾ßÓÐÖ¸¶¨ÅÉÉúÀàÐ͵ÄÔªËØ±»ÓÃÓÚÌæ´ú¸ÃÔªËØ¡£¸ÃÖµ¿ÉÒÔ°üº¬ #all »òÕßÒ»¸öÁÐ±í£¬¸ÃÁбíÊÇ extension¡¢restriction »ò substitution µÄ×Ó¼¯£º

  • extension - ·Àֹͨ¹ýÀ©Õ¹ÅÉÉúµÄÔªËØ±»ÓÃÀ´Ìæ´ú¸ÃÔªËØ¡£
  • restriction - ·Àֹͨ¹ýÏÞÖÆÅÉÉúµÄÔªËØ±»ÓÃÀ´Ìæ´ú¸ÃÔªËØ¡£
  • substitution - ·Àֹͨ¹ýÌæ»»ÅÉÉúµÄÔªËØ±»ÓÃÀ´Ìæ´ú¸ÃÔªËØ¡£
  • #all - ·ÀÖ¹ËùÓÐÅÉÉúµÄÔªËØ±»ÓÃÀ´Ìæ´ú¸ÃÔªËØ¡£

final

¿ÉÑ¡¡£ÉèÖà element ÔªËØÉÏ final ÊôÐÔµÄĬÈÏÖµ¡£Èç¹û¸¸ÔªËز»ÊÇ schema ÔªËØ£¬Ôò²»ÄÜʹÓøÃÊôÐÔ¡£¸ÃÖµ¿ÉÒÔ°üº¬ #all »òÕßÒ»¸öÁÐ±í£¬¸ÃÁбíÊÇ extension »ò restriction µÄ×Ó¼¯£º

  • extension - ·Àֹͨ¹ýÀ©Õ¹ÅÉÉúµÄÔªËØ±»ÓÃÀ´Ìæ´ú¸ÃÔªËØ
  • restriction - ·Àֹͨ¹ýÏÞÖÆÅÉÉúµÄÔªËØ±»ÓÃÀ´Ìæ´ú¸ÃÔªËØ
  • #all - ·ÀÖ¹ËùÓÐÅÉÉúµÄÔªËØ±»ÓÃÀ´Ìæ´ú¸ÃÔªËØ

any attributes

¿ÉÑ¡¡£¹æ¶¨´øÓÐ non-schema ÃüÃû¿Õ¼äµÄÈÎºÎÆäËûÊôÐÔ¡£

ʵÀý

Àý×Ó 1

ÏÂÃæµÄÀý×ÓÊÇÒ»¸öschema£¬ÆäÖдøÓÐËĸö¼òµ¥ÔªËØ£º"fname", "lname", "age" ÒÔ¼° "dateborn"£¬ÀàÐÍÊÇ string¡¢nonNegativeInteger ÒÔ¼° date£º

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="fname" type="xs:string"/>
<xs:element name="lname" type="xs:string"/>
<xs:element name="age" type="xs:nonNegativeInteger"/>
<xs:element name="dateborn" type="xs:date"/>

</xs:schema>

Àý×Ó 2

ÏÂÃæµÄÀý×ÓÊÇÒ»¸ö´øÓи´ÔÓÀàÐÍ "note" ÔªËØµÄ schema¡£"note" ÔªËØ°üº¬Ëĸö¼òµ¥ÔªËØ£º"to", "from", "heading" ÒÔ¼° "body":

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="note">
    <xs:complexType>
      <xs:sequence>
	<xs:element name="to" type="xs:string"/>
	<xs:element name="from" type="xs:string"/>
	<xs:element name="heading" type="xs:string"/>
	<xs:element name="body" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
</xs:element>

</xs:schema>

Àý×Ó 3

±¾ÀýÓëÀý×Ó 2 Ïàͬ£¬µ«ÊÇÔÚ´ËÀýÖУ¬ÎÒÃÇÑ¡ÔñʹÓà ref ÊôÐÔÀ´ÒýÓÃÔªËØÃû³Æ£º

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="note">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="to"/>
      <xs:element ref="from"/>
      <xs:element ref="heading"/>
      <xs:element ref="body"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>

</xs:schema>
VUE