XML Shema Study

http://www.w3schools.com/schema/schema_schema.asp

 

<?xml version="1.0"?> 


<xs:schema 
      xmlns:xs="http://www.w3.org/2001/XMLSchema"          targetNamespace="http://www.w3schools.com"       xmlns="http://www.w3schools.com" elementFormDefault="qualified"> 


</xs:schema> 

 

 

The following fragment:

xmlns:xs="http://www.w3.org/2001/XMLSchema" 

 

indicates that the elements and data types used in the schema come from the "http://www.w3.org/2001/XMLSchema" namespace. It also specifies that the elements and data types that come from the "http://www.w3.org/2001/XMLSchema" namespace should be prefixed with xs:

 

This fragment:

targetNamespace="http://www.w3schools.com" 

 

indicates that the elements defined by this schema (note, to, from, heading, body.) come from the "http://www.w3schools.com" namespace.

 

This fragment:

xmlns="http://www.w3schools.com" 

 

indicates that the default namespace is http://www.w3schools.com.

 

 

This fragment:

elementFormDefault="qualified" 

 

indicates that any elements used by the XML instance document which were declared in this schema must be namespace qualified.

 

 

 

 

Default and Fixed Values for Simple Elements

 

<xs:element name="color" type="xs:string" default="red"/>

 

<xs:element name="color" type="xs:string" fixed="red"/>

  

 

 

XSD Attributes

All attributes are declared as simple types.

If an element has attributes, it is considered to be of a complex type. But the attribute itself is always declared as a simple type.

 

<xs:attribute name="xxx" type="yyy"/> 

 

Default and Fixed Values for Attributes

 

<xs:attribute name="lang" type="xs:string" default="EN"/>

 

<xs:attribute name="lang" type="xs:string" fixed="EN"/>

 

Optional and Required Attributes

<xs:attribute name="lang" type="xs:string" use="required"/>

 

 

 

XSD Restrictions/Facets

Restrictions are used to define acceptable values for XML elements or attributes. Restrictions on XML elements are called facets.

 

Restrictions on Values

 

<xs:element name="age"><xs:simpleType>
  <xs:restriction base="xs:integer">
    <xs:minInclusive value="0"/>
    <xs:maxInclusive value="120"/>
  </xs:restriction>
</xs:simpleType></xs:element>  

 

 

Restrictions on a Set of Values

 

<xs:element name="car"><xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:enumeration value="Audi"/>
    <xs:enumeration value="Golf"/>
    <xs:enumeration value="BMW"/>
  </xs:restriction>
</xs:simpleType></xs:element> 

 

or

 

<xs:element name="car" type="carType"/>

<xs:simpleType name="carType">
  <xs:restriction base="xs:string">
    <xs:enumeration value="Audi"/>
    <xs:enumeration value="Golf"/>
    <xs:enumeration value="BMW"/>
  </xs:restriction>
</xs:simpleType>

 

 in the aboving , carType can be reused.

 

 

Restrictions on a Series of Values

<xs:element name="letter">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:pattern value="[a-z]"/>
    <xs:pattern value="[A-Z][A-Z][A-Z]"/>
    <xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/>
    <xs:pattern value="[xyz]"/>
    <xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/>
    <xs:pattern value="([a-z][A-Z])+"/>
    <xs:pattern value="([a-z])*"/>
    <xs:pattern value="male|female"/>
    <xs:pattern value="[a-zA-Z0-9]{8}"/>
    <xs:whiteSpace value="preserve"/>
    <xs:whiteSpace value="replace"/>
    <xs:whiteSpace value="collapse"/>
    <xs:length value="8"/>
  </xs:restriction>
</xs:simpleType></xs:element> 

 

 

 

复合类型:

 

继承使用复合类型。

 

<xs:element name="employee" type="fullpersoninfo"/><xs:complexType name="personinfo">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
  </xs:sequence>
</xs:complexType><xs:complexType name="fullpersoninfo">
  <xs:complexContent>
    <xs:extension base="personinfo">
      <xs:sequence>
        <xs:element name="address" type="xs:string"/>
        <xs:element name="city" type="xs:string"/>
        <xs:element name="country" type="xs:string"/>
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

 

 

 

 

