A simple XSD
The first thing to do when generating a new XML content
type is to create its XSD (XML Schema Definition). A XSD
provides the syntax and defines a way in which elements and attributes can be
represented in a XML document. It also advocates that the given XML document
should be of a specific format and specific data type. Therefore, a new
plain text file has to be created in the OpenCms VFS, usually in a module
folder.
Let's create a content
type with a very simple data structure: a title, one up to maximum three teasers and an editable
text should be part of this simple XML content type. The following code is taken
from the simple example in the documentation folder /alkacon-documentation/documentation-xmlcontent/simpleexample/simpleexample.xsd:
1 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
2
3 <xsd:include schemaLocation="opencms://opencms-xmlcontent.xsd"/>
4 <xsd:element name="XmlContentDocSimpleTypes" type="OpenCmsXmlContentDocSimpleTypes"/>
5
6 <xsd:complexType name="OpenCmsXmlContentDocSimpleTypes">
7 <xsd:sequence>
8 <xsd:element name="XmlContentDocSimpleType" type="OpenCmsXmlContentDocSimpleType" minOccurs="0" maxOccurs="unbounded"/>
9 </xsd:sequence>
10 </xsd:complexType>
11
12 <xsd:complexType name="OpenCmsXmlContentDocSimpleType">
13 <xsd:sequence>
14 <xsd:element name="Title" type="OpenCmsString" />
15 <xsd:element name="Teaser" type="OpenCmsString" maxOccurs="3"/>
16 <xsd:element name="Text" type="OpenCmsHtml" />
17 </xsd:sequence>
18 <xsd:attribute name="language" type="OpenCmsLocale" use="required"/>
19 </xsd:complexType>
20
21 </xsd:schema>
There are a few rules to
follow when creating XSDs in OpenCms, a line by line explanation is now following:
- Line 1: The root element provides the namespace
declaration, this is always the same for every OpenCms XML content XSD.
- Line 3: Inclusions of other schema definitions, this
one has to be included always, followed by optional nested content XSDs (this
feature is described later in the documentation).
- Line 4: The root element name and type of the XML
content is defined here. The element name has to end with an "s" (indicating a
plural), the type must have exactly the same term with the prefix "OpenCms".
- Line 6: Definition of the complex type of the root
element described in line 4. The sequence of this complex type describes one
element.
- Line 8: Description of the child element: name and
type follow the same restrictions as the root element and must have the same
name and type value, but the trailing "s" has to be omitted (indicating a
single element). The occurences of the element have to be set from none to
unbounded as shown in the example.
- Line 12: Start of the second complex type definition.
- Line 14-16: The element sequence can contain an unlimited number of
element definitions. Each element must specify the corresponding data type in
OpenCms and can contain restrictions of occurences (with minOccurs and maxOccurs). If you do not
enter minimum or maximum occurences, the element has to occur exactly
once. The definition of a default value (default) is possible, too.
- Line 18: The child element which is described by the second complex type
definition must have an attribute named "language" of the type
"OpenCmsLocale". This is fixed for every XML content except for nested
contents (later in this documentation).
Now our first simple XSD is complete.
Check out the example XML content simplecontent.html using
the described XSD. Edit the file to see the form based editing of the
structured content and use the "Edit controlcode" entry in the context menu of
the file to verify the generated XML.
Learn in the next step which data types are available for XML contents in OpenCms and which editor widgets can be used
for form based content editing.
|