| prev | table of contents | next |
Different document types (within one project) require different schemas. If the documents share common XML types, they should be written once, in a separate schema file, and re-used from there wherever they are required. The common definitions could be assembled into a namespace of their own, to avoid any conflicts with type or element names in the schemas where they are used. The XML schema snippets given below illustrate this approach.
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://astronomy.org/common"
xmlns:ast="http://astronomy.org/common">
<xsd:complexType name="BodyType">
</xsd:sequence>
<xsd:element name="name" type="xsd:ID">
<xsd:element name="mass" type="xsd:float">
<xsd:element name="radius" type="xsd:float">
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
The definitions from this schema can be used by importing
the schema file, e.g.:
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:ast="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://astronomy.org/solarsystem"
xmlns:ss="http://astronomy.org/solarsystem">
<xsd:import namespace="http://astronomy.org/common"
schemaLocation="common.xsd"/>
<xsd:complexType name="MoonType">
<xsd:extension base="ast:BodyType">
</xsd:sequence>
<xsd:element name="planet" type="xsd:IDREF">
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="PlanetType">
<xsd:extension base="ast:BodyType">
</xsd:sequence>
<xsd:element name="moon" type="ss:MoonType"
minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
The schema compiler derives the package names from the URIs given in
the namespace definitions, resulting in org.astronomy.common
and org.astronomy.solarsystem.
It should also be noted that it is not necessary to have one schema file
for each document type. Any top-level element definition (by some
xsd:element schema element) is a candidate for a root
element, and you may have as many as you like within a single schema.
The previous schema snippet might be extended with the following
element definitions:
<xsd:schema ...>
...
<xsd:element name="planet" type="ss:PlanetType"/>
<xsd:element name="solarSystem">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="planet" type="ss:PlanetType"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Marshalling either element is possible with a marshaller created
from the context based on the package org.astronomy.solarsystem.
Marshaller m = context.createMarshaller(); ObjectFactory of = new ObjectFactory(); // Create a single planet instance document. PlanetType planet = of.createPlanetType(); //... Add attributes and moons JAXBElement<PlanetType> jbe = of.createPlanet( planet ); m.marshal( jbe, System.out ); // Create a solar system. SolarSystem sol = of.createSolarSystem(); //... Add planets. m.marshal( sol, System.out );
| prev | table of contents | next |