More Complex Schema Example
Consider the following scenario. The Person class is as follows:
Class SchemaWriter.Person Extends (%Persistent, %XML.Adaptor)
{
Parameter NAMESPACE = "http://www.myapp.com";
Property Name As %Name;
Property DOB As %Date(FORMAT = 5);
Property PatientID as %String;
Property HomeAddress as Address;
Property OtherAddress as AddressOtherNS ;
}
The Address class is defined to be in the same XML namespace ("http://www.myapp.com"), and the OtherAddress class is defined to be in a different XML namespace ("http://www.other.com").
The Company class is also defined to be in the XML namespace "http://www.myapp.com". It is defined as follows:
Class SchemaWriter.Company Extends (%Persistent, %XML.Adaptor)
{
Parameter NAMESPACE = "http://www.myapp.com";
Property Name As %String;
Property CompanyID As %String;
Property HomeOffice As Address;
}
Notice that there is no property relationship that connects the Person and Company classes.
To generate a schema for the namespace "http://www.myapp.com", we could use the following:
ClassMethod Demo()
{
Set schema=##class(%XML.Schema).%New()
Set schema.DefaultNamespace="http://www.myapp.com"
Set status=schema.AddSchemaType("SchemaWriter.Person")
Set status=schema.AddSchemaType("SchemaWriter.Company")
Do schema.DefineLocation("http://www.other.com","c:/other-schema.xsd")
Set schema=schema.GetSchema("http://www.myapp.com")
//create writer
Set writer=##class(%XML.Writer).%New()
Set writer.Indent=1
Do writer.AddSchemaNamespace()
Do writer.AddNamespace("http://www.myapp.com")
Do writer.AddNamespace("http://www.other.com")
Set status=writer.DocumentNode(schema)
If $$$ISERR(status) {Do $system.OBJ.DisplayError() Quit }
}
The output is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:s="http://www.w3.org/2001/XMLSchema"
xmlns:s01="http://www.myapp.com"
xmlns:s02="http://www.other.com"
elementFormDefault="qualified"
targetNamespace="http://www.myapp.com">
<import namespace="http://www.other.com" schemaLocation="c:/other-schema.xsd"/>
<complexType name="Person">
<sequence>
<element minOccurs="0" name="Name" type="s:string"/>
<element minOccurs="0" name="DOB" type="s:date"/>
<element minOccurs="0" name="PatientID" type="s:string"/>
<element minOccurs="0" name="HomeAddress" type="s01:Address"/>
<element minOccurs="0" name="OtherAddress" type="s02:AddressOtherNS"/>
</sequence>
</complexType>
<complexType name="Address">
<sequence>
<element minOccurs="0" name="State">
<simpleType>
<restriction base="s:string">
<maxLength value="2"/>
</restriction>
</simpleType>
</element>
<element minOccurs="0" name="Zip">
<simpleType>
<restriction base="s:string">
<maxLength value="10"/>
</restriction>
</simpleType>
</element>
</sequence>
</complexType>
<complexType name="Company">
<sequence>
<element minOccurs="0" name="Name" type="s:string"/>
<element minOccurs="0" name="CompanyID" type="s:string"/>
<element minOccurs="0" name="HomeOffice" type="s01:Address"/>
</sequence>
</complexType>
</schema>
Notice the following:
-
The schema includes types for Person and all its referenced classes, as well as Company and all its referenced classes.
-
The <import> directive imports the namespace used by the OtherAddress class; because we used DefineLocation(), this directive also indicates the location of the corresponding schema.
-
Because we used AddSchemaNamespace() and AddNamespace() just before calling DocumentNode(), the <schema> element includes namespace declarations, which define prefixes for these namespaces.
If we had not used AddSchemaNamespace() and AddNamespace(), <schema> would not include these namespace declarations, and the schema would instead have been as follows:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://www.myapp.com">
<import namespace="http://www.other.com" schemaLocation="c:/other-schema.xsd"/>
<complexType name="Person">
<sequence>
<element minOccurs="0" name="Name" type="s01:string" xmlns:s01="http://www.w3.org/2001/XMLSchema"/>
<element minOccurs="0" name="DOB" type="s02:date" xmlns:s02="http://www.w3.org/2001/XMLSchema"/>
<element minOccurs="0" name="PatientID" type="s03:string" xmlns:s03="http://www.w3.org/2001/XMLSchema"/>
<element minOccurs="0" name="HomeAddress" type="s04:Address" xmlns:s04="http://www.myapp.com"/>
<element minOccurs="0" name="OtherAddress" type="s05:AddressOtherNS" xmlns:s05="http://www.other.com"/>
</sequence>
</complexType>
<complexType name="Address">
<sequence>
<element minOccurs="0" name="State">
<simpleType>
<restriction base="s06:string" xmlns:s06="http://www.w3.org/2001/XMLSchema">
...