XSD Complex Types Indicators

We can control HOW elements are to be used in documents with indicators.

Indicators

There are seven indicators:

Order indicators:

  • All
  • Choice
  • Sequence

Occurrence indicators:

  • maxOccurs
  • minOccurs

Group indicators:

  • Group name
  • attributeGroup name

All Indicator

The <all> indicator specifies that the child elements can appear in any order, and that each child element must occur 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>

 

The <choice> indicator specifies that either one child element or another can occur

 

<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

The <sequence> indicator specifies that the 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 Indicators

Occurrence indicators are used to define how often an element can occur.

Note: For all "Order" and "Group" indicators (any, all, choice, sequence, group name, and group reference) the default value for maxOccurs and minOccurs is 1.

 

maxOccurs Indicator

The <maxOccurs> indicator specifies 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="10"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

 

minOccurs Indicator

The <minOccurs> indicator specifies 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>

 

 

 

Group Indicators

Group indicators are used to define related sets of elements.

You must define an all, choice, or sequence element inside the group declaration.

After you have defined a 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 Groups

Attribute groups are defined with the attributeGroup declaration

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

 

 

XSD The <any> Element

The <any> element enables us to extend the XML document with elements not specified by the schema!

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

 

 

the following xml isvalid

<?xml version="1.0" encoding="ISO-8859-1"?><persons xmlns="http://www.microsoft.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:SchemaLocation="http://www.microsoft.com family.xsd
http://www.w3schools.com children.xsd">
<person>
<firstname>Hege</firstname>
<lastname>Refsnes</lastname>
<children>
  <childname>Cecilie</childname>
</children>
</person>
<person>
<firstname>Stale</firstname>
<lastname>Refsnes</lastname>
</person>
</persons> 

 

The <any> and <anyAttribute> elements are used to make EXTENSIBLE documents! They allow documents to contain additional elements that are not declared in the main XML schema.

 

 

XSD The <anyAttribute> Element

<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:anyAttribute/>
  </xs:complexType>
</xs:element>

 

 

<persons xmlns="http://www.microsoft.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:SchemaLocation="http://www.microsoft.com family.xsd
                                   http://www.w3schools.com attribute.xsd">

 

使用两个xsd文件。

 

 

XSD Element Substitution

With XML Schemas, one element can substitute another element.

 

<xs:element name="name" type="xs:string"/>
<xs:element name="navn" substitutionGroup="name"/>

<xs:complexType name="custinfo">
  <xs:sequence>
    <xs:element ref="name"/>
  </xs:sequence>
</xs:complexType>

<xs:element name="customer" type="custinfo"/>
<xs:element name="kunde" substitutionGroup="customer"/> 

 Valid xml can be the following:

 

<customer>
  <name>John Smith</name>
</customer>

<kunde>
  <navn>John Smith</navn>
</kunde> 

 

 

 

 

 

Divide the Schema Demo

 

Example XSD File:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"><xs:element name="shiporder">
 <xs:complexType>
  <xs:sequence>
   <xs:element name="orderperson" type="xs:string"/>
   <xs:element name="shipto">
    <xs:complexType>
     <xs:sequence>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="address" type="xs:string"/>
      <xs:element name="city" type="xs:string"/>
      <xs:element name="country" type="xs:string"/>
     </xs:sequence>
    </xs:complexType>
   </xs:element>
   <xs:element name="item" maxOccurs="unbounded">
    <xs:complexType>
     <xs:sequence>
      <xs:element name="title" type="xs:string"/>
      <xs:element name="note" type="xs:string" minOccurs="0"/>
      <xs:element name="quantity" type="xs:positiveInteger"/>
      <xs:element name="price" type="xs:decimal"/>
     </xs:sequence>
    </xs:complexType>
   </xs:element>
  </xs:sequence>
  <xs:attribute name="orderid" type="xs:string" use="required"/>
 </xs:complexType>
</xs:element></xs:schema>

 

 

