XML Schema learning notes

Source: Internet
Author: User

XML SchemaStudy Notes

1The most fundamental difference between a complex type and a simple type is that the content of a complex type can contain other elements or attributes (Attribute),However, a simple type can neither contain child elements nor have any attributes.

<XSD: complextype name = "cnaddress">

<XSD: sequence>

<XSD: element name = "name"Type = "XSD: string"/>

<XSD: element name = "street" type = "XSD: string"/>

<XSD: element name = "city"Type = "XSD: string"/>

<XSD: element name = "Zip"Type = "XSD: decimal"/>

</XSD: sequence>

<XSD: attribute name = "country" type = "XSD: nmtoken" fixed = "us"/>

</XSD: complextype>

 

2,ElementConstraints:ElementYou can useMinoccursAndMaxoccursTwo attributes are used to restrict the number of element instances. The default values of these two attributes are1By default, this element isXMLThe instance document must appear once.

3,AttributeConstraint: the element attribute can also be passed throughAttributeOfUseAttribute;UseThe attribute value can beRequired, optional, prohibitedThree values. The default value isOptional.

4,ElementAndAttributeEach hasDefaultAndFixedAttribute,ElementLaishu, only whenElementThis parameter is used when the instance is empty.DefaultValue, whileAttributeIs when the instance does not provide thisAttributeThis is used only whenDefaultValue, soAttributeOnlyUseThe value isOptionalHourDefaultValue is meaningful, andElementAndAttributeFor exampleFixedAndDefaultThe two attributes cannot exist at the same time; otherwise, an error occurs.

5, Directly defined inSchemaElement, that isSchemaTop-level child elementElementAndAttributeThey are all global. They are called global elements and global attributes. You can directly reference them in other types of definitions.

6There are two ways to derive a new type: the first is to extend (inherit) from other types directly, and the other is to restrict the existing types by limiting the nature.

For example, there are three new types defined by restrictions:

 Limited by value range:

<XSD: simpletype name = "myinteger">

<XSD: Restriction base = "XSD: integer">

<XSD: min‑sive value = "10000"/>

<XSD: maxcompute sive value = "99999"/>

</XSD: Restriction>

</XSD: simpletype>

 Usage mode matching restrictions:

<XSD: simpletype name = "SKU">

<XSD: Restriction base = "XSD: string">

<XSD: Pattern value = "\ D {3}-[A-Z] {2}"/>

</XSD: Restriction>

</XSD: simpletype>

 Limits using enumeration:

<XSD: simpletype name = "cncity">

<XSD: Restriction base = "XSD: string">

<XSD: enumeration value =" Beijing "/>

<XSD: enumeration value =" Nanchang "/>

<XSD: enumeration value =" Shanghai "/>

</XSD: Restriction>

</XSD: simpletype>

 

7, Atomic type (an inseparable type, suchString, integerBuilt-in types), list types, and Union types are collectively referred to as simple types. InSchemaInNmtokens,Idrefs,EntitiesThree built-in list types, you can also create from existing simple typesList (List)Type, but you cannotListType and complex type to create a list (List) Type.

For example:

<XSD: simpletype name = "listofmyinttype">

<XSD: List itemtype = "myinteger"/>

</XSD: simpletype>

InXMLThe list type values in the instance document are separated by spaces. IfListofmyinttypeElement, whose value may be:

<Listofmyint> 20003 15037 95977 95945 </listofmyint>

8, There are several elements that can be appliedListType. They are:Length,Minlength,MaxlengthAndEnumerationSuch:

<XSD: simpletype name = "usstatelist">

 <XSD: List itemtype = "usstate"/>

</XSD: simpletype>

 

<XSD: simpletype name = "sixusstates">

<XSD: Restriction base = "usstatelist">

 <XSD: length value = "6"/>

</XSD: Restriction>

</XSD: simpletype>

 NOTE: For the list type, note that the member isStringType, becauseStringThe space in the type is partially confused with the space in the list type separator.

9You can specify an element by specifying itsTypeAn attribute is a defined attribute, or an anonymous type can be used, for example:

Use Type Definition:

<XSD: element name = "comment" type = "XSD: string">

Anonymous definition:

<XSD: element name = "quantity">

<XSD: simpletype>

<XSD: Restriction base = "XSD: positiveinteger">

<XSD: maxexclusive value =" 100" />

</XSD: Restriction>

</XSD: simpletype>

</XSD: Element>

10,Union(Union) indicates thatXMLThe element instance in the instance document conformsUnionOne of the member types defined by the type can be (valid ).C ++The Union type in has similar concepts, such:

<XSD: simpletype name = "addrunion">

<XSD: Union membertypes = "XSD: String integer"/>

</XSD: simpletype>

11And complex types can be divided into three types: the first class contains character content and attributes but does not contain child elements; the second category includes attributes and child elements, but does not contain character data (character data is included in child elements). The third category includes attributes and character content and child elements; how can we define these three types? For the first type, you can useSimplecontentThe second type can be implemented throughComplexcontentTo achieve this, the third type only needsComplextypeAttributesMixedSetTrueYou can. An example is as follows:

The first type (extended from a simple type, added attributes ):

<XSD: element name = "internationalprice">

<XSD: complextype>

<XSD: simplecontent>

<XSD: Extension base = "XSD: decimal">

