Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
HydraExpress XML Binding Development Guide

8.2 Names and Identifiers

HydraExpress follows the process below to convert XML names to C++ identifiers.

Naming collisions may still occur due to the way that HydraExpress maps XML names to C++. The code generator attempts to work around some of these naming collisions, but does not always find a satisfactory resolution.

If the code generator does not successfully resolve a naming collision, or if the resolution results in an inconvenient name, use the HydraExpress properties file mappings element to specify the class name mapping, as described in Section 8.7.1.

8.2.1 Static Data Members

The classes generated for complex types have these static data members:

8.2.1.1 The DefaultElementName Data Member

Each class generated by HydraExpress has a data member called DefaultElementName. This data member is an instance of rwsf::XmlName.

The DefaultElementName for a class is determined as follows:

The marshal() and unmarshal() methods for a class use the DefaultElementName by default. This allows you to simply call marshal() or unmarshal() on a class, usually with good results. For example, if the schema defines a top-level element named purchaseOrder, the following code usually works as expected:

This code should produce, as expected, an XML document with a top-level element <purchaseOrder>.

There is one case, however, where the default use of the DefaultElementName does not work as you might expect:

In this case, if the following code were used:

the result would be an XML document containing a top-level element called <bar>, when it would make better sense for the name of the element to be <foo>. To obtain this better result, the second line above would need to be:

8.2.1.2 Contained Element Name and Attribute Data Members

For each of the elements contained in a complex type, the class for the complex type has data members for the element names, and for any attributes of the complex type. The following code is taken from the XML Binding basic example:

The class PurchaseOrderType contains the following static data members:

The names of the data members for the element names may not exactly match the names of the classes that represent them. They do not have parent names prepended, and may be different when substitution groups are used (see Section 8.5.9).

8.2.2 Namespaces

There are two aspects to namespace support:

For the former, HydraExpress passes rwsf::XmlName instances to the reader and writer for all fully qualified names. The header file for a complex type contains static constant rwsf::XmlName instances for the name of the complexType as well as any child elements and attributes that it defines. If the targetNamspace attribute on the XML Schema is present, this is the URI used for any namespace qualified structures:

Otherwise, elements and attributes are not to be namespace qualified, and only the local name is used.

8.2.2.1 Accessing the Fully Qualified XML Name of a Complex Type

HydraExpress allows you to access the fully-qualified XML name of complex types. If a target namespace is present in the schema, the value is programmatically accessible using the DefaultElementName constant, as shown here:

8.2.2.2 Mapping XML Namespaces to C++ Namespaces

If the schema contains a namespace declaration that associates a prefix to the target namespace URI, by default this prefix is used as the C++ namespace for the generated classes. In the example schema element declaration below, the po prefix is associated with the URI that matches the value of the targetNamespace attribute, so the classes would be generated within the po namespace.

To use the generated classes you must prefix the type with the po namespace or make the name accessible with a using directive or using declaration. The following code snippets show both options.

Prefixing the type with a the po namespace:

Importing all names in po with a using directive:

Importing po::PurchaseOrderType with a using declaration:

Instead of using the XML namespace prefix as the C++ namespace, you can choose your own C++ namespace through the use of the mappings element in a HydraExpress project file. The mapping element shown below instructs the code generator to generate the classes that are defined within the target namespace http://www.example.com/PO1, using the C++ namespace named POTypes.

8.2.2.3 Differentiating Same-Named Items

It is entirely possible in an XML Schema to have two or more elements, types, or groups with the same name, so long as they are defined in separate namespaces. In this case, HydraExpress creates a separate class for each namespace-qualified item, placing the code for each class in a single pair of header and source files, named as usual with the unqualified name of the element, type or group.

8.2.2.4 Adding a Namespace to a Root Element

You can add a namespace to the root element without adding it to any children elements. To do so, use the addNamespaceDecl() function on rwsf::XmlStringWriter.

For example, the code below adds a namespace to the root element of myDocument:

//1

Necessary includes.

//2

Creates a writer to marshal your object.

//3

Set the namespace with "qb" as the prefix and http://www.roguewave.com/Schema/Project as the URI.

//4

Invokes addNamespaceDecl() with the provided namespace.

//5

Marshals the object.

//6

Prints to standard out the marshaled XML document.

Once marshaled, the XML document contains the root namespace:

8.2.3 File Naming

As described in the remainder of this chapter, HydraExpress creates classes corresponding to top-level elements and complex types defined in the XML Schema. Generally, HydraExpress creates a header and a source file for each class, with the file name based on the unqualified name of the element or type. The exception is when multiple items have the same name but in different namespaces, as described above.

It is also possible for there to be name conflicts between elements, types, and groups in the same namespace. These are handled as follows:

So, for example, if a schema defined an element named Address and a type named Address, HydraExpress would generate a class Address for the element and a class AddressType for the type.



Previous fileTop of DocumentContentsNo linkNext file

Copyright © Rogue Wave Software, Inc. All Rights Reserved.

The Rogue Wave name and logo are registered trademarks of Rogue Wave Software, and HydraExpress is a trademark of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.