Step 3: Create a Custom XML Schema
To illustrate an example of creating an XML Schema for a custom DocBook framework, suppose the documents are
either articles or books, and composed of sections. The sections
may contain <title>
, <para>
,
<figure>
, <table>
, and other
<section>
elements. To complete the picture, each section includes a
<def>
element from another namespace.
The first schema file:
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.oxygenxml.com/sample/documentation" xmlns:doc="http://www.oxygenxml.com/sample/documentation" xmlns:abs="http://www.oxygenxml.com/sample/documentation/abstracts" elementFormDefault="qualified"> <xs:import namespace= "http://www.oxygenxml.com/sample/documentation/abstracts" schemaLocation= "abs.xsd"/>
The namespace of the documents will be
http://www.oxygenxml.com/sample/documentation
. The namespace of the
<def>
element is
http://www.oxygenxml.com/sample/documentation/abstracts
.
Next, the structure of the sections is defined. They all start with a
<title>
, then have the optional <def>
element
then either a sequence of other sections, or a mixture of paragraphs, images, and tables.
<xs:element name="book" type="doc:sectionType"/> <xs:element name="article" type="doc:sectionType"/> <xs:element name="section" type="doc:sectionType"/> <xs:complexType name="sectionType"> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element ref="abs:def" minOccurs="0"/> <xs:choice> <xs:sequence> <xs:element ref="doc:section" maxOccurs="unbounded"/> </xs:sequence> <xs:choice maxOccurs="unbounded"> <xs:element ref="doc:para"/> <xs:element ref="doc:image"/> <xs:element ref="doc:table"/> </xs:choice> </xs:choice> </xs:sequence> </xs:complexType>
The paragraph contains text and other custom styling markup, such as bold
(<b>
) and italic (<i>
) elements.
<xs:element name="para" type="doc:paragraphType"/> <xs:complexType name="paragraphType" mixed="true"> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="emphasis"/> <xs:element name="i"/> </xs:choice> </xs:complexType>
The <image>
element has an attribute with a reference to the file
containing image data.
<xs:element name="image"> <xs:complexType> <xs:attribute name="href" type="xs:anyURI" use="required"/> </xs:complexType> </xs:element>
The <table>
element contains a header row and then a sequence of rows
(<tr>
elements) each of them containing the cells. Each cell has the
same content as the paragraphs.
<xs:element name="table"> <xs:complexType> <xs:sequence> <xs:element name="header"> <xs:complexType> <xs:sequence> <xs:element name="td" maxOccurs="unbounded" type="doc:paragraphType"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="tr" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="td" type="doc:tdType" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> <xs:complexType name="tdType"> <xs:complexContent> <xs:extension base="doc:paragraphType"> <xs:attribute name="row_span" type="xs:integer"/> <xs:attribute name="column_span" type="xs:integer"/> </xs:extension> </xs:complexContent> </xs:complexType>
The <def>
element is defined as a text only element in the imported
schema abs.xsd:
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace= "http://www.oxygenxml.com/sample/documentation/abstracts"> <xs:element name="def" type="xs:string"/> </xs:schema>
Now the XML data structure will be styled.