XML Schema helps you model (2)

xiaoxiao2021-03-06  45

XML Schema modeling to help you (2) Author: Xiao Chai Road www.ASPCool.com

Composite type definitions, elements, and attributes declarations in XML Schema, for those who have their own content, and composite types that can carry their own properties, with simple types that cannot have element content and attributes, their representations have essentials different. In the instance document, there is a significant difference between the declarations that can create new types (regardless of simple and complex) definitions and allow elements and attributes (whether simple or complex) declarations (whether or not, complex). In this section, the composite type will be defined, and how to declare the elements of the composite type and its properties are described in more detail. In a mode document, when you need to define a new composite type, you should use the ComplexType element to be defined, such typical definitions include element declarations, elements reference, and attribute declarations. These elements declare that they are their own type, as well as the correlation between the element name controlled by the related mode and the type of constraint that controls these elements in the form of the example document. The element is declared by using the ELEMENT element, the attribute is declared by using the Attribute element. For example, USADDRESS is defined as a composite type, so it sees that it contains five elements declarations and a statement that it contains five elements (see Code 3). < / xsd: complexType> This definition is that any type declared in an instance document is an element (such as shiptto in Po.xml) that must contain five elements and an attribute, and these elements must be named Name. STREET, CITY, STATE, and ZIP, these names should be consistent with the values ​​of the Name attribute of the Element element in the mode definition. And these elements must appear in the same order in the pattern declaration, the first four elements must contain a string element content, and the fifth must contain a decimal number type element content. The elements that are declared for the USADRESS type can have a Country property that must contain strings "US". USADDRESS Type Definitions only contain an element declaration that references simple types, including String, Decimal, and NMToken. With the contrast, the PurchaseOrDertype type definition (see Code 4) contains an element declaration that reference composite types, such as USAddress. Both type declarations use the same TYPE attribute to identify type, without having to distinguish between types, simple or composite.

In the type definition of the PurchaseOrDertype, the declaration of two child elements of Shipto and Billto will be the same The composite type is associated, this composite type is USAddress. The result of this definition is that if any elements (such as PO.xml) appearing in the instance document, when the element type is declared as PurchaseOrDertype, then this element must contain two elements called Shipto and Billto, these two Elements must contain five sub-elements (Name, Street, City, State and Zip), which appears as part of the USADRESS statement. According to the related type definition of USAddress, Shipto and Billto elements can also include Country properties. The PURCHASEORDERTYPE type definition contains an ORDERDATE attribute declaration, just like the country property declaration in USAddress, it is identified as a simple type. In fact, all attribute declarations must reference the simple type. This is because attributes are different from the element declaration, and it cannot contain other elements or properties. To date, the elements we describe declare that each element name uses an existing type definition. Sometimes, using an existing element is more convenient than applying a type (see Code 5). code 5 This element declaration definition references an existing element Comment, the element is The other part of the purchase order mode document is defined. In general, the value of the REF attribute must point to a global element. That is to say, it should be declared below the element, rather than as part of the composite type definition. The meaning of code 5 declaration is that a element called Comment can appear in the relevant section of the instance document About this definition, and its content must be consistent with the type of the referenced element, in which case "string". The global element and attribute global elements and global properties are established when the global statement is established, and the global statement appears as the direct child element of the element. Once defined, global elements or global properties can use the REF attribute reference in one or more element / attribute declarations as previously described.

A declaration that references global elements, which allows the referenced elements to be referenced in the instance documentation in elements associated with the declaration. So, for example, the Comment element in PO.XML can also appear in Shipto, BillTo, and Items elements, because the declaration of composite type definitions that reference Comment also appear in the declarations of these three elements. A statement of a global element also allows elements to appear in a top document element in an instance document, so the PurchaseOrder element is declared as a global element as a global element in PO.XML. The top elements in PO.xml appear. It is worth noting that the Comment element based on this basic principle is also allowed in the document such as PO.xml as a top element. There are many restrictions on the use of global elements and attributes, one of which is a global statement that cannot contain references. The global statement definition cannot contain REF properties, they must use the Type property (or followed by an anonymous type definition). The second restriction is that constraints cannot be placed in a global statement, although they can be placed in a local statement that references global statements. In other words, the global statement cannot contain MinoCCurs, MaxocCurs, or USE properties. With regard to the use of constraints, it will be given in the next section. As a result of the number of times, we see that the value of the Minoccurs property is 0 in the element declaration in code 5, so the Comment element is an option in the PurchaseOrDertype type. In general, when the value of the MinoCCURS of an element is 1 or more, the element must appear. The maximum number of elements can appear is determined by the maxoccurs attribute in the statement. This value may be a positive integer such as 1, or a large integer 100, or in the form of "unbounded" to indicate that the number of unlimited appearances. The default value for the Minoccurs and Maxocus properties is 1. Therefore, when an element is as Comment, it does not define the maxoccurs property, then this element cannot be more than once. If only the Minoccurs property value is specified, it must be less than equivalent to the default value of MaxocCurs, that is, if Minoccurs appears separately, the value can only be 0 or 1, because the effective value of MinoCurs must be less than equivalent to Maxoccurs value. Similarly, if only the maxoccurs property is specified, it must be greater than or equal to the default value of Minoccurs, that is, the value must be 1 or more. If both attributes are omitted, the elements must appear and only once. For attributes, it can have two options: once or not, there is no other number of occurrences. Therefore, the syntax of the specified attributes has different symnages of the elements. It is important to note that the attribute statement can use a USE property to indicate whether the attribute needs to appear (see the declaration of the partnum attribute in PO.xsd). The default values ​​of attributes and elements are declared using the default property, but this property has different semantics in different situations. When an attribute is declared using the default value, if the attribute appears in the instance document, the value of the attribute is the value that the attribute appears in the instance document, that is, the value in the instance document is the final valid value. If the property does not appear in the instance document, the mode processor will consider the value of this attribute equal to the value of the default property in the declaration. It should be noted that the attribute default value is only meaningful when the attribute itself is "optional". If in the statement, the default value is specified, but also sets the USE attribute to other values ​​other than "optional" (that is, the appearance), the processor will generate errors. That is, the default value of the attribute is only effective when the property does not appear in the instance document.

转载请注明原文地址:https://www.9cbs.com/read-86213.html

New Post(0)