Schema (Schema)

Source: Internet
Author: User
Tags xmlns

DTDs are equivalent to data, and schemas are equivalent to collections.
XML Schemas are created by a set of pre-defined XML elements and attributes.
DTD does not follow XML syntax, DTD data type is limited, DTD is not extensible, DTD does not support space

Named.
Schema is based on XML syntax, can handle the processing of XML documents, extend the data type, can

With custom data types, schema supports inheritance,
The document structure of the schema.
The DTD and schema function the same, both for XML validation. The schema is more powerful.
Also the schema is a XML,DTD not an XML.
Myschema.xsd
<?xml version= "1.0" encoding= "UTF-8"?>
<xs:schema xmlns:xs= "Http://www.w3.org/2001/XMLSchema"
elementFormDefault = "qualified"

attributeformdefault= "Unqualified" >
<xs:element name= "book" type= "book type"/>
<xs:complextype name = "book type" >
<xs:sequence>
<xs:element name= "name" type= "xs:string"/>
<xs:element name= "Author" type= "Xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

Myschema.xml
<?xml version= "1.0" encoding= "UTF-8"?>
< book xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:nonamespaceschemalocation= "C:\Users\acer\Desktop\zm\xml

\myschema.xsd ">
< name > I love programming </name >
< author > Mingming </author >
</Books >

All schema documents use schema as their root element.
The elements and data types used to construct the schema come from

The Http://www.w3.org/2001/XMLSchema namespace.
The schema element function contains a schema that has already been defined.
The element element acts as a declaration of elements.
<xs:element name= "Cat" type= "xs:string" >
<xs:complexType>
<xs:sequence minoccur= "0" maxoccurs= "0" >
<xs:element ref= "Cat"/>
</xs:sequence>
</xs:complexType>


Test1.xsd
<?xml version= "1.0" encoding= "UTF-8"?>
<xs:schema xmlns:xs= "Http://www.w3.org/2001/XMLSchema"
elementFormDefault = "qualified"

attributeformdefault= "Unqualified" >
<xs:element name= "Cat" type= "xs:string"/>
<xs:element name= "Dog" type= "xs:string"/>

<xs:complextype name = "MyType" >
<xs:sequence minoccurs= "1" maxoccurs= "1" >

<xs:element ref= "Cat" type= "xs:string"/>
<xs:element ref= "Dog" type= "xs:string"/>

</xs:sequence>
</xs:complexType>

<xs:element name= "Pets" type= "MyType"/>
</xs:schema>



Test1.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<pets xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:nonamespaceschemalocation= "Test1.xsd" >

<cat>hello</cat>
<dog>world</dog>
</pets>


Unbounded does not limit the appearance of several times.

The group element function is to combine a set of element declarations so that they can be reused together

Type of use.
Test2.xsd
<?xml version= "1.0" encoding= "UTF-8"?>
<xs:schema xmlns:xs= "Http://www.w3.org/2001/XMLSchema"
elementFormDefault = "qualified"

attributeformdefault= "Unqualified" >
<xs:group name= "MyGroup" >
<xs:sequence>
<xs:element name= "name" type= "xs:string"/>
<xs:element name= "Birthday" type= "xs:date"/>
<xs:element name= "Age" type= "Xs:integer"/>
<xs:/sequence>
</xs:group>

<xs:element name= "Person" >
<xs:complexType>
<xs:group ref= "MyGroup"/>

</xs:complexType>
</xs:schema>

Test2.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<person xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:nonamespaceschemalocation= "Test2.xsd" >

<name>tom</name>
<bithday></bithday>
<age>25</age>

</person>

The attribute element function is to declare an attribute.
Test3.xsd

<?xml version= "1.0" encoding= "UTF-8"?>
<xs:schema xmlns:xs= "Http://www.w3.org/2001/XMLSchema"
elementFormDefault = "qualified"

attributeformdefault= "Unqualified" >

<xs:attribute name= "Interest" type= "Xs:integer"/>
<xs:element name = "Person" >
<xs:complexType>
<xs:sequence>
<xs:element name= "Hello" type= "xs:string"/>
<xs:element name= "World" type= "xs:string"/>
</xs:sequence>
<xs:attribute ref = "Interest" use= "required"/>
</xs:complexType>
</xs:element>
</xs:schema>

Test3.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<person xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:nonamespaceschemalocation= "test3.xsd" interest= "" 2>
<world></world>

</person>


The attributegroup element function is to combine a set of attribute declarations so that it can be

Composite type Application.
Test4.xsd
<?xml version= "1.0" encoding= "UTF-8"?>
<xs:schema xmlns:xs= "Http://www.w3.org/2001/XMLSchema"
elementFormDefault = "qualified"

attributeformdefault= "Unqualified" >
<xs:attributegroup name = "Myattributegroup" >
<xs:attribute name= "Hello" type= "xs:string"

use= "Required"/>
<xs:attribute name= "World" type= "xs:string"
use= "optional"/>
<xs:attributeGroup>

<xs:element name= "MyElement" >
<xs:complexType>
<xs:attributegroup ref = "Myattributegroup"/>
</xs:complexType>
<xs:element>
<xs:schema>

Test4.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<myelement xmlns:xsi= "http://www.w3.org/2001/xmlschema-

Instance
xsi:nonamespaceschemalocation= "test4.xsd" hello= "abc"/>

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.