Please enable JavaScript to view this site.

Altova MapForce 2020 Basic Edition

By default, when a mapping produces XML output, the namespace (or set of namespaces) of each element and attribute is automatically derived by MapForce from the schema associated with the target component. This is the default behavior in MapForce and is suitable for most mapping scenarios that involve generation of XML output.

 

However, there might be cases when you want to have more control over namespaces of elements in the resulting XML output. For example, you may want to manually declare the namespace of an element directly from the mapping.

 

To understand how this works, open the BooksToLibrary.mfd mapping available in the <Documents>\Altova\MapForce2020\MapForceExamples\Tutorial\. Right-click the library node, and select Add Namespace from the context menu.

mf_custom_namespace_01

Notice that two new nodes are now available under the library node: a namespace and a prefix.

mf_custom_namespace_02

You can now map to them string values from the mapping. In the image below, two constants were defined (from Insert | Constant menu command) that provide the namespace "altova.library" and the prefix "lib":

mf_custom_namespace_03

The result is that, in the generated output, an xmlns:<prefix>="<namespace>" attribute is added to the element, where <prefix> and <namespace> are values that come from the mapping (in this case, from constants). The generated output will now look as follows (notice the highlighted part):

 

<?xml version="1.0" encoding="UTF-8"?>
<library xmlns:lib="altova.library" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="library.xsd">

...

 

Note:Declaring custom namespaces (and the Add Namespace command) is meaningful only for target XML components, and applies to elements only. The Add Namespace command is not available for attributes and wildcard nodes. It is also not available for nodes which receive data by means of a Copy-All connection.

 

You can also declare multiple namespaces for the same element, if necessary. To do this, right-click the node again, and select Add Namespace from the context menu. A new pair of namespace and prefix nodes become available, to which you can connect the new prefix and namespace values.

 

To remove a previously added namespace declaration, right-click the ns:namespace node, and select Remove Namespace from the context menu.

 

Both the namespace and prefix input connectors must be mapped, even if you provide empty values to them.

 

If you want to declare a default namespace (that is, one in the format xmlns="mydefaultnamespace"), map an empty string value to prefix. To see this case in action, edit the example mapping above so as to make the second constant an empty string.

mf_custom_namespace_04

The resulting output would then looks as follows:

 

<?xml version="1.0" encoding="UTF-8"?>
<library xmlns="altova.library" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="library.xsd">

...

 

If you need to create prefixes for attribute names, for example <number prod:id="prod557">557</number>, you can achieve this by either enabling dynamic access to node's attributes (see Mapping Node Names), or by editing the schema so that it has a prod:id attribute for <number>.

© 2019 Altova GmbH