XML Schema helps you model (2)

xiaoxiao2021-03-06  95

Composite type definition, element and attribute declaration

In XML Schema, for those who have their own content, and composite types that can carry their own properties are different from those that are not capable of elemental content and attributes, their representations have essentially 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).

The meaning of this definition is that any type declared in the instance document is an element (such as Shiptto in Po.xml), 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 sub-elements of Shipto and Billto is associated with the same compound type, which 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 defines a reference to an existing element Comment that is defined in the other part of the purchase order mode document. 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".

Global elements and attributes

The overall element and global attribute are established when the global statement is established, and the global statement is 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.

Audience

We see that the value of the Minoccurs property is 0 in the element declaration in the 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. When there is a default value defined in an element declaration, the mode processor has some differences between the default elements value, compared to the default value of the processing attribute. If the element in the instance document occurs and with its own content, the value of the element is the content of the elements in the instance document. If the element is not content, the mode processor thinks that the value of this element is equal to the value of the default attribute in the declaration. If the element does not appear in the instance document, the mode processor does not think that the element appears at all.

In summary, the difference between the element and the attribute default value can be considered to be applied when the default attribute value is applied when the attribute does not appear; when the element content is empty, the default element value is applied; when the elements do not appear, the default Element values ​​are not applied.

In the property and element declaration, the Fixed property is used to ensure that attributes and elements are set to a special value. As the declaration of a country property is included in Po.xsd, the property declaration has a fixed property, the value is US. This declaration means that the appearance of the country property in the instance document is optional (the default value of the USE property is optional), but if the value appears in the property, it must be "US"; if the attribute does not appear, the mode processor will automatically set Country property value is "US". It should be noted that the concept of fixed values ​​and the concept of default values ​​is mutually exclusive. If the Fixed and Default properties are declared, the Mode processor will generate an error.

It is worth noting that in the global element and attribute statement, Minoccurs, Maxoccurs, but no appearance, no

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

New Post(0)