XML Language Foundation 3--schema

Source: Internet
Author: User

1. What is schema

XML Schemas are created with a set of pre-defined XML elements and attributes that define the structure and content patterns of the XML document.

The XML schema specifies the structure of an XML document instance and the data type of each element/attribute.

2. Why schema is required

Bridging the limitations of DTDs

New features of schema

Consistency: The XML Schema defines its document structure based on the XML basic syntax rules, inheriting the self-descriptive and extensible nature of XML.

Completeness: the introduction of data types (and customizable data types), namespaces, support for other XML Schema references, with strong modularity;

Normative and accurate: The XML Schema provides a more prescriptive and complete mechanism to constrain XML documents.

Object-oriented features: Many mature object-oriented mechanisms (such as inheritance and polymorphism) are introduced into XML schemas.

Extensibility: The XML Schema provides some extensibility mechanisms that allow you to add relevant data to your XML instance data as needed in case the data schema cannot be accurately described beforehand.

Document Structure of 3.Schema

The suffix name of the XML Schema file is typically. xsd

The XML Schema file is a special XML file

? All schema documents use schema as their root element? The elements and data types used to construct the schema come from the namespace Http://www.w3.org/2001/XMLSchema, with the prefix generally using XS or XSDExample (Save As Student.xsd):
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Xs:schemaXmlns:xs= "Http://www.w3.org/2001/XMLSchema" >3     <xs:elementname= "Student"type= "Student"/>4         <Xs:complextypename= "Student">5         <xs:sequence>6         <xs:elementname= "Name"type= "Xs:string"/>7         <xs:elementname= "Gender"type= "Gender"/>8         <xs:elementname= "No"type= "Xs:string"/>9         <xs:elementname= "Teliphone"type= "Telenum"/>Ten         <xs:elementname= "Age"type= "Xs:nonnegativeinteger"/> One         <xs:elementname= "GPA"type= "Xs:int"minOccurs= "0"/> A         </xs:sequence> -         </Xs:complextype> -         <Xs:simpletypename= "Gender"> the             <xs:restrictionBase= "Xs:string"> -             <xs:enumerationvalue= "Female"/> -             <xs:enumerationvalue= "Male"/> -             </xs:restriction> +         </Xs:simpletype> -         <Xs:simpletypename= "Telenum"> +             <xs:restrictionBase= "Xs:string"> A             <Xs:patternvalue= "(\d{4}-\d{8}) | (\d{3}-\d{8}) | (\d{4}-\d{7}) "/> at             </xs:restriction> -          -         </Xs:simpletype> - </Xs:schema>
View Code

XML document associated with the schema:

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <StudentXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:nonamespaceschemalocation= "Student.xsd">3     <name>Xiaoming</name>4     <Gender>Male</Gender>5     <No>123</No>6     <Teliphone>011-12345678</Teliphone>7     < Age>20</ Age>8 </Student>
View Code

Data structure of 4.Schema

The data types in the XML Schema are primarily for XML elements.

Simple Type : An element that contains no child elements and attributes, only text content or empty elements that do not contain text

--built-in data types

Basic data Type Primitive Datatype

Derived data type drived Datatype

--user-defined data type (defined by simpletype)

Complex Type : An element that contains child elements and/or attributes, or contains textual content (defined by complextype).

4.1 User-defined simple data types in XML schemas

A) Derive simple data types by limiting (restriction)

b

Derive simple data types by restriction (restriction)

c) Derive simple data types by merging

4.2 Complex data types

Several cases of complex data types

① empty Element (contains only attributes, does not contain child elements, and text content)

− for an empty element that contains a property, simply define the desired attribute and its data type.

② contains only child elements, does not contain text content (may contain attributes)

The declaration of a child element must be placed in a container, not directly in the Xs:complextype element, even if there is only one child element.

If there are attributes, the attribute declaration must be placed after all child element declarations. ? Xs:sequence represents a sequence in which elements must appear in the order in which they are declared. The Xs:choice represents the selection from the included content. the Xs:all indicates that the content contained therein is not in order of precedence.

③ contains only text content and attributes, and does not contain child elements

For elements of this type, containing only simple content (text and attributes), when declared, a new element simplecontent (used to denote "text", or "text + attributes") is introduced into the XML schema.

④ contains both child elements and text (may contain attributes)

Define this complex data type to use the mixed property of complextype.

If there are attributes, the attribute declaration must be placed after all child element declarations.

 

XML Language Foundation 3--schema

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.