An explanation of the XML trilogy schema

Source: Internet
Author: User
Tags define local xml parser xmlns


The previous blogs discussed the use of XML and DTDs, and the discussion learned that XML is a meta-markup language that allows you to define new markup languages. In addition, the document Definition Model provides specifications for XML documents, although the introduction of DTDs solves the problem of normalization of XML documents, but its file format types and XML file format types are inconsistent, and the data types provided by DTDs sometimes fail to meet the needs of the industry, so the schema is introduced. The schema schema determines the structure of the elements and attributes of an XML document, the order of elements, the data values of elements and attributes, and, depending on the scope, enumeration, and style matching.

I. The first knowledge of the schema
The XML Schema language, also known as XML Schema Definition (XSD), is defined as a legitimate component group of XML documents (the structure of an XML document), just as a DTD does. XML Schema is based on XML language, it can be said that XML schema itself is an application of XML.
1. Schema role
XML Schema is the same as the DTD, it is used to define the structure of an XML document schema, then why have the DTD and XML schema. because XML schemas are more powerful than DTDs.
XML Schema is more advantageous than DTD: patterns are extensible patterns that are richer than DTDs and more useful patterns are XML-written patterns that support the data type Schema support namespaces do not need to learn other languages can directly use the XML editor to write XML Schema can directly make Parsing XML Schemas with an XML parser can use XML DOM to manipulate XML Schemas using XSLT technology to transform XML schemas

2. Comparative study

1.1 effects on


The same is the pattern that defines the structure of an XML document.

1.2 usage on


The two work the same, but there are some grammatical differences.        The schema is an extension of the DTD, which also supports the definition of elements and attributes, and the syntax of the definitions is similar, but the schema can add the corresponding data types to the elements and attributes, introduces the syntax of global and local element declarations, and introduces simple types and complex types based on the data content of elements and attributes. The so-called simple types (SimpleType) and complex types (ComplexType) are not specific data types themselves, they are simply an abstract description of the types of nodes or custom types.
That is, the introduction of schemas makes schema declarations more similar to the programming languages we use.

Second, detailed examples


2.1 Schema Instance


Checklist 1:user.xml Document Structure

<?xml version= "1.0". >
< user list >
    < user >
         < username >xx</user name >
         < password >123456</password >
         < User type >1</user type >
   </user >
</user list >

Listing 2: Defining schema,user.xsd Using the Global component form

<!--defined with global components--
<?xml version= ' 1.0 ' encoding= ' utf-8 '?>
<xs:schema xmlns:xs= '/http Www.w3.org/2001/XMLSchema ' elementformdefault= ' qualified ' attributeformdefualt= ' unqualified ' >
 <xs: Element name= ' user list ' type= ' userlist '/>
 <xs:complextype name= ' userlist ' ><!-- Use complextype to declare elements of the type composite type-
  <xs:sequence><!--Use the sequence instructions the following elements must be displayed sequentially in the XML document-
   < Xs:element name= ' users ' type= ' user '/>
  </xs:sequence>
 </xs:complexType>
 <xs: ComplexType name= ' users ' >
  <xs:sequence>
   <xs:element name= ' username ' type= ' user '/>
   <xs: Element name= ' password ' type= ' user '/>
   <xs:element name= ' subscriber type ' type= ' user '/>
  </xs:sequence>
 </xs:complexType>
</xs:schema>

Listing 3: Defining schema,user.xsd with local form

<!--using local form definition--
<?xml version= ' 1.0 ' encoding= ' utf-8 '?>
<xs:schema xmlns:xs= '/http Www.nishishui.org/2000/XML Schema ' elementformdefault= ' qualified ' attributeformdefualt= ' unqualified ' >
 < Xs:element name= ' user list ' >
  <xs:complexType>
   <xs:sequence>
    <xs:element name= ' users ' >
     <xs:complexType>
      <xs:sequence>
       <xs:element name= ' user name ' type= ' xs:string '/>
       < Xs:element name= ' password ' type= ' xs:string '/>
       <xs:element name= ' user type ' type= ' Xs:integer '/>
      </xs: sequence>
     </xs:complexType>
    </xs:element>
   </xs:sequence>
  </xs: complextype>
 </xs:element>
</xs:schema>

Both listing 2 and listing 3 define USER.XSD, and they all work the same, to verify the legitimacy of the XML document, but the method is defined differently, listing 2 is defined using the form of the global component, and the local form definition used in Listing 3, the specific differences are shown below.


2.2 Basic Usage



