W3C home > Mailing lists > Public > xmlschema-dev@w3.org > October 2012

Here's how to empower instance document authors to create their own root element

From: Costello, Roger L. <costello@mitre.org>
Date: Sun, 14 Oct 2012 12:59:49 +0000
To: "xmlschema-dev@w3.org" <xmlschema-dev@w3.org>
Message-ID: <B5FEE00B53CF054AA8439027E8FE17751E67EB96@IMCMBX04.MITRE.ORG>
Hi Folks,

It is not necessary to declare a root element in your XML Schema. You can empower instance document authors to create their own root element. Here's how:

1. In the XML Schema create a complexType for the root element, but don't declare a root element.

2. Instance document authors create their own root element name. They add this attribute onto their root element: xsi:type

This is a powerful technique. The XML Schema is versatile since it can be repurposed for different things by instance document authors.

Example: Here is an XML Schema. Notice that there is no declaration of a root element, there is only a global complexType (Books):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:complexType name="Books">
            <xs:element name="Book" maxOccurs="unbounded">
                        <xs:element name="Title" type="xs:string"/>
                        <xs:element name="Author" type="xs:string"/>
                        <xs:element name="Date" type="xs:gYear"/>
                        <xs:element name="ISBN" type="xs:string"/>
                        <xs:element name="Publisher" type="xs:string"/>

Now each instance document author can name the root element anything he wants. One person may name the root element BookStore. Another may name the root element Library. And a third may name the root element Inventory. And so forth. 

This ability to repurpose the XML Schema is powerful.

Here is one instance document, where the root element is named BookStore:

<?xml version="1.0" encoding="UTF-8"?>
<BookStore xsi:type="Books"
    <Title>My Life and Times</Title>
    <Author>Paul McCartney</Author>
    <Publisher>McMillan Publishing</Publisher>
    <Title>Illusions The Adventures of a Reluctant Messiah</Title>
    <Author>Richard Bach</Author>
    <Publisher>Dell Publishing Co.</Publisher>
    <Title>The First and Last Freedom</Title>
    <Author>J. Krishnamurti</Author>
    <Publisher>Harper &amp; Row</Publisher>

Received on Sunday, 14 October 2012 13:00:17 UTC

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 23:16:02 UTC