XML Tutorial/XML Schema/Russian Doll — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 08:26, 26 мая 2010
Содержание
A book element of our library
File:Data.xml
<book xmlns="http://www.wbex.ru"
identifier="isbn-0836217462">
<isbn>0836217462</isbn>
<title>Java</title>
<author-ref ref="anotherRef" />
<chapter-refs>
ref
</chapter-refs>
</book>
File: Schema.xsd
<?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="book">
<xs:complexType>
<xs:sequence>
<xs:element name="isbn" type="xs:string" />
<xs:element name="title" type="xs:string" />
<xs:element name="author-ref">
<xs:complexType>
<xs:attribute name="ref" type="xs:string"
use="required" />
</xs:complexType>
</xs:element>
<xs:element name="chapter-refs" type="xs:string" />
</xs:sequence>
<xs:attribute name="identifier" type="xs:ID" use="required" />
</xs:complexType>
</xs:element>
</xs:schema>
Adapting the structure of your document
The result is instance documents such as:
<?xml version="1.0"?>
<library>
<book id="b1111111111" available="true">
<title lang="en">Java</title>
<isbn>1111111111</isbn>
<authors>
<author id="Client">
<start>1922-11-26</start>
<dead>2000-02-12</dead>
<name>James</name>
</author>
</authors>
<chapters>
<chapter id="PP">
<name>JButton</name>
<qualification>Yes</qualification>
<start>2005-08-22</start>
</chapter>
<chapter id="JTable">
<start>2001-10-04</start>
<name>JTable</name>
<qualification>Yes</qualification>
</chapter>
</chapters>
</book>
</library>
This instance document defined by a full schema, which could be:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="name" type="xs:token" />
<xs:element name="qualification" type="xs:token" />
<xs:element name="start" type="xs:date" />
<xs:element name="dead" type="xs:date" />
<xs:element name="isbn" type="xs:NMTOKEN" />
<xs:attribute name="id" type="xs:ID" />
<xs:attribute name="available" type="xs:boolean" />
<xs:attribute name="lang" type="xs:language" />
<xs:element name="title">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:token">
<xs:attribute ref="lang" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element ref="book" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="authors">
<xs:complexType>
<xs:sequence>
<xs:element ref="author" minOccurs="0"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="author">
<xs:complexType>
<xs:all>
<xs:element ref="name" />
<xs:element ref="start" />
<xs:element ref="dead" minOccurs="0" />
</xs:all>
<xs:attribute ref="id" />
</xs:complexType>
</xs:element>
<xs:element name="book">
<xs:complexType>
<xs:all>
<xs:element ref="isbn" />
<xs:element ref="title" />
<xs:element ref="authors" />
<xs:element ref="chapters" />
</xs:all>
<xs:attribute ref="id" />
<xs:attribute ref="available" />
</xs:complexType>
</xs:element>
<xs:element name="chapters">
<xs:complexType>
<xs:sequence>
<xs:element ref="chapter" minOccurs="0"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="chapter">
<xs:complexType>
<xs:all>
<xs:element ref="name" />
<xs:element ref="start" />
<xs:element ref="qualification" />
</xs:all>
<xs:attribute ref="id" />
</xs:complexType>
</xs:element>
</xs:schema>
Russian Doll and Object-Oriented Design
The style of defining elements and attributes locally is often called the Russian doll design.
Since the definition of each element is embedded in the definition of its parent.
Objects are created locally where they are needed as opposed to being created globally and cloned when we need them.
File: Data.xml
<?xml version="1.0"?>
<library>
<book id="b01111111111" available="true">
<isbn>1111111111</isbn>
<title lang="en">Java</title>
<author id="CMS">
<name>Swing</name>
<start>1996-11-26</start>
<dead>2008-02-12</dead>
</author>
<chapter id="PP">
<name>JButton</name>
<start>2001-08-22</start>
<qualification>Yes</qualification>
</chapter>
</book>
</library>
File: Schema.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element name="book" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="isbn" type="xs:integer"/>
<xs:element name="title">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="lang" type="xs:language"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="author" minOccurs="0"
maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="start" type="xs:date"/>
<xs:element name="dead" type="xs:date"/>
</xs:sequence>
<xs:attribute name="id" type="xs:ID"/>
</xs:complexType>
</xs:element>
<xs:element name="chapter" minOccurs="0"
maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="start" type="xs:date"/>
<xs:element name="qualification" type="xs:string"/>
</xs:sequence>
<xs:attribute name="id" type="xs:ID"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="id" type="xs:ID"/>
<xs:attribute name="available" type="xs:boolean"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Russian Doll design nest the local element declaration within other schema component
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://www.wbex.ru/namespaces/pub"
xmlns="http://www.wbex.ru/namespaces/pub">
<xsd:element name="publications">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="book" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="author" type="xsd:string"/>
<xsd:element name="description" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="isbn" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Russian doll type element
File: Data.xml
<?xml version="1.0"?>
<bk:Books xmlns:bk="http://www.wbex.ru"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.wbex.ru
Books.xsd">
<Book>
<Title>title 1</Title>
<Author>author 1</Author>
<Date>1998</Date>
<ISBN>1-11111-111-1</ISBN>
<Publisher>publisher 1</Publisher>
</Book>
<Book>
<Title>title 2</Title>
<Author>author 2</Author>
<Date>1977</Date>
<ISBN>2-222-22222-2</ISBN>
<Publisher>publisher 2</Publisher>
</Book>
</bk:Books>
File: Books.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="unqualified">
<xsd:element name="Books">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Book" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<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:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>