<< Back - Alkacon logo

OpenCms 6.0 interactive documentation:

Step 5: Nested contents

OpenCms logo - Forward >>

Nested contents

So far, you are able to create simple XML contents for news messages or other needs. For most use cases, it is sufficient to build those "simple" XML contents. But sometimes it may be necessary to create more complex XML contents, e.g. if you need to enter address data more than once for a content.

The solution is to create a XML content definition that uses a second one defining the elements for address data. Have a look at the XSDs in the folder /xmlcontentdemo/nestedcontent/. We have a XSD bookmark.xsd using a nested XML content for addresses defining the following elements:

1  <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
	
2	<xsd:include schemaLocation="opencms://opencms-xmlcontent.xsd"/>
3	<xsd:include schemaLocation="opencms://sites/default/xmlcontent/nestedcontent/address.xsd"/>
	
4	<xsd:element name="Bookmarks" type="OpenCmsBookmarks"/>
	
5	<xsd:complexType name="OpenCmsBookmarks">
6		<xsd:sequence>
7			<xsd:element name="Bookmark" type="OpenCmsBookmark" minOccurs="0" maxOccurs="unbounded"/>
8		</xsd:sequence>
9	</xsd:complexType>

10	<xsd:complexType name="OpenCmsBookmark">
11		<xsd:sequence>
12			<xsd:element name="Title" type="OpenCmsString" />
13			<xsd:element name="Description" type="OpenCmsHtml" minOccurs="0" maxOccurs="1" />
14			<xsd:element name="Logo" type="OpenCmsVfsFile" minOccurs="0" />
15			<xsd:element name="Contact" type="OpenCmsAddress" minOccurs="0" maxOccurs="3" />
16		</xsd:sequence>
17		<xsd:attribute name="language" type="OpenCmsLocale" use="optional"/>
18	</xsd:complexType>

There are a few rules to follow when using nested contents. An explanation for the most important lines is following:

  • Line 3: The schema definition of the nested XML content has to be specified in the XML content definition that uses it.
  • Line 15: The type of the element node representing the nested content has to of the nested content inner complex type, in this case "OpenCmsAddress". 

The included XSD address.xsd has to define the nested complex type:

1  <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
	
2	<xsd:include schemaLocation="opencms://opencms-xmlcontent.xsd"/>	
3	<xsd:element name="Addresss" type="OpenCmsAddresss"/>
	
4	<xsd:complexType name="OpenCmsAddresss">
5		<xsd:sequence>
6			<xsd:element name="Address" type="OpenCmsAddress" minOccurs="0" maxOccurs="unbounded"/>
7		</xsd:sequence>
8	</xsd:complexType>

9	<xsd:complexType name="OpenCmsAddress">
10		<xsd:sequence>
			...
11		</xsd:sequence>
12		<xsd:attribute name="language" type="OpenCmsLocale" use="optional"/>
13	</xsd:complexType>

	...
	  
14 </xsd:schema>

The following lines are important to check when nesting XML contents:

  • Line 9: The name of the complex type that is given here has to be used in the XSD that includes the nested one. 
  • Line 12: The attribute value of the "use" attribute for the language has to be "optional". 

There are no limitations for nesting contents, it is possible to build three or even more nested levels. But the more nested contents are used, the more complex the automatically generated XML content editor will appear. Be aware that complex XML structures with a lot of nested contents may be too complex for the users to work with.

Note: When using nested XML contents, the mappings to properties or resource attributes and the default value definition have to be done  in the <appinfo> node of the outer XML content for all nested content types. Use the elements XPath to determine the element for which the mapping or default value is defined.

Congratulations! Now you can go on and try to create your own localized nested XML contents.

The last page shows how to display XML contents on the frontend of a web page.

©2005 Alkacon Software GmbH (http://www.alkacon.com) - The OpenCms experts