Initial experience of XML schema learning

Source: Internet
Author: User
Tags define definition integer reference string version valid xmlns
Xml

Recently immersed in the wmfc definition of the interface 1 standard document, where the definition of XML format used XML Schema, due to XML Schema in many ways has a great role in the trend to replace the DTD , so here are some learning notes, there are omissions in the place please correct me!

Other aspects of the XML Schema I'm not here to wordy, go straight to the subject, about the basic syntax of the XML Schema .

The xsdl(XML Schema Definition language ) is composed of elements, attributes, namespaces, and other nodes of the XML document species.

One, XSD the elements in

The XSD document must contain at least: theschema root element and the definition of the XML schema namespace, the element definition.

1 ,schema root element

The syntax is as follows:

<xsd:schema xmlns:xsd= " http://www.w3.org/2001/XMLSchema">

...

</xsd:schema>

You must define one in the XSD and only one schema root element can be defined. The root element includes the constraints of the schema, the definition of theXML schema namespace, the definition of other namespaces, version information, language information, and other attributes.

2 , Element

The syntax is as follows:

<xsd:element name= "user" type= "xsd:string"/>

Elements in an XSD are declared using the element identifier. where Name property is the name of the element, the Type property is the kind of the element value, where it can be a data type or other type that is built into the XML Schema .

Example:

<xsd:schema xmlns:xsd= " http://www.w3.org/2001/XMLSchema">

<xsd:element name= "user" type= "xsd:string"/>

</xsd:schema>

The following valid XML documents correspond to the above documents:

<?xml version= "1.0"?>

<user>string</user>

There are also 2 attributes in the definition of an element:minoccurs and maxOccurs. Where minoccurs defines the minimum number of times the element appears in the parent element (the default is 1, and the value is an integer greater than or equal to 0 ),maxOccurs Defines the maximum number of times the element appears in the parent element (the default is 1, and the value is an integer greater than or equal to 0 ). You can set the value to unboundedin maxOccurs to indicate that there are no restrictions on the maximum number of times an element appears.

Example:

<xsd:schema xmlns:xsd= Http://www.w3.org/2001/XMLSchema >

<xsd:element name= "user" type= "xsd:string" minoccurs= "0" maxoccurs= "unbounded"/>

</xsd:schema>

The type that is represented as element user is string, and the number of occurrences is at least 0(that is, optional), at most not limited.

3 , reference elements, and substitution

The syntax is as follows:

<xsd:schema xmlns:xsd= " Http://www.w3.org/2001/XMLSchema ">

<xsd:element name= "user" type= "xsd:string"/>

<xsd:element name= "Name" >

<xsd:complexType>

<xsd:sequence>

<xsd:element ref= "User"/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

</xsd:schema>

References are implemented using the ref attribute of the element marker. Primarily to avoid defining the same element more than once in a document, you should define frequently used elements as child elements of the root element so that you can refer to it anywhere in the document.

Here you can also alias a defined element (I understand ^o^) as follows:

<xsd:schema xmlns:xsd= " Http://www.w3.org/2001/XMLSchema ">

<xsd:element name= "Yonghu" type= "xsd:string" substitutiongroup= "user"/>

<xsd:element name= "user" type= "xsd:string"/>

<xsd:element name= "Name" >

<xsd:complexType>

<xsd:sequence>

<xsd:element ref= "User"/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

</xsd:schema>

The following valid XML documents correspond to the above documents:

<?xml version= "1.0"?>

<name>

<user>string</user>

</name>

Or:

<?xml version= "1.0"?>

<name>

<yonghu>string</yonghu>

</name>

This is accomplished primarily by using the attribute substitutiongroup in the element identifier.

4 , set default values, and fixed values

The syntax is as follows:

<xsd:element name= "City" type= "xsd:string" default= "Xian"/>

<xsd:element name= "Country" type= "xsd:string" fixed= "the"

by Setting the default property, you can assign a default value to the city element when it is not defined in the XML document. Using the fixed attribute, you can set a fixed value for the element Country and do not allow change!

5 , using the control structure of the combination device

The sequence combo defines a list of elements that must be displayed in the order specified in the pattern (or not, if it is optional). The syntax is as follows:

<xsd:schema xmlns:xsd= " Http://www.w3.org/2001/XMLSchema ">

<xsd:element name= "Name" >

<xsd:complexType>

<xsd:sequence>

<xsd:element name= "The" "Type=" xsd:string "/>

<xsd:element name= "Middle" type= "xsd:string"/>

<xsd:element name= "Last" type= "xsd:string"/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

</xsd:schema>

The All combo, which allows the defined elements to be displayed in any order , and the child elements of the all element are required by default, and are displayed at most once per time. The syntax is as follows:

<xsd:schema xmlns:xsd= " Http://www.w3.org/2001/XMLSchema ">

<xsd:element name= "Name" >

<xsd:complexType>

<xsd:all minoccurs= "0" >

<xsd:element name= "The" "Type=" xsd:string "/>

<xsd:element name= "Middle" type= "xsd:string"/>

<xsd:element name= "Last" type= "xsd:string"/>

</xsd:all>

</xsd:complexType>

</xsd:element>

</xsd:schema>

The Choice combo allows you to specify one of multiple sets of declarations for mutual exclusion. The syntax is as follows:

<xsd:schema xmlns:xsd= " Http://www.w3.org/2001/XMLSchema ">

<xsd:element name= "Name" >

<xsd:complexType>

<xsd:choice>

<xsd:element name= "The" "Type=" xsd:string "/>

<xsd:element name= "Middle" type= "xsd:string"/>

<xsd:element name= "Last" type= "xsd:string"/>

</xsd:choice>

</xsd:complexType>

</xsd:element>

</xsd:schema>

Two, define the attribute

You can define attributes in an XML Schema document by the method of defining the elements, but with a higher degree of restriction. They can only be simple types, can contain only text, and have no child attributes. The attributes that can be applied to attribute element definitions are as follows:

Default Initial defaults

Fixed values of properties that cannot be modified and overwritten by fixed

Name Property

ref A reference to the previous property definition

Type the XSD type or simple type of this property

Use how properties are used

form determines the local value of the attributeFormDefault

ID mode unique ID of attribute in document

The Default,fixed,name,ref , and type attributes are identical to the corresponding attributes defined in the element tag, but Type can only be a simple type. The value of the Use property can be:optional(the property is not required, this is the default property),prohibited , or required(the property is mandatory).

1. Create Properties

The syntax is as follows:

<xsd:attribute name= "Age" type= "Xsd:integer"/>

The statement defines an attribute named age , whose value must be an integer. When you add it to a pattern, it must be a schema element, acomplexType element, or a child element of a attributegroup element.

Example:

<xsd:schema xmlns:xsd= " Http://www.w3.org/2001/XMLSchema ">

<xsd:element name= "Name" >

<xsd:complexType>

<xsd:sequence>

<xsd:element name= "The" "Type=" xsd:string "/>

</xsd:sequence>

<xsd:attribute name= "Age" type= "Xsd:integer" use= "optional"/>

</xsd:complexType>

</xsd:element>

</xsd:schema>

The above documents correspond to valid XML documents as follows:

<?xml version= "1.0"?>

<name age= ">"

<first>string</first>

</name>

As shown above, to attach a property to an element, the property should be defined or referenced after the complexType in the definition.



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.