XML Schema Tuto...login
XML Schema Tutorial
author:php.cn  update time:2022-04-20 14:13:02

XML Schema indicator


XSD Indicators


Indicators allow us to control how elements are used in the document.


Indicators

There are seven types of indicators:

Order indicator:

  • All

  • Choice

  • Sequence

##Occurrence Indicator:

  • maxOccurs

  • minOccurs

Group indicator:

  • Group name

  • attributeGroup name


#Order indicator

The Order indicator is used to define the order of elements.

All indicator

<all> The indicator specifies that child elements can appear in any order, and each child element must appear only once:

< xs:element name="person">
<xs:complexType>
<xs:all>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element>

Note: When using the <all> indicator, you can replace <minOccurs> ; is set to 0 or 1, and only the <maxOccurs> indicator can be set to 1 (<minOccurs> and <maxOccurs> will be explained later).

Choice indicator

<choice> The indicator specifies that a certain child element can appear or another child element can appear (either this or that):

<xs:element name="person">
<xs:complexType>
<xs:choice>
<xs:element name="employee" type="employee"/>
<xs:element name="member" type="member"/>
</xs:choice>
</xs:complexType>
</xs:element>

Sequence indicator

<sequence> Specifies that child elements must appear in a specific order:

<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>


Occurrence indicator

The Occurrence indicator is used to define the occurrence of an element Frequency of.

Note: For all "Order" and "Group" indicators (any, all, choice, sequence, group name and group reference), the default values ​​​​of maxOccurs and minOccurs Both are 1.

maxOccurs indicator

<maxOccurs> The indicator specifies the maximum number of times an element can appear:

<xs:element name="person ">
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="child_name" type="xs:string" maxOccurs="10"/>
</xs:sequence>
</xs:complexType>
</xs:element>

The above example shows that the child element "child_name" can appear at least once in the "person" element (where minOccurs The default value is 1), which can occur up to 10 times.

minOccurs indicator

<minOccurs> The indicator can specify the minimum number of times an element can appear:

<xs:element name="person ">
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="child_name" type="xs:string"
maxOccurs="10" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>

The above example shows that the child element "child_name" can appear at least 0 times and at most 10 times in the "person" element.

Tips: If you want to make the number of occurrences of an element unlimited, please use the maxOccurs="unbounded" statement:

A practical example:

XML file named "Myfamily.xml":

<?xml version="1.0" encoding="ISO-8859-1"?>

<persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="family.xsd">

<person>
<full_name>Hege Refsnes</full_name>
<child_name>Cecilie</child_name>
</person>

<person>
<full_name>Tove Refsnes</full_name>
<child_name>Hege</child_name>
<child_name>Stale</child_name>
<child_name>Jim</child_name>
<child_name>Borge</child_name>
</person>

<person>
<full_name>Stale Refsnes</full_name>
</person>

</persons>

The above XML file contains a root element named "persons". Inside this root element, we define three "person" elements. Each "person" element must contain a "full_name" element, and it can contain up to 5 "child_name" elements.

This is the schema file "family.xsd":

<?xml version="1.0" encoding="ISO-8859-1"?>
< ;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">

<xs:element name="persons"> ;
<xs:complexType>
<xs:sequence>
<xs:element name="person" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="child_name" type="xs:string"
minOccurs="0" maxOccurs="5"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>


##Group indicator

Group indicator is used to define related batches of elements.

Element group

Element group is defined through the group declaration:

<xs:group name="groupname">
...
</xs:group>
You must define an all, choice, or sequence element inside the group declaration. The following example defines a group named "persongroup", which defines a set of elements that must appear in a precise order:

<xs:group name="persongroup">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="birthday" type="xs:date"/>
</xs:sequence>
</xs:group>

After you define group, you can reference it in another definition:

<xs:group name="persongroup">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="birthday" type="xs:date"/>
</xs:sequence>
</xs:group>

<xs:element name="person" type="personinfo"/>

<xs :complexType name="personinfo">
<xs:sequence>
<xs:group ref="persongroup"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>

Attribute Group

Attribute groups are defined through the attributeGroup declaration:

<xs:attributeGroup name="groupname">
...
</xs:attributeGroup>

The following example defines a group named "personattrgroup" An attribute group:

<xs:attributeGroup name="personattrgroup">
<xs:attribute name="firstname" type="xs:string"/>
<xs:attribute name="lastname" type="xs:string"/>
<xs:attribute name="birthday" type="xs:date"/>
</xs:attributeGroup>

After you have defined the attribute group, you can Reference it in another definition, like this:

<xs:attributeGroup name="personattrgroup">
<xs:attribute name="firstname" type="xs:string"/>
<xs:attribute name="lastname" type="xs:string"/>
<xs:attribute name="birthday" type="xs:date"/>
</xs:attributeGroup>

<xs:element name="person">
<xs:complexType>
<xs:attributeGroup ref="personattrgroup"/>
</xs:complexType>
</xs:element>