Divided XSD Files

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- definition of simple elements -->
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>

<!-- definition of attributes -->
<xs:attribute name="orderid" type="xs:string"/>

<!-- definition of complex elements -->
<xs:element name="shipto">
 <xs:complexType>
  <xs:sequence>
   <xs:element ref="name"/>
   <xs:element ref="address"/>
   <xs:element ref="city"/>
   <xs:element ref="country"/>
  </xs:sequence>
 </xs:complexType>
</xs:element>

<xs:element name="item">
 <xs:complexType>
  <xs:sequence>
   <xs:element ref="title"/>
   <xs:element ref="note" minOccurs="0"/>
   <xs:element ref="quantity"/>
   <xs:element ref="price"/>
  </xs:sequence>
 </xs:complexType>
</xs:element>


<xs:element name="shiporder">
 <xs:complexType>
  <xs:sequence>
   <xs:element ref="orderperson"/>
   <xs:element ref="shipto"/>
   <xs:element ref="item" maxOccurs="unbounded"/>
  </xs:sequence>
  <xs:attribute ref="orderid" use="required"/>
 </xs:complexType>
</xs:element>
</xs:schema>

 

 

 

 

Using Named Types

 

<xs:simpleType name="stringtype">
 <xs:restriction base="xs:string"/>
</xs:simpleType>

 

  <xs:element name="title" type="stringtype"/>
  <xs:element name="note" type="stringtype" minOccurs="0"/>

 

 

 

 

XSD String Data Types

Define:

<xs:element name="customer" type="xs:string"/>

 Example:

<customer>	John Smith	</customer> 

Note: The XML processor will not modify the value if you use the string data type.

NormalizedString Data Type

Define:

<xs:element name="customer" type="xs:normalizedString"/>

Example:

<customer>	John Smith	</customer>
Note: In the example above the XML processor will replace the tabs with spaces.

 

Token Data Type

The token data type is also derived from the String data type.

The token data type also contains characters, but the XML processor will remove line feeds, carriage returns, tabs, leading and trailing spaces, and multiple spaces.

 

 

<xs:element name="customer" type="xs:token"/> 

 

 

String Data Types

Note that all of the data types below derive from the String data type (except for string itself)!

NameDescription
ENTITIES  
ENTITY  
ID A string that represents the ID attribute in XML (only used with schema attributes)
IDREF A string that represents the IDREF attribute in XML (only used with schema attributes)
IDREFS  
language A string that contains a valid language id
Name A string that contains a valid XML name
NCName  
NMTOKEN A string that represents the NMTOKEN attribute in XML (only used with schema attributes)
NMTOKENS  
normalizedString A string that does not contain line feeds, carriage returns, or tabs
QName  
string A string
token A string that does not contain line feeds, carriage returns, tabs, leading or trailing spaces, or multiple spaces

 

 

Restrictions on String Data Types

Restrictions that can be used with String data types:

  • enumeration
  • length
  • maxLength
  • minLength
  • pattern (NMTOKENS, IDREFS, and ENTITIES cannot use this constraint)
  • whiteSpace

 

 

Date and Time Data Types

NameDescription
date Defines a date value
dateTime Defines a date and time value
duration Defines a time interval
gDay Defines a part of a date - the day (DD)
gMonth Defines a part of a date - the month (MM)
gMonthDay Defines a part of a date - the month and day (MM-DD)
gYear Defines a part of a date - the year (YYYY)
gYearMonth Defines a part of a date - the year and month (YYYY-MM)
time Defines a time value

 

Restrictions on Date Data Types

Restrictions that can be used with Date data types:

  • enumeration
  • maxExclusive
  • maxInclusive
  • minExclusive
  • minInclusive
  • pattern
  • whiteSpace

Numeric Data Types

Note that all of the data types below derive from the Decimal data type (except for decimal itself)!

