XML Namespaces and Prefixes (C++)
After you generate code from the example schema, a test C++ application is created, along with several supporting Altova libraries. Recall that the example schema (Main.xsd) has multiple namespace declarations. Consequently, the generated code includes namespaces that correspond to namespace aliases (prefixes) from the schema, namely: Main::ord, Main::pur, Main::cmn, and Main::cust.
In general, in order to control XML namespaces and prefixes with the help of the schema wrapper libraries, you have the following methods at your disposal:
•DeclareAllNamespacesFromSchema(). Call this method if you want to declare the same namespaces in your XML instance as in the schema. Otherwise, if you need different namespaces as in this example, then DeclareNamespace() should be used. The method DeclareAllNamespacesFromSchema() is not used in this example because we specifically want to create XML elements with prefixes that are slightly different from those declared in the schema.
•DeclareNamespace(). Call this method to create or override the existing namespace prefix attribute on an element. The element must already be created using either the append() or appendWithPrefix() methods, as further illustrated below.
•appendWithPrefix(). Use this method to append an instance element with a specific prefix. To create the XML instance illustrated in this example, it was sufficient to call this method for the root element only. All other elements were appended using just append(), and their prefixes were added automatically based on their namespaces, according to the rules above.
The code listing below shows you how to create an XML document with multiple namespace declarations and prefixed element names. Specifically, it generates a Purchase Order instance as illustrated in the Example: Purchase Order. Importantly, for illustrative purposes, some prefixes are overridden in the XML instance (that is, they are not exactly the same as the ones declared in the schema).