XML Shema Study

本文介绍了XML Schema的基础概念,包括命名空间、默认与固定值、属性声明等。详细解析了简单类型与复杂类型的定义方法,以及如何使用限制和片段来定义元素和属性的有效值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 
IDA string that represents the ID attribute in XML (only used with schema attributes)
IDREFA string that represents the IDREF attribute in XML (only used with schema attributes)
IDREFS 
languageA string that contains a valid language id
NameA string that contains a valid XML name
NCName 
NMTOKENA string that represents the NMTOKEN attribute in XML (only used with schema attributes)
NMTOKENS 
normalizedStringA string that does not contain line feeds, carriage returns, or tabs
QName 
stringA string
tokenA 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
dateDefines a date value
dateTimeDefines a date and time value
durationDefines a time interval
gDayDefines a part of a date - the day (DD)
gMonthDefines a part of a date - the month (MM)
gMonthDayDefines a part of a date - the month and day (MM-DD)
gYearDefines a part of a date - the year (YYYY)
gYearMonthDefines a part of a date - the year and month (YYYY-MM)
timeDefines 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
byteA signed 8-bit integer
decimalA decimal value
intA signed 32-bit integer
integerAn integer value
longA signed 64-bit integer
negativeIntegerAn integer containing only negative values ( .., -2, -1.)
nonNegativeIntegerAn integer containing only non-negative values (0, 1, 2, ..)
nonPositiveIntegerAn integer containing only non-positive values (.., -2, -1, 0)
positiveIntegerAn integer containing only positive values (1, 2, ..)
shortA signed 16-bit integer
unsignedLongAn unsigned 64-bit integer
unsignedIntAn unsigned 32-bit integer
unsignedShortAn unsigned 16-bit integer
unsignedByteAn 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
allSpecifies that the child elements can appear in any order. Each child element can occur 0 or 1 time
annotationSpecifies the top-level element for schema comments
anyEnables 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

appInfoSpecifies information to be used by the application (must go inside annotation)
attributeDefines an attribute
attributeGroupDefines an attribute group to be used in complex type definitions
choiceAllows only one of the elements contained in the <choice> declaration to be present within the containing element
complexContentDefines extensions or restrictions on a complex type that contains mixed content or elements only
complexTypeDefines a complex type element
documentationDefines text comments in a schema (must go inside annotation)
elementDefines an element
extensionExtends an existing simpleType or complexType element
fieldSpecifies an XPath expression that specifies the value used to define an identity constraint
groupDefines a group of elements to be used in complex type definitions
importAdds multiple schemas with different target namespace to a document
includeAdds multiple schemas with the same target namespace to a document
keySpecifies an attribute or element value as a key (unique, non-nullable, and always present) within the containing element in an instance document
keyrefSpecifies that an attribute or element value correspond to those of the specified key or unique element
listDefines a simple type element as a list of values
notationDescribes the format of non-XML data within an XML document
redefineRedefines simple and complex types, groups, and attribute groups from an external schema
restrictionDefines restrictions on a simpleType, simpleContent, or a complexContent
schemaDefines the root element of a schema
selectorSpecifies an XPath expression that selects a set of elements for an identity constraint
sequenceSpecifies that the child elements must appear in a sequence. Each child element can occur from 0 to any number of times
simpleContentContains extensions or restrictions on a text-only complex type or on a simple type as content and contains no elements
simpleTypeDefines a simple type and specifies the constraints and information about the values of attributes or text-only elements
unionDefines a simple type as a collection (union) of values from specified simple data types
uniqueDefines that an element or an attribute value must be unique within the scope

 

 

 

XSD Restrictions/Facets for Datatypes

Look at XSD Restrictions!

ConstraintDescription
enumerationDefines a list of acceptable values
fractionDigitsSpecifies the maximum number of decimal places allowed. Must be equal to or greater than zero
lengthSpecifies the exact number of characters or list items allowed. Must be equal to or greater than zero
maxExclusiveSpecifies the upper bounds for numeric values (the value must be less than this value)
maxInclusiveSpecifies the upper bounds for numeric values (the value must be less than or equal to this value)
maxLengthSpecifies the maximum number of characters or list items allowed. Must be equal to or greater than zero
minExclusiveSpecifies the lower bounds for numeric values (the value must be greater than this value)
minInclusiveSpecifies the lower bounds for numeric values (the value must be greater than or equal to this value)
minLengthSpecifies the minimum number of characters or list items allowed. Must be equal to or greater than zero
patternDefines the exact sequence of characters that are acceptable
totalDigitsSpecifies the exact number of digits allowed. Must be greater than zero
whiteSpaceSpecifies how white space (line feeds, tabs, spaces, and carriage returns) is handled

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值