NameDescription
byte A signed 8-bit integer
decimal A decimal value
int A signed 32-bit integer
integer An integer value
long A signed 64-bit integer
negativeInteger An integer containing only negative values ( .., -2, -1.)
nonNegativeInteger An integer containing only non-negative values (0, 1, 2, ..)
nonPositiveInteger An integer containing only non-positive values (.., -2, -1, 0)
positiveInteger An integer containing only positive values (1, 2, ..)
short A signed 16-bit integer
unsignedLong An unsigned 64-bit integer
unsignedInt An unsigned 32-bit integer
unsignedShort An unsigned 16-bit integer
unsignedByte An unsigned 8-bit integer

 

 

Restrictions on Numeric Data Types

Restrictions that can be used with Numeric data types:

  • enumeration
  • fractionDigits
  • maxExclusive
  • maxInclusive
  • minExclusive
  • minInclusive
  • pattern
  • totalDigits
  • whiteSpace

 

 

XSD Miscellaneous Data Types

Other miscellaneous data types are boolean, base64Binary, hexBinary, float, double, anyURI, QName, and NOTATION.

 

 

Miscellaneous Data Types

NameDescription
anyURI  
base64Binary  
boolean  
double  
float  
hexBinary  
NOTATION  
QName  


Restrictions on Miscellaneous Data Types

Restrictions that can be used with the other data types:

  • enumeration (a Boolean data type cannot use this constraint)
  • length (a Boolean data type cannot use this constraint)
  • maxLength (a Boolean data type cannot use this constraint)
  • minLength (a Boolean data type cannot use this constraint)
  • pattern
  • whiteSpace

 

 

 

XSD Elements

ElementExplanation
all Specifies that the child elements can appear in any order. Each child element can occur 0 or 1 time
annotation Specifies the top-level element for schema comments
any Enables the author to extend the XML document with elements not specified by the schema
anyAttribute

Enables the author to extend the XML document with attributes not specified by the schema

appInfo Specifies information to be used by the application (must go inside annotation)
attribute Defines an attribute
attributeGroup Defines an attribute group to be used in complex type definitions
choice Allows only one of the elements contained in the <choice> declaration to be present within the containing element
complexContent Defines extensions or restrictions on a complex type that contains mixed content or elements only
complexType Defines a complex type element
documentation Defines text comments in a schema (must go inside annotation)
element Defines an element
extension Extends an existing simpleType or complexType element
field Specifies an XPath expression that specifies the value used to define an identity constraint
group Defines a group of elements to be used in complex type definitions
import Adds multiple schemas with different target namespace to a document
include Adds multiple schemas with the same target namespace to a document
key Specifies an attribute or element value as a key (unique, non-nullable, and always present) within the containing element in an instance document
keyref Specifies that an attribute or element value correspond to those of the specified key or unique element
list Defines a simple type element as a list of values
notation Describes the format of non-XML data within an XML document
redefine Redefines simple and complex types, groups, and attribute groups from an external schema
restriction Defines restrictions on a simpleType, simpleContent, or a complexContent
schema Defines the root element of a schema
selector Specifies an XPath expression that selects a set of elements for an identity constraint
sequence Specifies that the child elements must appear in a sequence. Each child element can occur from 0 to any number of times
simpleContent Contains extensions or restrictions on a text-only complex type or on a simple type as content and contains no elements
simpleType Defines a simple type and specifies the constraints and information about the values of attributes or text-only elements
union Defines a simple type as a collection (union) of values from specified simple data types
unique Defines that an element or an attribute value must be unique within the scope

 

 

 

XSD Restrictions/Facets for Datatypes

Look at XSD Restrictions!

ConstraintDescription
enumeration Defines a list of acceptable values
fractionDigits Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero
length Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero
maxExclusive Specifies the upper bounds for numeric values (the value must be less than this value)
maxInclusive Specifies the upper bounds for numeric values (the value must be less than or equal to this value)
maxLength Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero
minExclusive Specifies the lower bounds for numeric values (the value must be greater than this value)
minInclusive Specifies the lower bounds for numeric values (the value must be greater than or equal to this value)
minLength Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero
pattern Defines the exact sequence of characters that are acceptable
totalDigits Specifies the exact number of digits allowed. Must be greater than zero
whiteSpace Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled

你可能感兴趣的:(xml,Microsoft,asp,Go)