Before you create complicated XML Schema
structures, you should first create a skeleton XML Schema model and save it in the
Eclipse workspace. You have the option of giving
the schema a namespace or not.
Creating an XML Schema file with no target namespace
As shown in Listing 1 below, the steps for creating an XML Schema file with no target
namespace are:
-
Create the
org.eclipse.emf.ecore.resource.Resource using
the new xml schema Uri
- Create the XSDSchema root object
-
Set up the schema for schema namespace
information on the root XSDSchema object
-
Call updateElement() on the root XSDSchema
object
-
Add the root XSDSchema object to the resource you
created in step 1
- Save the resource
Listing 1. Creating an XML Schema file with no target namespace
org.eclipse.xsd.examples.command.CreateXSDWithNoTNSOperation
public XSDSchema createXSDSchema(IFile xsdFile)
{
try
{
//Get the URI of the model file.
URI fileURI = URI.createPlatformResourceURI(xsdFile.getFullPath().toString());
//Create a resource set to manage the different resources
ResourceSet resourceSet = new ResourceSetImpl();
//Create a resource for this file.
Resource resource = resourceSet.createResource(fileURI);
//Create the root XSDSchema object
XSDSchema xsdSchema = XSDFactory.eINSTANCE.createXSDSchema();
//set the schema for schema QName prefix to "xsd"
xsdSchema.setSchemaForSchemaQNamePrefix("xsd");
java.util.Map qNamePrefixToNamespaceMap = xsdSchema.getQNamePrefixToNamespaceMap();
//put the following namespace in the root schema namespace map
//xsd:http://www.w3.org/2001/XMLSchema
qNamePrefixToNamespaceMap.put(xsdSchema.getSchemaForSchemaQNamePrefix(),
XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001);
//We call updateElement to synchronize the MOF model with the underlying DOM model
//This should only have to be done after creating a new model
xsdSchema.updateElement();
//Add the root schema to the resource that was created above
resource.getContents().add(xsdSchema);
//Save the contents of the resource to the file system.
resource.save(Collections.EMPTY_MAP);
return xsdSchema;
}
catch (Exception exception)
{
exception.printStackTrace();
}
return null;
}
Creating an XML Schema file with target namespace
"http://www.eclipse.org/xsd/examples/createxsd"
As shown in Listing 2, the basic
steps in creating an XML Schema file with a target
namespace are very similar to creating aa XML Schema file
with no target namespace. The major difference is that
you set the targetNamespace() on the XSDSchema root, and
you also add a defined prefix to the root schema
QName prefix namespace map, which maps to the new target namespace.
Listing 2. Creating an XML Schema file with a target namespace
org.eclipse.xsd.examples.command.CreateXSDWithTNSOperation
public XSDSchema createXSDSchema(IFile xsdFile)
{
try
{
//Get the URI of the model file.
URI fileURI = URI.createPlatformResourceURI(xsdFile.getFullPath().toString());
//Create a resource set to manage the different resources
ResourceSet resourceSet = new ResourceSetImpl();
//Create a resource for this file.
Resource resource = resourceSet.createResource(fileURI);
//Create the root XSDSchema object
XSDSchema xsdSchema = XSDFactory.eINSTANCE.createXSDSchema();
//Set the target namespace of the given schema document to
//http://www.eclipse.org/xsd/examples/createxsd
xsdSchema.setTargetNamespace("http://www.eclipse.org/xsd/examples/createxsd");
java.util.Map qNamePrefixToNamespaceMap = xsdSchema.getQNamePrefixToNamespaceMap();
qNamePrefixToNamespaceMap.put(xsdSchema.getSchemaForSchemaQNamePrefix(),
XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001);
//put the following namespace in the root schema namespace map
//createxsd:http://www.eclipse.org/xsd/examples/createxsd
qNamePrefixToNamespaceMap.put("createxsd", xsdSchema.getTargetNamespace());
//We call updateElement to synchronize the MOF model with the underlying DOM model
//This should only have to be done after creating a new model
xsdSchema.updateElement();
//Add the root schema to the resource that was created above
resource.getContents().add(xsdSchema);
// Save the contents of the resource to the file system.
resource.save(Collections.EMPTY_MAP);
return xsdSchema;
}
catch (Exception exception)
{
exception.printStackTrace();
}
return null;
}