XML Schema verification for Java programmers from stupid birds to cainiao (25)

Source: Internet
Author: User

XML schema is created with a set of predefined XML elements and attributes. These elements and attributes define the structure and content mode of the XML document. XML Schema specifies the structure of the XML document instance and the Data Type of each element/attribute.

Why schema?

Limitations of DTD

1. The DTD does not comply with the XML syntax (one syntax is used for writing an XML document instance, and the other syntax is used for writing a DTD)

2. The DTD data type is limited (inconsistent with the database data type)

3. The DTD cannot be expanded.

4. DTD does not support namespaces (Name Conflict)

New Features of. Schema

1. schema is based on XML syntax

2. the schema can be processed using tools that can process XML documents.

3. the schema greatly expands the data type and can customize the data type.

4. schema supports element inheritance-object-oriented'

5. schema supports attribute groups.

I. Basic schema knowledge

1. schema (schema): similar to DTD, it is used to verify the effectiveness of XML documents, but it provides more powerful functions and finer-grained data types than DTD, in addition, the schema can also customize the data type. In addition, schema is also an XML file, while DTD is not.

2. The root element of all schema documents must be schema.

3. schema document structure

4. schema Data Type

1. Basic Type

 

2. Extended Data Types

3. Data Type features

Ii. schema element type

1. schema element:

Purpose: Include the defined schema.

Usage: <Xs: schema>

• Attribute

-Xmlns-targetnamespace

2. element: declares an element.

Attribute:-name-type-ref-minoccurs-maxoccurs

-Substitutiongroup-fixed-Default

Example:

<xs:element name="cat" type="xs:string"/><xs:element name="dog" type="xs:string"/><xs:element name="pets">

3. group element

Purpose: combine a group of element declarations so that they can be applied together

• Attribute: Name/Ref

Example:

<xs:group name="myGroupOfThings"> <xs:sequence> <xs:element ref="thing1"/> <xs:element ref="thing2"/> </xs:sequence> </xs:group> 

4. Attribute Element

Purpose: declare an attribute.

• Attribute: Name/type/Ref/Use

• Example:

<xs:complexType name="myComplexType"> <xs:attribute name="mybaseattribute" type="xs:string" use="required"/> </xs:complexType> 

5. attributegroup Element

Purpose: combine a set of attribute declarations so that they can be applied to a composite type.

. Attribute: Name/Ref

. Example:

<xs:attributeGroup name="myAttributeGroup"><xs:attribute name="someattribute1" type="xs:integer"/><xs:attribute name="someattribute2" type="xs:string"/></xs:attributeGroup>

6. simpletype Element

Purpose: define a simple type that determines the constraints and related information of elements and attribute values.

. Property: Name

. Content: There are three simple types of applications:

-Restrict → define a range

-List → select from the list

-Union → combination of Values

1. The child element is: <Xs: Restriction> to define a constraint.

2. The sub-element is: <Xs: List> to define a simple type element from a collection of specific data types

3. The child element is: <Xs: Union> to define a simple type element from a set of specific simple data types

Example: <Xs: simpletype name = "roadbikesize">

<xs:restriction base="xs:positiveInteger"><xs:enumeration value="46"/><xs:enumeration value="52"/><xs:enumeration value="55"/></xs:restriction></xs:simpleType><xs:simpleType name="mountainbikesize"><xs:restriction base="xs:string"><xs:enumeration value="small"/><xs:enumeration value="medium"/><xs:enumeration value="large"/></xs:restriction></xs:simpleType></xs:schema>

7. complextyep type

Purpose: define a composite type that determines the constraints and related information of a group of elements and attribute values.

• Attribute: Name

• Example:

<xs:complexType name="internationalShoeSize"> <xs:simpleContent> <xs:extension base="xs:decimal"> <xs:attribute name="sizing" type="xs:string"/> </xs:extension> </xs:simpleContent> </xs:complexType> <xs:element name="myShoeSize" type="internationalShoeSize"/>

Differences between simpletype elements and complextyep types (important)

Simpletype elements cannot contain elements or attributes.

• Complextype is used to declare the child elements and/or attributes of an element;

• Simpletype is used to define a new data type based on the built-in basic data type.

8. simplecontent Element

Function: applies to complextype and restricts and extends its content.

9. Choice Element

Purpose: Allow a unique element to be selected from a group

. Attribute: minoccurs/maxoccurs

10. sequence element

Purpose: Give a group of elements a specific sequence.

A complete schema example:

<xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema targetNamespace="http://tempuri.org/po.xsd" xmlns="http://tempuri.org/po.xsd" > <xs:element name="purchaseOrder" type="PurchaseOrderType"/> <xs:element name="comment" type="xs:string"/> <xs:complexType name="PurchaseOrderType"> <xs:sequence> <xs:element name="shipTo" type="USAddress"/> <xs:element name="billTo" type="USAddress"/> <xs:element ref="comment" minOccurs="0"/> <xs:element name="items" type="Items"/> </xs:sequence> <xs:attribute name="orderDate" type="xs:date"/> </xs:complexType> <xs:complexType name="USAddress"><xs:sequence><xs:element name="name" type="xs:string"/><xs:element name="street" type="xs:string"/><xs:element name="city" type="xs:string"/><xs:element name="state" type="xs:string"/><xs:element name="zip" type="xs:decimal"/></xs:sequence><xs:attribute name="country" type="xs:NMTOKEN"fixed="US"/></xs:complexType><xs:complexType name="Items"> <xs:sequence> <xs:element name="item" minOccurs="0" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="productName" type="xs:string"/> <xs:element name="quantity"> <xs:simpleType> <xs:restriction base="xs:positiveInteger"> <xs:maxExclusive value="100"/> </xs:restriction> </xs:simpleType> </xs:element> <xs:element name="USPrice" type="xs:decimal"/> <xs:element ref="comment" minOccurs="0"/> <xs:element name="shipDate" type="xs:date" minOccurs="0"/> </xs:sequence> <xs:attribute name="partNum" type="SKU" use="required"/> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> <!-- Stock Keeping Unit, a code for identifying products --> <xs:simpleType name="SKU"> <xs:restriction base="xs:integer"> <xs: minInclusive=“2”/> <xs:maxInclusive=“10”> </xs:restriction> </xs:simpleType> </xs:schema> 

Schema summary:

Schema is another document type definition, which complies with the XML language specification.

. Schema is scalable and supports namespaces;

. Schema supports more data types and element types;

. Schema uses element to declare elements and attribute to declare attributes of elements;

. Schema defines simple types with simpletype and complex types with complextype.

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.