XML Schema Definition

Source: Internet
Author: User

<Continued XML>

XSDL (XML Schema Definition Language)It consists of elements, attributes, namespaces, and other nodes of the XML document type.

I,XSDElement in

 

The XSD document must include at least the schema root element, the XML schema namespace definition, and the element definition.

 

1, SchemaRoot element

 

Syntax:

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

...

</Xsd: schema>

 

Only one schema root element must be defined in XSD. The root element includes schema constraints, XML Schema namespaces, definitions of other namespaces, version information, language information, and other attributes.

 

2, Element

 

Syntax:

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

 

Elements in XSD are declared using element identifiers. Here, the name attribute is the name of the element, and the type attribute is the type of the element value. Here it can be the built-in data type in XML Schema or other types.

 

Example:

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

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

</Xsd: schema>

The valid XML documents corresponding to the above documents are as follows:

<? Xml version = "1.0"?>

<User> string </user>

 

The element definition has two attributes: minOccurs and maxOccurs. MinOccurs defines the minimum number of times this element appears in the parent element (1 by default, and the value is an integer greater than or equal to 0 ), maxOccurs defines the maximum number of times this element appears in the parent element (1 by default, and an integer greater than or equal to 0 ). In maxOccurs, you can set the value to unbounded, indicating that there is no limit 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 of the element user is string, and the number of occurrences is at least 0 (optional.

 

3, Reference element and substitution

 

Syntax:

<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>

 

Reference is implemented using the ref attribute of the element tag. It is mainly applicable to avoid defining the same element multiple times in the Document. the frequently used element should be defined as a child element of the root element so that it can be referenced anywhere in the document.

 

You can also create an alias for a defined element (even ^ 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 valid XML documents corresponding to the above documents are as follows:

 

<? Xml version = "1.0"?>

<Name>

<User> string </user>

</Name>

Or:

<? Xml version = "1.0"?>

<Name>

<Yonghu> string </yonghu>

</Name>

 

It is mainly implemented using the attribute substitutionGroup in the element identifier.

 

4, Set the default value and fixed value

 

Syntax:

<Xsd: element name = "city" type = "xsd: string" default = "xian"/>

<Xsd: element name = "country" type = "xsd: string" fixed = "china"/>

 

By setting the default attribute, you can assign the default value when the city element is not defined in the XML document. With the fixed attribute, you can set a fixed value of china for the country element and cannot change it!

 

5The use of the bundle Control Structure

 

SequenceAggregators define that a column of elements must be displayed in the order specified in the mode (optional or not ). Syntax:

 

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

<Xsd: element name = "name">

<Xsd: complexType>

<Xsd: sequence>

<Xsd: element name = "first" 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>

 

AllThe aggregator allows the defined elements to be displayed in any order. The sub-elements of the all element are required by default and can be displayed at most once each time. Syntax:

 

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

<Xsd: element name = "name">

<Xsd: complexType>

<Xsd: all minOccurs = "0">

<Xsd: element name = "first" 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>

 

ChoiceYou can specify one of multiple sets of declarations for mutual exclusion. Syntax:

 

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

<Xsd: element name = "name">

<Xsd: complexType>

<Xsd: choice>

<Xsd: element name = "first" 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>

 

Ii. Define attributes

 

In the XML Schema document, attributes can be defined according to the method of the definition element, but the degree of restriction is high. They can only be simple, contain only text, and have no sub-attributes. Attributes that can be applied to attribute element definitions are as follows:

Default initial default Value

Fixed attribute values that cannot be modified or overwritten by fixed

Name of the name attribute

Ref references to the previous attribute Definition

Type the XSD type or simple type of this attribute

Use attributes

Form determines the local value of attributeFormDefault

Id: the unique ID of the attribute in the id mode document.

 

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

 

1. Create attributes

 

Syntax:

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

 

This statement defines an attribute named age. Its value must be an integer. When adding a schema to a schema, it must be a sub-element of the schema element, complexType element, or attributeGroup element.

 

Example:

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

<Xsd: element name = "name">

<Xsd: complexType>

<Xsd: sequence>

<Xsd: element name = "first" 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 the following valid XML documents:

 

<? Xml version = "1.0"?>

<Name age = "27">

<First> string </first>

</Name>

 

As shown above, to attach an attribute to an element, the attribute should be defined or referenced after the aggregator in the complexType 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.