Programmatic adding of schemas

onion.net is designed to be able to modify schemas at runtime. You also have tools available for the transactional change of several schemas, as well as for mass data manipulation via XSLT. Like in the onion.net Editor, it is also not possible here to make a change to a schema if it contradicts the stock data in any form.

In addition to the content schema, onion.net also offers a structural schema. This indicates that only data objects of certain data types are permissible underneath a data object. Also, the modification of the structural schema is always checked against the stock data.

In the following example, an abstract root schema, a container schema and a schema for example data are created. The root and container schema serve for preventing disorder in your system. The root schema groups all schemas allocated to a purpose. In our case, it also provides a simple data type for re-use. The container schema is intended to form the structural root of all data allocated to a purpose.

using System;
using Onion.Client;

class Program
{
  private static void Main(string[] args)
  {
    using (var session = new OnionSession("ipc://onion.net/onion/server", "admin", "admin"))
    {
      // fetch some necessary system schemata
      ISchema rootSchema = session.Schemata["http://onionworks.net/2004/schema/systemroot"];
      ISchema userdefined = session.Schemata["http://onionworks.net/2004/schema/userdefined"];

      // create an abstract root schema for our test types including a simple type
      ISchema exampleRootType = userdefined.Children.Create(
        @"<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"" 
                     xmlns:onion=""http://onionworks.net/2004/schema""
                     onion:schemaLocation=""http://onion.net/2010/example"">
            <xs:simpleType name=""specialNumber"">
              <xs:restriction base=""xs:integer"">
                <xs:minInclusive value=""3"" />
                <xs:maxInclusive value=""8"" />
              </xs:restriction>
            </xs:simpleType>
          </xs:schema>",
        true);

      // create a container schema
      ISchema containerType = exampleRootType.Children.Create(
        @"<xs:schema elementFormDefault=""qualified""
                     xmlns:xs=""http://www.w3.org/2001/XMLSchema"" 
                     xmlns:onion=""http://onionworks.net/2004/schema""
                     onion:schemaLocation=""http://onion.net/2010/example/container"">
            <xs:element name=""container"" />
          </xs:schema>");

      // allow the container to be created below the system root
      rootSchema.StructureSchemaChildren.Add(containerType);

      // add a simple data schema using our root schema
      ISchema dataType = exampleRootType.Children.Create(
        @"<xs:schema elementFormDefault=""qualified""
                     xmlns:xs=""http://www.w3.org/2001/XMLSchema""
                     xmlns:onion=""http://onionworks.net/2004/schema""
                     onion:schemaLocation=""http://onion.net/2010/example/data"">
            <xs:include schemaLocation=""http://onion.net/2010/example"" />
            <xs:element name=""data"">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name=""text"" type=""xs:string"" />
                  <xs:element name=""number"" type=""specialNumber"" />
                  <xs:element name=""dateTime"" type=""xs:dateTime"" />
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:schema>");

      // allow the data schema to be created below the container
      containerType.StructureSchemaChildren.Add(dataType);
    }
  }
}

Over the property “IOnionSession.Schemata” you can get to all operations you need for editing the information model. The schemas are held in a hierarchical order. Self-contained models should be held below a root schema. XML schema offers the re-use of external schemas’ data types via the composition tool “xs:include” and “xs:import”. “xs:redefine” also belongs to this group and is the basis for object-oriented inheritance in onion.net.

After you have executed the program, you can create the container object in the onion.net Editor below the system root and any example data objects below that.