Schema constraints for XML

Source: Internet
Author: User

The XSD document must contain at least: the schema root element and the XML Schema namespace definition, element definition. It is important to note that the XSD must define one and only one schema root element, including schema constraints, XML Schema namespace definitions, other namespace definitions, version information, language information, and some other information.

1. Schema root element definition :

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

2. Common element definitions :

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

The elements in the XSD are declared with the element identifier, in the example above the name attribute is the name of the elements, the type attribute is the element's value, and you can make the data type or other type built into the XML Schema .

all elements are as follows:

element name " Td>
element description
name
type type of element value
minoccurs
maxoccurs The maximum number of occurrences of this element in the parent element (default is 1, must be greater than or equal to 0), when set to unbounded means no limit.

2.1 Element Reference: To avoid defining the same element more than once in a document, you can use the element's reference:

<xsd:element name="user" type="xsd:string" /><xsd: sequence>    Ref="user" /> <!--The current element is the user element--></xsd: Sequence>

2.2 Element aliases: Define First

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

after use

<yonghu>admin</yonghu><user>admin</user>

2.3 Default value and fixed value

By setting the default property, you can set a fixed value for the element country in the XML document without assigning the defaults to the city definition, and it does not allow changes.

<xsd:element name="City" type="xsd:string"Default ="Xian" /><xsd:element name="country" type ="xsd:string"fixed="China" />

2.4 combination Device

Sequence, which defines a column of elements that must be displayed in the order specified in the pattern (or not, if optional).

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

    all a combination that allows the defined elements to be displayed in any order, and the child elements of the all element are required by default. and is displayed at most once per time.

 <xsd:all minoccurs= " 0   " > <xsd:element name="  first   " type="   xsd:string   "/> <xsd:element name="  middle   " type=  " xsd:string   "/> <xsd:element name="  last   " type="  Span style= "COLOR: #800000" >xsd:string   "/> </xsd:all> 

choice, which allows you to specify one of multiple sets of declarations for mutually exclusive cases.

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

3. Defining Attributes

attributes can be defined in an XML Schema document by defining the elements , but are highly restrictive. The properties that can be applied to the attribute element definition are shown in the following table.

Property Meaning
Defalt Initial default value
Fixed Fixed property values that cannot be modified and overridden
Name Name of the property
Ref A reference to the previous property definition
Type The XSD type or simple type of the property
Use How to use the property optional (optional attribute, which is not required, default is this), prohibited (forbidden), or required (mandatory).
Form Determine the address of the attributeFormDefault
Id ID of a property unique in a schema document

Creating properties

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

The statement defines a property named age whose value must be an integer. when you add it to a schema, it must be a schema element, a complextype element, or a child element of the attributegroup element .

 <xsd:element name= " name   " > <xsd:complexType> <xsd:sequence> <xsd:element name= " first  "  type=< Span style= "COLOR: #800000" > " xsd:string  "  /> </xsd:sequence> <xsd:attribute name= " age  "  type= " xsd:integer  "  use= optional   "/> <!-- Add an attribute to the element name property--</xsd:complextype></xsd:element> 

the above documents correspond to valid XML documents as follows:

<?xml version="1.0"? ><name age="> <first" >string</first></name>

this digest from: XML Schema of the inverse heart < third >

Schema constraints for XML

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.