By default, during serialization namespace declarations are written to the resulting XML document only if the relevant namespace prefix is used as part of the name in a literal XML element or attribute or in a non-literal attribute. A namespace declaration is written exactly into the element in which it is needed. Namespace declarations whose prefix is not used explicitly, are not considered during serialization.
During deserialization, the binding of the namespace prefix to the correct URI is checked.
For XML elements of the resulting XML document, in which a namespace declaration is desired even though the relevant namespace prefix is not part of a name but used, for example, as content of an attribute, you can enforce this during serialization using the command tt:namespace. The syntax is as follows:
<tt:namespace name="prefix"/>
You can specify this statement one or more times within a literal XML element before its subelements. Each statement declares a namespace for the current XML element with the namespace prefix prefix specified after the attribute name. The namespace prefix must be known in the current context, that is, it must have been defined in a surrounding XML element with xmlns:prefix="...".
If a surrounding element does not yet contain a declaration of the namespace, the statement tt:namespace inserts the definition xmlns:prefix="..." into the definition of the literal XML element and thus applies it to all its subelements. Within the XML element, no other declarations of this namespace are generated. Especially, other tt:namespace statements within the same XML element have no effect.
During deserialization, the command has no effect.
The ST program below shows how namespaces are treated by default:
<tt:transform
xmlns:tt="http://www.sap.com/transformation-templates"
xmlns:abc="www.abc.com" xmlns:xyz="www.xyz.com">
<tt:root name="ROOT"/>
<tt:template>
<context1
xmlns:abc="www.abc.com" attr="abc:uvw">
<X>...</X>
</context1>
<context2>
<abc:X
xyz:attr="xyz">...</abc:X>
<xyz:Y>...</xyz:Y>
</context2>
</tt:template>
</tt:transform>
The definition of the namespace with the prefix abc in element context1 is not needed in standard XML and is omitted during serialization. In the subelements abc:Xand xyz:Y of context2, the declaration of namespaces is needed and inserted during serialization. The result of a serialization is the following:
<context1
attr="abc:uvw">
<X>
...
</X>
</context1>
<context2>
<abc:X xmlns:abc="www.abc.com"
xmlns:xyz="www.xyz.com" xyz:attr="xyz">
...
</abc:X>
<xyz:Y
xmlns:xyz="www.xyz.com">
...
</xyz:Y>
</context2>
The ST program below contains explicit namespace declarations:
<tt:transform
xmlns:tt="http://www.sap.com/transformation-templates"
xmlns:abc="www.abc.com"
xmlns:xyz="www.xyz.com">
<tt:root name="ROOT"/>
<tt:template>
<context1
attr="abc:uvw">
<tt:namespace name="abc" />
<X>...</X>
</context1>
<context2>
<tt:namespace name="abc" />
<tt:namespace name="xyz" />
<abc:X
xyz:attr="xyz">...</abc:X>
<xyz:Y>...</xyz:Y>
</context2>
</tt:template>
</tt:transform>
Use the tt:namespace commands to explicitly insert namespace declarations for the prefixes abc and xyzinto the elements context1 and context2. Now the content of attribute attr of context1 can be evaluated specifically to the namespace and in the elements abc:X and xyz:Y of context2 the declaration of namespaces is no longer needed.
<context1
xmlns:abc="www.abc.com" attr="abc:uvw">
<X>
...
</X>
</context1>
<context2 xmlns:abc="www.abc.com" xmlns:xyz="www.xyz.com">
<abc:X xyz:attr="xyz">
...
</abc:X>
<xyz:Y>
...
</xyz:Y>
</context2>