XML Tutorial/XML Schema/complexType
Содержание
- 1 Build complexType with complexType
- 2 Complex type for element with child elements and attribute
- 3 Complex type with sequence
- 4 Content Models
- 5 Defining Complex Types
- 6 Defining Elements to Contain Only Elements
- 7 Defining Elements with Mixed Content
- 8 To declare an empty content model in a <complexType> definition
- 9 When an element contains both child elements and character data, it follows the mixed content model
Build complexType with complexType
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <products xmlns ="http://www.wbex.ru"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.wbex.ru Schema.xsd"> <product> <description>description 1</description> <warranty>lifetime warranty</warranty> <name>name 1</name> <image>a.gif</image> <cost>41.95</cost> <retailer>http://www.wbex.ru</retailer> </product> <product> <description>description 1</description> <warranty>lifetime warranty</warranty> <name>name 2</name> <image>b.gif</image> <cost>239.00</cost> <retailer>http://www.wbex.ru</retailer> </product>
</products> File: Schema.xsd <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru" elementFormDefault="qualified"> <xsd:complexType name="appliance"> <xsd:sequence> <xsd:element name="description" type="xsd:string"/> <xsd:element name="warranty" type="xsd:string" minOccurs="0"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="productType"> <xsd:complexContent> <xsd:extension base="appliance"> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="image" type="imageType"/> <xsd:element name="weight" type="xsd:positiveInteger" minOccurs="0"/> <xsd:element name="cost" type="money" maxOccurs="unbounded"/> <xsd:element name="retailer" type="xsd:anyURI"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:element name="products"> <xsd:complexType> <xsd:sequence> <xsd:element name="product" type="productType" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:simpleType name="money"> <xsd:restriction base="xsd:decimal"> <xsd:fractionDigits value="2"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="imageType"> <xsd:restriction base="xsd:string"> <xsd:pattern value="(.)+\.(gif|jpg|jpeg|bmp)"/> </xsd:restriction> </xsd:simpleType>
</xsd:schema></source>
Complex type for element with child elements and attribute
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <BOOK InStock="true">
<TITLE>title 1</TITLE> <AUTHOR>author 1</AUTHOR> <BINDING>trade paperback</BINDING> <PAGES>473</PAGES> <PRICE>10.95</PRICE>
</BOOK> File: Schema.xsd
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="BOOK"> <xsd:complexType> <xsd:sequence> <xsd:element name="TITLE" type="xsd:string"/> <xsd:element name="AUTHOR" type="xsd:string"/> <xsd:element name="BINDING" type="xsd:string"/> <xsd:element name="PAGES" type="xsd:positiveInteger"/> <xsd:element name="PRICE" type="xsd:decimal"/> </xsd:sequence> <xsd:attribute name="InStock" type="xsd:boolean" use="required"/> </xsd:complexType> </xsd:element>
</xsd:schema>
If an element does not contain any character data, it?considered empty.
<xsd:element name="mark">
<xsd:complexType/>
</xsd:element></source>
Complex type with sequence
<source lang="xml">
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru" elementFormDefault="qualified"> <xs:element name="note"> <xs:complexType> <xs:sequence> <xs:element name="to" type="xs:string" /> <xs:element name="from" type="xs:string" /> <xs:element name="heading" type="xs:string" /> <xs:element name="body" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element>
</xs:schema></source>
Content Models
<source lang="xml">
In XML Schemas you can specify an element"s content model using the following: A <sequence>declaration A <choice>declaration A reference to a global <group>declaration An <all>declaration Each of these declarations may contain the following: Inner content models Element declarations Element wildcards</source>
Defining Complex Types
<source lang="xml">
An element that can contain other elements or that is allowed to contain attributes is a complex type. There are four kinds of elements of complex type: "element only" contain just other elements or attributes-but no text, "empty" elements contain attributes-but never elements or text, "mixed content" elements contain a combination of elements, attributes, and/or text-especially elements and text, "text only" elements contain only text-and possibly attributes. Within <complexType> definitions, you can specify the allowable element content for the declaration: When we created a local declaration, we did not include a name attribute in our <complexType> definition. Local <complexType> definitions are not named, which are called anonymous complex types. Global <complexType> definitions are always named, so that they can be identified later. <complexType> definitions can also be used to create mixed and empty content models. Mixed content models allow you to include both text and element content within a single content model. <element name="description">
<complexType mixed="true"> <choice minOccurs="0" maxOccurs="unbounded"> <element name="em" type="string"/> <element name="strong" type="string"/> <element name="br" type="string"/> </choice> </complexType>
</element></source>
Defining Elements to Contain Only Elements
<source lang="xml">
An element with the most basic of complex types can contain other elements, and possibly attributes as well-but not text. The elements contained within a complex type must be part of a sequence, choice, unordered group, or named group.
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru" elementFormDefault="qualified"> <xsd:complexType name="endspeciesType"> <xsd:sequence> <xsd:element name="animal" type="xsd:string" /> </xsd:sequence> </xsd:complexType>
</xsd:schema></source>
Defining Elements with Mixed Content
<source lang="xml">
When creating a complex type that will define such an element, you must declare that the content will be mixed.
File: Schema.xsd <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru" elementFormDefault="qualified"> <xsd:complexType name="paragraph" mixed="true"> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> </xsd:sequence> <xsd:attribute name="length" type="xsd:string" /> </xsd:complexType> <xsd:element name="description" type="paragraph" />
</xsd:schema>
File: Data.xml <description xmlns="http://www.wbex.ru" length="short">The <name>tiger</name> world. </description></source>
To declare an empty content model in a <complexType> definition
<source lang="xml">
Create the <complexType> definition without any <element> or content model declarations. <element name="knows">
<complexType> </complexType>
</element> <element name="knows">
<complexType/>
</element> In both cases, the <complexType> definition is empty, indicating that knows will not contain text or element children. When used in our instance document, <knows> must be empty. Even when you are declaring an empty element, attribute declarations may still appear within the <complexType>. For example, the following elements would be valid: <knows/> <knows></knows> <element name="knows">
<complexType> <attribute name="contacts" type="IDREFS"/> </complexType>
</element></source>
When an element contains both child elements and character data, it follows the mixed content model
<source lang="xml">
<message>
A <projectLead>B</projectLead> C <projectMember>D</projectMember> E <projectMember>F</projectMember>. G<client>H<client> I
</message> The declaration that should be used to define a mixed content model is as follows: <xsd:element name="message">
<xsd:complexType mixed="true"> <xsd:sequence> <xsd:element name="projectMember" type="xsd:string"/> <xsd:element name="projectLead" type="xsd:string"/> <xsd:element name="client" type="xsd:string"/> </xsd:sequence> </xsd:complexType>
</xsd:element></source>