<XSD: attribute name = "currency" type = "XSD: string"/>

</XSD: Extension>

</XSD: simplecontent>

</XSD: complextype>

</XSD: Element>

 Type 2 (oneElementAnd twoAttributeComposition ):

<XSD: element name = "internationalprice">

<XSD: complextype>

<XSD: complexcontent>

<XSD: element name = "country"Type = "XSD: string"/>

<XSD: attribute name = "currency" type = "XSD: string"/>

<XSD: attribute name = "value"Type = "XSD: decimal"/>

</XSD: complexcontent>

</XSD: complextype>

</XSD: Element>

 Note: The default value isComplexcontentTherefore, the simple syntax here is:

<XSD: element name = "internationalprice">

<XSD: complextype>

<XSD: element name = "country"Type = "XSD: string"/>

<XSD: attribute name = "currency" type = "XSD: string"/>

<XSD: attribute name = "value"Type = "XSD: decimal"/>

</XSD: complexcontent>

</XSD: Element>

 Third Type:

<XSD: element name = "letterbody">

<XSD: complextype mixed = "true">

<XSD: sequence>

<XSD: element name = "salutation">

<XSD: complextype mixed = "true">

<XSD: sequence>

<XSD: element name = "name" type = "XSD: string"/>

</XSD: sequence>

</XSD: complextype>

</XSD: Element>

<XSD: element name = "quantity"Type = "XSD: positiveinteger"/>

<XSD: element name = "productname" type = "XSD: string"/>

<XSD: element name = "shipdate"Type = "XSD: Date" minoccurs = "0"/>

</XSD: sequence>

</XSD: complextype>

</XSD: Element>

The third type of instance may be as follows:

<Letterbody>

<Salutation> dear mr. <Name> Robert Smith </Name>. </salutation>

Your order of <quantity> 1 </quantity> <productname> baby

Monitor </productname> shipped from our warehouse on

<Shipdate> 2017-05-21 </Shipdate>

</Letterbody>

12, According11To define an empty content element, that is, to define an element that only contains attributes, as longComplexcontentIt does not contain any child elements, such:

<XSD: element name = "internationalprice">

<XSD: complextype>

<XSD: attribute name = "currency" type = "XSD: string"/>

<XSD: attribute name = "value"Type = "XSD: decimal"/>

</XSD: complextype>

</XSD: Element>

13,AnytypeYes allSchemaType Base type, andJavaInObjectSimilar. Therefore, the following definitions:

<XSD: element name = "anything" type = "XSD: anytype"/>

 Can be written:

 <XSD: element name = "anything"/>

14,SchemaChina siteAnnotation,Document,AppinfoThree elements for comments.APPIAndDocumentAll ActAnnotation. AndAnnotationGenerallySchemaSub-elements,ElementThe top-level sub-element of the type definition.

For example:

<XSD: element name = "internationalprice">

<XSD: annotation>

<XSD: Documentation XML: lang = "en">

Element declared with anonymous type

</XSD: Documentation>

</XSD: annotation>

<XSD: complextype>

<XSD: annotation>

<XSD: Documentation XML: lang = "en">

Empty anonymous type with 2 attributes

</XSD: Documentation>

</XSD: annotation>

<XSD: complexcontent>

<XSD: Restriction base = "XSD: anytype">

<XSD: attribute name = "currency" type = "XSD: string"/>

<XSD: attribute name = "value"Type = "XSD: decimal"/>

</XSD: Restriction>

</XSD: complexcontent>

</XSD: complextype>

</XSD: Element>

15,ChoiceYou can only use one of the Child elements in the instance documentation.AllAll elements can appear once or once, and the element instance has no order constraints, andAllIt must be placed at the top of any content model. To illustrate this problem, we first list a valid one, and then list an invalid one for reference:

<XSD: complextype name = "purchaseordertype">

<XSD: All>

<XSD: element name = "shipto" type = "usaddress"/>

<XSD: element name = "billto" type = "usaddress"/>

<XSD: Element ref = "comment" minoccurs = "0"/>

<XSD: element name = "items" type = "items"/>

</XSD: All>

<XSD: attribute name = "orderdate" type = "XSD: Date"/>

</XSD: complextype>

 The following is an invalid one:

<XSD: complextype name = "purchaseordertype">

<XSD: sequence>

<XSD: All>

<XSD: element name = "shipto" type = "usaddress"/>

<XSD: element name = "billto" type = "usaddress"/>

<XSD: element name = "items" type = "items"/>

</XSD: All>

<XSD: sequence>

<XSD: Element ref = "comment"/>

</XSD: sequence>

</XSD: sequence>

<XSD: attribute name = "orderdate" type = "XSD: Date"/>

</XSD: complextype>

16There are several identical types in many types. You can use Attribute groups for reuse. The format of attribute groups is as follows:

<XSD: attributegroup name = "attrgroupname">

<XSD: attribute name = "attrname 1" Type = "XSD: string"/>

<XSD: attribute name = "attrname 2" Type = "XSD: string"/>

...

</XSD: attributegroup>

 You can use the following methods:

<XSD: element name = "testattrgroup">

<XSD: comlextype>

<XSD: element name = "element 1" Type = "XSD: string"/>

<XSD: attributegroup ref = "attrgroupname"/>

</XSD: complextype>

</XSD: Element>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.