Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 42 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
42
Dung lượng
711,67 KB
Nội dung
314 Essential XML Quick Reference Example Use of documentation A schema annotated with human readable information 9.2.12 element <xs:element abstract='boolean' block='token' default='string' final='token' fixed='string' form='NMTOKEN' id='ID' maxOccurs='union' minOccurs='nonNegativeInteger' name='NCName' nillable='boolean' ref='QName' substitutionGroup='QName' type='QName' > <! annotation complexType key keyref simpleType unique > </xs:element> The element element either denotes an element declaration, defining a named element and associating that element with a type, or it is a reference to such a declaration. Element declarations appearing as children of a schema element are known as global element declarations and can be referenced from else- where in the schema or from other schemas. Element declarations appearing as part of complex type definitions, either directly or through a group reference, are known as local element declarations. Such element declarations are local to the type in which they appear. Global element declarations describe elements that <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='urn:example-org:People' xmlns:tns='urn:example-org:People' > <xs:annotation> <xs:documentation xml:lang='UK-ENG' > This schema is an example in a book. The colour of the book cover is green. </xs:documentation> </xs:annotation> </xs:schema> Skonnard.book Page 314 Monday, October 1, 2001 8:57 AM XML Schema Structures 315 Schema II are always part of the target namespace of the schema. Local element declara- tions describe elements that may be part of the target namespace of the schema depending on the values of the form attribute on the element declaration and the value of the elementFormDefault attribute on the schema element. Attributes Name Type Default Description abstract boolean false Specifies whether the element being declared is abstract. An abstract element may not appear in an instance document; rather, an element in this element’s substitu- tion group must appear instead. block token Specifies what substitution mecha- nisms are prohibited for the element being declared. The value of this attribute can be a list of one or more of extension, restriction, and substitution or #all. The value of this attribute overrides any schemawide default specified by a blockDefault attribute on the schema element. Value Description extension Types derived by extension may not appear in place of this element either through use of xsi:type or substitution groups. restriction Types derived by restriction may not appear in place of this element either through use of xsi:type or substitution groups. substitution Elements in the substitution group for this element may not appear in place of this element. #all All of the above default string None Specifies a default value for an ele- ment whose type is a simple type or a complex type derived from a simple type. The default and fixed attributes are mutually exclusive. Skonnard.book Page 315 Monday, October 1, 2001 8:57 AM 316 Essential XML Quick Reference final token None Specifies which derivation mecha- nisms are prohibited for element declarations that are part of a sub- stitution group with this element declaration as the head. The value of this attribute overrides any sche- mawide default specified by a finalDefault attribute on the schema element. Value Description extension Elements in the substitution group of this element may not be of a type derived by extension, either directly or indirectly, from the type of this element. restriction Elements in the substitution group of this element may not be of a type derived by restriction, either directly or indirectly, from the type of this element. #all Both of the above fixed string None Specifies a fixed value for an element with a type that is a simple type or a complex type derived from a sim- ple type. The fixed and default attributes are mutually exclusive. form NMTOKEN None Specifies whether a local element declaration is qualified (in the target Namespace for the schema) or unqualified (in no namespace). The value of this attribute overrides any schemawide default specified by an elementFormDefault attribute on the schema element. This attribute may not appear on a glo- bal element declaration. Value Description qualified The local name of the element is qualified by the target namespace of the schema. unqualified The element is unqualified. Name Type Default Description Skonnard.book Page 316 Monday, October 1, 2001 8:57 AM XML Schema Structures 317 Schema II id ID None An attribute for application use. maxOccurs Union 1 Specifies the maximum number of times this element may appear in the context in which the declaration appears. The value of this attribute may be any nonNegativeInteger or the string unbounded. This attribute may not appear on a glo- bal element declaration. minOccurs nonNegative- Integer 1 Specifies the minimum number of times this element must appear in the context in which the declaration appears. This attribute may not appear on a global element declaration. name NCName None Specifies the local part of the name of the element being declared. The name and ref attributes are mutu- ally exclusive. nillable boolean false If this attribute is set to true, then the element may have no content, provided it is annotated in the instance document with an xsi:nil attribute with a value of true. ref QName None Specifies a reference to a global element declaration. The refer- enced element declaration may be in the same schema document as the referencing element declaration or it may be in a different schema document, potentially in a different namespace. This attribute may not appear on a global element decla- ration. The ref and name attributes are mutually exclusive. Name Type Default Description Skonnard.book Page 317 Monday, October 1, 2001 8:57 AM 318 Essential XML Quick Reference Child elements Examples A global element declaration substitution- Group QName None Specifies the element that serves as the head of the substitution group to which this element decla- ration belongs. This attribute can only appear on a global element declaration. The referenced ele- ment declaration may be in the same schema document as the ref- erencing element declaration or it may be in a different schema docu- ment, potentially in a different namespace. type QName None Specifies the type of the element being declared. This attribute is a reference to a simple type or a complex type. If the type and ref attributes are both absent, then the element declaration may have a simpleType element or a complexType element as one of its children, in which case the type of the element is that anonymous inline type. If no such children are present, then the type of the element is the anyType in the namespace http://www.w3.org/2001/ XMLSchema . Name Type Default Description Name Occurrence annotation Optional, once simpleType or complexType Optional, once key or keyref or unique Optional, unlimited <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='urn:example-org:People' > Skonnard.book Page 318 Monday, October 1, 2001 8:57 AM XML Schema Structures 319 Schema II A global element declaration for an element with a local name of description and a namespace name of urn:example-org:People. This element is based on the built-in string type. A global element declaration and several local element declarations A global element declaration for an element with a local name of person and a namespace name of urn:example-org:People. This element is based on the Person complex type in the same namespace that has several local element declarations. A document conforming to this schema would have a document ele- ment with a local name of person in the urn:example-org:People namespace. That element would have unqualified name and height children, in that order. The name element would contain a string and the height element would contain a double-precision floating point number. A global element declaration and a reference to the declaration <xs:element name='description' type='xs:string' /> </xs:schema> <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='urn:example-org:People' xmlns:tns='urn:example-org:People' > <xs:element name='Person' type='tns:Person' /> <xs:complexType name='person' > <xs:sequence> <xs:element name='name' type='xs:string' /> <xs:element name='height' type='xs:double' /> </xs:sequence> </xs:complexType> </xs:schema> <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='urn:example-org:Utilities' xmlns:tns='urn:example-org:Utilities' > <xs:element name='height' type='xs:double' /> Skonnard.book Page 319 Monday, October 1, 2001 8:57 AM 320 Essential XML Quick Reference A global element declaration for an element with a local name of height and a namespace name of urn:example-org:Utilities and a reference to that global element declaration. The element is based on the built-in double type. Elements of type HeightVector must have a child element with a local name of height in the namespace urn:example-org:Utilities. 9.2.13 extension <xs:extension base='QName' id='ID' > <! all annotation anyAttribute attribute attributeGroup choice group sequence > </xs:extension> The extension element appears as part of a complex type definition and indi- cates that the complex type is being derived by extension from some base type. The base type may be either a simple type or a complex type. If the base type is a simple type, then the extended type may only add attributes. If the base type is a complex type, the extended type may add extra elements and/or attributes. When extra elements are added, these logically appear after the elements of the base type, the resulting content model being the content model of the base type followed by the content model of the derived type with both parts treated as if they were wrapped in a sequence element. Complex types with all as their top-level compositor cannot be extended by adding particles. Only attributes can be added to such types. Similarly, an all group can only be used to extend a type that has an empty content model. <xs:complexType name='HeightVector' > <xs:sequence> <xs:element ref='tns:height' /> </xs:sequence> </xs:complexType> </xs:schema> Skonnard.book Page 320 Monday, October 1, 2001 8:57 AM XML Schema Structures 321 Schema II Attributes Child elements Examples Extending a base type with a sequence compositor with another sequence Name Type Default Description base QName None Specifies the base type from which the new type is derived. If the parent of the extension element is a simpleContent element, then this attribute must refer to a simple type. If the parent of the extension element is a complexContent element, then this attribute must refer to a complex type. The base type may be in the same schema document as the derived type or it may be in a different schema document, potentially in a different namespace. id ID None An attribute for application use. Name Occurrence annotation Optional, once all or choice or group or sequence Optional, once attribute or attributeGroup Optional, unlimited anyAttribute Optional, once <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='http://example.org/People' xmlns:tns='http://example.org/People' > <xs:complexType name='Person' > <xs:sequence> <xs:element name='name' /> <xs:element name='height' /> </xs:sequence> </xs:complexType> <xs:complexType name='Employee' > <xs:complexContent> <xs:extension base='tns:Person' > <xs:sequence> <xs:element name='salary' /> Skonnard.book Page 321 Monday, October 1, 2001 8:57 AM 322 Essential XML Quick Reference Both types have sequence as their top-level model group. Elements of type Employee must have name, height, and salary children, in that order. They may also have an employeeNumber attribute. Extending a base type with a sequence compositor with a choice The base type has sequence as its top-level model group whereas the derived type has choice. Elements of type MaleFemalePerson must have name and height children, in that order, followed by either a boybits element or a girlbits element. </xs:sequence> <xs:attribute name='employeeNumber' /> </xs:extension> </xs:complexContent> </xs:complexType> </xs:schema> <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='http://example.org/People' xmlns:tns='http://example.org/People' > <xs:complexType name='Person' > <xs:sequence> <xs:element name='name' /> <xs:element name='height' /> </xs:sequence> </xs:complexType> <xs:complexType name='MaleFemalePerson' > <xs:complexContent> <xs:extension base='tns:Person' > <xs:choice> <xs:element name='boybits' /> <xs:element name='girlbits' /> </xs:choice> </xs:extension> </xs:complexContent> </xs:complexType> </xs:schema> Skonnard.book Page 322 Monday, October 1, 2001 8:57 AM XML Schema Structures 323 Schema II Extending a base type with an all compositor The base type has an all compositor. The extended type adds an attribute to the base type. Elements of type Employee must have name and height children, in any order. They may also have an employeeNumber attribute. Extending a simple type <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='http://example.org/People' xmlns:tns='http://example.org/People' > <xs:complexType name='Person' > <xs:all> <xs:element name='name' /> <xs:element name='height' /> </xs:all> </xs:complexType> <xs:complexType name='Employee' > <xs:complexContent> <xs:extension base='tns:Person' > <xs:attribute name='employeeNumber' /> </xs:extension> </xs:complexContent> </xs:complexType> </xs:schema> <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='http://example.org/People' xmlns:tns='http://example.org/People' > <xs:complexType name='Person' > <xs:simpleContent> <xs:extension base='xs:string'> <xs:attribute name='height' /> </xs:extension> </xs:complexType> </xs:schema> Skonnard.book Page 323 Monday, October 1, 2001 8:57 AM [...]... http://www.w3.org /XML/ 199 8/ namespace The import element does not specify a schemaLocation attribute The schema processor will locate a schema for the http:// www.w3.org /XML/ 199 8/namespace by some out-of-band technique Skonnard.book Page 3 29 Monday, October 1, 2001 8:57 AM XML Schema Structures 3 29 Use of import ... name='WeightlessHermaphroditePerson' > Schema II Skonnard.book Page 340 Monday, October 1, 2001 8:57 AM 340 Essential XML Quick Reference Schema II A schema document with no target namespace Skonnard.book Page 344 Monday, October 1, 2001 8:57 AM 344 Essential XML Quick Reference A schema document describing constructs in no namespace The... complex type containing elements and attributes Skonnard.book Page 3 39 Monday, October 1, 2001 8:57 AM XML Schema Structures 3 39 ... namespace In the latter case, the constructs in the imported schema document are available to be referenced through unqualified names Such constructs do not become part of the target namespace of the importing schema document Skonnard.book Page 328 Monday, October 1, 2001 8:57 AM 328 Essential XML Quick Reference Name Type Default Description schemaLocation anyURI None Identifies the location of the . 314 Essential XML Quick Reference Example Use of documentation A schema annotated with human readable information 9. 2.12 element <xs:element abstract='boolean'. name='height' type='xs:double' /> Skonnard.book Page 3 19 Monday, October 1, 2001 8:57 AM 320 Essential XML Quick Reference A global element declaration for an element with a local. AM 328 Essential XML Quick Reference Child element Example Use of import A schema document for the namespace urn:example-org:Base that imports another schema for the namespace http://www.w3.org /XML/ 199 8/ namespace.