We compare the schema and the DTD separately from the function and usage above, the biggest difference between schema and DTD is that the schema introduces the data type, and other declarations such as elements and attributes are similar to DTDs, which are no longer detailed in the following discussion.

schema Basic content map:

2.2.1 Reference Syntax when a schema file is established, it can be used to validate the validity of an XML document, that is, to verify that an XML document follows the definition of a schema file.        So, how does an XML document refer to a schema document? The reference to the schema model is more like the application of the previously mentioned namespace, as shown in the following example:

<?xml version= ' 1.0 ' encoding= ' utf-8 '?>
< user list Xmlns:xsi=http://www.nishishui.org/2000/xmlschema xsi: nonamespaceschemalocation= ' user.xsd ' ><!--references user.xsd-
 < users >
  < username > who I am </user name >
  < password >123456</password >
  < user type >1</user type >
 </user >
</user list >


2.2.2 element type

(1) According to the content of the different into simple and complex elements, respectively, using simpletype and complextype markers.

simple elements: the content in an element can only be text, and does not contain other elements and attributes.

<?xml version= ' 1.0 ' encoding= ' utf-8 '?> <xs:schema xmlns:xs=
' Http://www.nishishui.org/2000/XML schema ' elementformdefault= ' qualified ' attributeformdefualt= ' unqualified ' >
	<xs:element name= ' age ' >
		< xs:simpletype><!--using the keyword SimpleType to declare simple elements-
			<!-- The restriction keyword combined with mininclusive and maxinclusive controls the acceptable range of values for elements in XML 0~100-->
			<xs:restriction base= "Xs:integer" >
				<xs:mininclusive value= ' 0 '/>
				<xs:maxinclusive value= '/> '
			</xs:restriction>
		</xs:simpleType>
	</xs:element>
</xs:schema>
complex elements: elements contain other elements and attributes. It has four types, which are empty elements, contain only other elements, contain only the body, contain the body, and contain other elements.
<?xml version= ' 1.0 ' encoding= ' utf-8 '?> <xs:schema xmlns:xs=
' Http://www.nishishui.org/2000/XML schema ' elementformdefault= ' qualified ' attributeformdefualt= ' unqualified ' >
	<xs:element name= ' age ' >
		< xs:complextype><!--Use the keyword ComplexType to declare complex elements
			--<!--sequence control the order in which elements appear in the XML Content--
			<xs: Sequence>
				<!--define specific elements, these are simple elements--
				<xs:element name= ' firstname '
				type= ' xs:string '/> <xs:element name= ' lastname ' type= ' xs:string '/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

(2) According to the defined position can be divided into local and global elements.

global Element The parent element of the:<element> element must be <schema>;

local element: a local element declaration can only appear inside a complex type (<complexType> element) definition. That is, the parent element of the <element> element can only be a <all>, <choice>, or <sequence> element.

<?xml version= ' 1.0 ' encoding= ' utf-8 '?> <xs:schema xmlns:xs=
' Http://www.nishishui.org/2000/XML schema ' elementformdefault= ' qualified ' attributeformdefualt= ' unqualified ' >
	<xs:element name= ' users ' type= ' user '/ ><!--Global Element--
	<xs:element name= ' user name ' type= ' xs:string '/><!--global element--
	<xs:element Name= ' password ' type= ' xs:string ' ><!--global element--
		<xs:complextype name= ' user ' >
			<!-- Sequence controls the order in which elements appear in the XML Content-
			<xs:sequence>
				<!--define specific elements, which are simple elements--
				<!--define local elements, Use the REF keyword reference and use minOccurs and maxOccurs to make the least and most occurrences of the element--
				<xs:element ref= ' user name ' minoccurs= ' 0 ' maxoccurs= ' 1 '/ ><!--Local Elements--
				<xs:element ref= ' password ' minoccurs= ' 0 ' maxoccurs= ' 1 '/><!--local elements--
			</xs :sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

Third, conclusion


The basic usage of the schema we discussed here, although the above is explained in detail, but there are many things not explained, especially the nature of the schema, the reason is not explained in detail is not because it is not important, but because he and the basic usage of the DTD is very similar, the key is the accumulation of problems, Take a look at the various properties in practice and if you want to learn more about the use of the schema attribute, download the map above.

Now that all the content in the XML trilogy has been discussed, from the beginning of the XML syntax to the use of the DTD, and finally to the use of the schema, we have roughly covered all the XML usage content, hoping to help other children's shoes, more content in practice slowly accumulate.

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.