Please enable JavaScript to view this site.

Altova MapForce 2020 Enterprise Edition

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).

 

void Example()
{
  // Create the XML document and append the root element
  Main::pur::CMain doc = Main::pur::CMain::CreateDocument();
  Main::pur::CPurchaseType purchase = doc.Purchase.appendWithPrefix(_T("p"));
 
  // Set schema location
  doc.SetSchemaLocation(_T("Main.xsd"));
 
  // Declare namespaces on root element
  purchase.DeclareNamespace(_T("o"), _T("http://NamespaceTest.com/OrderTypes"));
  purchase.DeclareNamespace(_T("c"), _T("http://NamespaceTest.com/CustomerTypes"));
  purchase.DeclareNamespace(_T("cmn"), _T("http://NamespaceTest.com/CommonTypes"));
 
  // Append the OrderDetail element
  Main::ord::COrderType order = purchase.OrderDetail.append();
  Main::ord::CItemType item = order.Item.append();
  item.ProductName.append() = _T("Lawnmower");
  item.Quantity.append() = 1;
  item.UnitPrice.append() = 148.42;
 
  // Append the PaymentMethod element
  Main::cmn::CPaymentMethodTypeType paymentMethod = purchase.PaymentMethod.append();
  paymentMethod.SetEnumerationValue(Main::cmn::CPaymentMethodTypeType::k_VISA);
 
  // Append the CustomerDetails element
  Main::cust::CCustomerType customer = purchase.CustomerDetails.append();
  customer.Name.append() = _T("Alice Smith");
  Main::cmn::CAddressType deliveryAddress = customer.DeliveryAddress.append();
  deliveryAddress.Line1.append() = _T("123 Maple Street");
  deliveryAddress.Line2.append() = _T("Mill Valley");
  Main::cmn::CAddressType billingAddress = customer.BillingAddress.append();
  billingAddress.Line1.append() = _T("8 Oak Avenue");
  billingAddress.Line2.append() = _T("Old Town");
 
  // Save to file and release object from memory
  doc.SaveToFile(_T("Main1.xml"), true);
  doc.DestroyDocument();  
}

© 2020 Altova GmbH