XML (v)-schema validation

Source: Internet
Author: User

SchemaWhat is schema

Schema (Schema) : It acts as a DTD and is used to validate the validity of an XML document, except that it provides more powerful functionality and finer-grained data types than DTDs. In addition, schema can customize the data type.

The schema is also an XML file, and the DTD is not.

Comparison of schema and DTD

  

Why to Schema

Schema document structure:

For all schema documents, the root element must be called schema. Schemas can contain attributes, such as:

<? XML version= "1.0" ?> <  xmlns:xs= "Http://www.w3.org/2001/XMLSchema"targetnamespace= "http// www.w3school.com.cn "xmlns=" http://www.w3school.com.cn "elementformdefault = "qualified" > ...... </ Xs:schema >
An example of a schema

Create a new DTD file

<?XML version= "1.0" encoding= "UTF-8"?><Xs:schemaXmlns:xs= "Http://www.w3.org/2001/XMLSchema"elementFormDefault= "qualified"attributeFormDefault= "unqualified">    <xs:elementname= "Name"type= "Xs:string"></xs:element>    <xs:elementname= "Health"type= "Xs:int"></xs:element>    <xs:elementname= "Love"type= "Xs:int"></xs:element>    <xs:elementname= "Strain"type= "Xs:string"></xs:element>    <Xs:groupname= "MyGroup" >        <xs:sequence>            <xs:elementref= "Name"></xs:element>            <xs:elementref= "Health"></xs:element>            <xs:elementref= "Love"></xs:element>            <xs:elementref= "Strain"></xs:element>        </xs:sequence>    </Xs:group>
<!--define a composite type--<Xs:complextypename= "Dogtype"> <Xs:groupref= "MyGroup"></Xs:group> </Xs:complextype> <xs:elementname= "Dog"type= "Dogtype"></xs:element></Xs:schema>

XML file written according to the DTD above

<?XML version= "1.0" encoding= "UTF-8"?><DogXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:nonamespaceschemalocation= "C:\Users\x\Desktop\schema\my3.xsd">    <name>Shinzo Abe</name>    < Health>0</ Health>    < Love>0</ Love>    <Strain>Grass Dog</Strain></Dog>
Data type of Schema

Basic data types

Extended Data types

Other data types

Constraints on data types

Rules

The element type of the schema

Schema element:

Role: Contains the schema usage that has already been defined:<xs:schema> properties: xmlns targetnamespace elementformdefault Attri Buteformdefault

Element elements

Group Element

Attribute elements

attributegroup Elements

Comprehensive case:

Schema

<?XML version= "1.0" encoding= "UTF-8"?><Xs:schemaXmlns:xs= "Http://www.w3.org/2001/XMLSchema"elementFormDefault= "qualified"attributeFormDefault= "unqualified">    <xs:elementname= "Name"type= "Xs:string"></xs:element>    <xs:elementname= "Health"type= "Xs:int"></xs:element>    <xs:elementname= "Love"type= "Xs:int"></xs:element>    <xs:elementname= "Strain"type= "Xs:string"></xs:element>    <Xs:groupname= "MyGroup" >        <xs:sequence>            <xs:elementref= "Name"></xs:element>            <xs:elementref= "Health"></xs:element>            <xs:elementref= "Love"></xs:element>            <xs:elementref= "Strain"></xs:element>        </xs:sequence>    </Xs:group>    <Xs:attributename= "id"type= "Xs:string"></Xs:attribute>    <Xs:attributename= "Color"type= "Xs:string"></Xs:attribute>    <Xs:attributegroupname= "MyAttr">        <Xs:attributeref= "id"></Xs:attribute>        <Xs:attributeref= "Color"></Xs:attribute>    </Xs:attributegroup>    <xs:elementname= "Dog">        <Xs:complextype>                <Xs:groupref= "MyGroup"></Xs:group>                <Xs:attributegroupref= "MyAttr"></Xs:attributegroup>        </Xs:complextype>            </xs:element></Xs:schema>

Xml

<?XML version= "1.0" encoding= "UTF-8"?><DogXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:nonamespaceschemalocation= "C:\Users\xx\Desktop\schema\my4.xsd"Color= "Red"ID= "ID01">    <name/>    < Health>10</ Health>    < Love>10</ Love>    <Strain/></Dog>

XML (v)-schema validation

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.