Generating Code from XML Schemas or DTDs

www.altova.com Print this Topic Previous Page Up One Level Next page

Home >  Code Generator >

Generating Code from XML Schemas or DTDs

When you generate code from a mapping, MapForce generates a complete application that executes all steps of the mapping automatically. Optionally, you can generate libraries for all the XML schemas used in the mapping. These allow your code to easily create or read XML instances that are used or created by the mapping code.

 

To generate libraries for all the XML schemas used in the mapping, select the Generate Wrapper Classes check box in the Options dialog (see Code Generator Options ). Next time when you generate code, MapForce will create not only the mapping application, but also wrapper classes for all schemas used in the mapping, as follows:

 

C++ or C#

Java

Purpose

Altova

com.altova

Base library containing common runtime support, identical for every schema.

AltovaXML

com.altova.xml

Base library containing runtime support for XML, identical for every schema.

YourSchema

com.YourSchema

A library containing declarations generated from the input schema, named as the schema file or DTD. This library is a DOM (W3C Document Object Model) wrapper that allows you to read, modify and create XML documents easily and safely. All data is held inside the DOM, and there are methods for extracting data from the DOM, and to update and create data into the DOM.

 

The generated C++ code supports either Microsoft MSXML or Apache Xerces 3. The syntax for using the generated code is identical for both DOM implementations.

 

The generated C# code uses the .NET standard System.XML library as the underlying DOM implementation.

 

The generated Java code uses JAXP (Java API for XML Processing) as the underlying DOM interface.

 

While prototyping an application from a frequently changing XML schema, you may need to frequently generate code to the same directory, so that the schema changes are immediately reflected in the code. Note that the generated test application and the Altova libraries are overwritten every time when you generate code into the same target directory. Therefore do not add code to the generated test application. Instead, integrate the Altova libraries into your project (see Integrating Schema Wrapper Libraries).

 

In addition to the base libraries listed above, some supporting libraries are also generated. The supporting libraries are used by the Altova base libraries and are not meant for custom integrations, since they are subject to change.

 

Name generation and namespaces

MapForce generates classes corresponding to all declared elements or complex types which redefine any complex type in your XML Schema, preserving the class derivation as defined by extensions of complex types in your XML Schema. In the case of complex schemas which import schema components from multiple namespaces, MapForce preserves this information by generating the appropriate C# or C++ namespaces or Java packages.

 

Generally, the code generator tries to preserve the names for generated namespaces, classes and members from the original XML Schema. Characters that are not valid in identifiers in the target language are replaced by a "_". Names that would collide with other names or reserved words are made unique by appending a number. Name generation can be influenced by changing the default settings in the SPL (Spy Programming Language) template.

 

The namespaces from the XML Schema are converted to packages in Java or namespaces in C# or C++ code, using the namespace prefix from the schema as code namespace. The complete library is enclosed in a package or namespace derived from the schema file name, so you can use multiple generated libraries in one program without name conflicts.

 

Data Types

XML Schema has a more elaborate data type model than Java, C# or C++. Code Generator converts the built-in XML Schema types to language-specific primitive types, or to classes delivered with the Altova library. Complex types and derived types defined in the schema are converted to classes in the generated library. Enumeration facets from simple types are converted to symbolic constants.

 

The mapping of simple types can be configured in the SPL template, see SPL (Spy Programming Language).

 

If your XML instance files use schema types related to time and duration, these are converted to Altova native classes in the generated code. For information about the Altova library classes, see:

 

Reference to Generated Classes (C++)
Reference to Generated Classes (C#)
Reference to Generated Classes (Java)

 

For information about type conversion and other details applicable to each language, see:

 

About Schema Wrapper Libraries (C++)
About Schema Wrapper Libraries (C#)
About Schema Wrapper Libraries (Java)

 

Memory management

A DOM tree is comprised of nodes, which are always owned by a specific DOM document - even if the node is not currently part of the document's content. All generated classes are references to the DOM nodes they represent, not values. This means that assigning an instance of a generated class does not copy the value, it only creates an additional reference to the same data.

 

XML Schema support

The following XML Schema constructs are translated into code:

 

a) XML namespaces

 

b) Simple types:

 

Built-in XML schema types
Simple types derived by extension
Simple types derived by restriction
Facets
Enumerations
Patterns

 

c) Complex types:

 

Built-in anyType node
User-defined complex types
Derived by extension: Mapped to derived classes
Derived by restriction
Complex content
Simple content
Mixed content

 

The following advanced XML Schema features are not supported (or not fully supported) in generated wrapper classes:

 

Wildcards: xs:any and xs:anyAttribute
Content models (sequence, choice, all). Top-level compositor is available in SPL (Spy Programming Language), but is not enforced by generated classes.
Default and fixed values for attributes. These are available in SPL (Spy Programming Language), but are not set or enforced by generated classes.
The attributes xsi:type, abstract types. When you need to write the xsi:type attribute, use the SetXsiType() method of the generated classes.
Union types: not all combinations are supported.
Substitution groups are partially supported (resolved like "choice").
Attribute nillable="true" and xsi:nil
Uniqueness constraints
Identity constraints (key and keyref)

© 2019 Altova GmbH