XSD Composite Type Indicator

Source: Internet
Author: User
Tags element groups xmlns

xsd mixed content xsd <any>

With an indicator, we can control how elements are used in a document. Indicator

There are seven kinds of indicators: Order Indicator: all Choice Sequence occurrence indicator : maxOccurs minOccurs Group indicator: Group Nam E attributegroup name Order indicator

The order indicator is used to define the sequence of elements. All indicator

The <all> indicator specifies that child elements can appear in any order, and that 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 set the <minOccurs> to 0 or 1, but only the <maxOccurs> indicator to 1 (will be explained later <minOccurs> and <m axoccurs>). Choice Indicator

The <choice> indicator specifies that a child element may appear or another sub-element may appear (either):

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

Tip: If you want to set child elements to occur any number of times, you can set <maxOccurs> (explained later) to unbounded (unlimited). 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 how often an element appears.

Note: For all "Order" and "group" indicators (any, all, choice, sequence, group name, and group reference), the default values for MaxOccurs and minOccurs are 1. maxOccurs Indicator

The <maxOccurs> indicator can specify the maximum number of times an element can occur:

<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="/> </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 the default value of MinOccurs is 1) and occurs up to 10 times. minOccurs Indicator

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

<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 in the "person" element, up to 10 times.

Tip: If you want to make an element appear unlimited, 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 >tony smith</full_name>
<child_name>Cecilie</child_name>
</person>

< person>
<full_name>david smith</full_name>
<child_name>Jogn</child_name>
<child_name>mike</child_name>
<child_name>kyle</child_name>
<child_ name>mary</child_name>
</person>

<person>
<full_name>michael Smith </full_name>
</person>

</persons>

The above XML file contains a root element named "persons". Within this root element, we have defined three "person" elements. Each person element must contain a "full_name" element, and it can contain as many as 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

The Group indicator is used to define the relevant batch elements. Element Group

Element groups are defined by the Group declaration:

<xs:group name= "group name" >
  ...
</xs:group>

You must define an all, choice, or sequence element within the group declaration. The following example defines a group named "Persongroup", which defines a set of elements that must appear in an exact 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 have defined the 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 by the AttributeGroup declaration:

<xs:attributegroup name= "group name" >
  ...
</xs:attributeGroup>

The following example defines an attribute group named "Personattrgroup":

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

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.