XML/XML Schema/Reference
Содержание
Nexted reference
File: Data.xml
<?xml version="1.0"?>
<images xmlns="http://www.wbex.ru"
xmlns:i="http://www.wbex.ru"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.wbex.ru Schema.xsd">
<image/>
<image i:src="http://www.wbex.ru"/>
</images>
File: Schema.xml
<?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:element name="image">
<xsd:complexType>
<xsd:attribute ref="src"/>
</xsd:complexType>
</xsd:element>
<xsd:attribute name="src">
<xsd:simpleType>
<xsd:restriction base="xsd:anyURI">
<xsd:enumeration value="http://www.wbex.ru"/>
<xsd:enumeration value="http://www.wbex.ru/style/logo.png"/>
<xsd:enumeration value="http://www.wbex.ru/isbn.html"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:element name="images">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="image" maxOccurs="2"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Reference defined data type with target namespace
File: Data.xml
<?xml version="1.0"?>
<employees
xmlns="http://www.wbex.ru/employees"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.wbex.ru/employees Schema.xsd"
source="source"
version="1.0">
<employee person="persiondata" tags="tag content">
<name>
<first>first</first>
<middle>middle</middle>
<last>last</last>
</name>
<location>
<latitude>1</latitude>
<longitude>2</longitude>
<address>USA</address>
</location>
<phone>1234567890</phone>
<description>data<em>data</em></description>
</employee>
</employees>
File: Schema.xsd
<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:employees="http://www.wbex.ru/employees"
targetNamespace="http://www.wbex.ru/employees"
elementFormDefault="qualified">
<attributeGroup name="employeeAttributes">
<attribute name="version" type="decimal" fixed="1.0" />
<attribute name="source" type="string"/>
</attributeGroup>
<element name="employees">
<complexType>
<sequence>
<element name="employee" minOccurs="0" maxOccurs="unbounded">
<complexType>
<sequence>
<element name="name" type="employees:NameType"/>
<element name="location" type="employees:LocationType"/>
<element name="phone" type="employees:PhoneType"/>
<element name="description" type="employees:DescriptionType"/>
</sequence>
<attribute name="tags" type="token"/>
<attribute name="person" type="ID"/>
</complexType>
</element>
</sequence>
<attributeGroup ref="employees:employeeAttributes"/>
</complexType>
</element>
<complexType name="NameType">
<group ref="employees:NameGroup"/>
<attribute name="title" type="string"/>
</complexType>
<group name="NameGroup">
<sequence>
<element name="first" type="string" minOccurs="1" maxOccurs="unbounded"/>
<element name="middle" type="string" minOccurs="0" maxOccurs="1"/>
<element name="last" type="string"/>
</sequence>
</group>
<complexType name="LocationType">
<choice minOccurs="0" maxOccurs="unbounded">
<element name="address" type="string"/>
<sequence>
<element name="latitude" type="float"/>
<element name="longitude" type="float"/>
</sequence>
</choice>
</complexType>
<complexType name="PhoneType">
<simpleContent>
<extension base="string">
<attribute name="kind" default="Home">
<simpleType>
<restriction base="string">
<enumeration value="Home"/>
<enumeration value="Work"/>
<enumeration value="Cell"/>
<enumeration value="Fax"/>
</restriction>
</simpleType>
</attribute>
</extension>
</simpleContent>
</complexType>
<complexType name="DescriptionType" mixed="true">
<choice minOccurs="0" maxOccurs="unbounded">
<element name="em" type="string"/>
<element name="strong" type="string"/>
<element name="br" type="string"/>
</choice>
</complexType>
</schema>
reference element
<?xml version="1.0"?>
<name xmlns="http://www.wbex.ru/name"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.wbex.ru/name Schema.xsd"
title="Mr.">
<first>first</first>
<middle>middle</middle>
<last>last</last>
</name>
File: Schema.xsd
<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:target="http://www.wbex.ru/name"
targetNamespace="http://www.wbex.ru/name"
elementFormDefault="qualified">
<element name="first" type="string" />
<element name="middle" type="string" />
<element name="last" type="string" />
<complexType name="NameType">
<sequence>
<element ref="target:first" />
<element ref="target:middle" />
<element ref="target:last" />
</sequence>
<attribute name="title" type="string" />
</complexType>
<element name="name" type="target:NameType" />
</schema>
reference with maxOccurs
File: Data.xml
<?xml version="1.0"?>
<ElementExamples xmlns="http://www.wbex.ru"
xmlns:ex="http://www.wbex.ru"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.wbex.ru
Schema.xsd">
<aircraft>F-16</aircraft>
</ElementExamples>
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:element name="aircraft" type="xsd:string"/>
<xsd:element name="ElementExamples">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="aircraft" maxOccurs="2"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Reference your type with namespace
File: Data.xml
<?xml version="1.0"?>
<Books xmlns="http://www.wbex.ru"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.wbex.ru Schema.xsd">
<Book>
<Title>title 1</Title>
<Author>author 1</Author>
<Date>2008</Date>
<ISBN>1-11111-111-1</ISBN>
<Publisher>Publisher 1</Publisher>
</Book>
<Book>
<Title>title 2</Title>
<Author>author 2</Author>
<Date>2007</Date>
<ISBN>0-111-11111-1</ISBN>
<Publisher>Publisher 2</Publisher>
</Book>
<Book>
<Title>title 3</Title>
<Author>author 3</Author>
<Date>2004</Date>
<ISBN>0-11-111111-1</ISBN>
<Publisher>Publisher 3</Publisher>
</Book>
</Books>
File: Schema.xml
<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.wbex.ru"
xmlns:bk="http://www.wbex.ru"
elementFormDefault="qualified">
<element name="Books">
<complexType>
<sequence>
<element ref="bk:Book" minOccurs="1" maxOccurs="unbounded"/>
</sequence>
</complexType>
</element>
<element name="Book">
<complexType>
<sequence>
<element ref="bk:Title"/>
<element ref="bk:Author"/>
<element ref="bk:Date"/>
<element ref="bk:ISBN"/>
<element ref="bk:Publisher"/>
</sequence>
</complexType>
</element>
<element name="Title" type="string"/>
<element name="Author" type="string"/>
<element name="Date" type="string"/>
<element name="ISBN" type="string"/>
<element name="Publisher" type="string"/>
</schema>
Refernece anonymous complexType
File: Data.xml
<?xml version="1.0"?>
<images xmlns="http://www.wbex.ru"
xmlns:ex="http://www.wbex.ru"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.wbex.ru Schema.xsd">
<image/>
<image src="http://www.wbex.ru"/>
</images>
File: Schema.xml
<?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:element name="image">
<xsd:complexType>
<xsd:attribute name="src">
<xsd:simpleType>
<xsd:restriction base="xsd:anyURI">
<xsd:enumeration value="http://www.wbex.ru"/>
<xsd:enumeration value="http://www.wbex.ru/style/logo.png"/>
<xsd:enumeration value="http://www.wbex.ru/isbn.html"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
<xsd:element name="images">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="image" maxOccurs="2"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Set minOccurs, maxOccurs for referenced types
File: Data.xml
<?xml version="1.0"?>
<Books xmlns="http://www.wbex.ru"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.wbex.ru Schema.xsd">
<Book>
<Title>title 1</Title>
<Author>author 1</Author>
<Date>2008</Date>
<ISBN>1-11111-111-1</ISBN>
<Publisher>Publisher 1</Publisher>
</Book>
<Book>
<Title>title 2</Title>
<Author>author 2</Author>
<Date>2007</Date>
<ISBN>0-111-11111-1</ISBN>
<Publisher>Publisher 2</Publisher>
</Book>
<Book>
<Title>title 3</Title>
<Author>author 3</Author>
<Date>2004</Date>
<ISBN>0-11-111111-1</ISBN>
<Publisher>Publisher 3</Publisher>
</Book>
</Books>
File: Schema.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.wbex.ru"
xmlns="http://www.wbex.ru"
elementFormDefault="qualified">
<xsd:element name="Books">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Book" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Book">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Title" minOccurs="1" maxOccurs="1"/>
<xsd:element ref="Author" minOccurs="1" maxOccurs="1"/>
<xsd:element ref="Date" minOccurs="1" maxOccurs="1"/>
<xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/>
<xsd:element ref="Publisher" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Date" type="xsd:string"/>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:schema>
Use ref to remove the nested definition
File: Data.xml
<?xml version="1.0"?>
<ElementExamples xmlns="http://www.wbex.ru"
xmlns:ex="http://www.wbex.ru"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.wbex.ru
Schema.xsd">
<aircraft>
<type>F-16</type>
</aircraft>
</ElementExamples>
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:element name="aircraft">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="type" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="ElementExamples">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="aircraft" maxOccurs="2"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>