Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Data Interfaces and Communication Interfaces →  ABAP and XML →  Simple Transformations →  ST - Serialization and Deserialization 

ST - tt:namespace, Namespaces

Other versions: 7.31 | 7.40 | 7.54

Syntax


<tt:namespace name="prefix"/>

Effect

The ST command tt.namespace declares an explicit namespace.

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 into the exact element in which it is needed. Namespace declarations whose prefix is not used explicitly are ignored by 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 by using the command tt:namespace. You can specify the 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".

Serializing Namespace Declarations

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. In particular, other tt:namespace statements within the same XML element have no effect.

Deserializing Namespace Declarations

During deserialization, the command tt:namespace has no effect.


Example

The ST program below shows how namespaces are handled 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>
    <X0>
      <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>
    </X0>
  </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:X and xyz:Y of context2, the declaration of namespaces is needed and inserted during serialization. The result of a serialization is as follows:

<X0>
  <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>
</X0>

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>
    <X0>
      <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>
    </X0>
  </tt:template>
</tt:transform>

You use the tt:namespace commands to explicitly insert namespace declarations for the prefixes abc and xyz into the elements context1 and context2. Now the content of attribute attr of context1 can be evaluated with specific reference to the namespace and the declaration of namespaces is no longer needed in the elements abc:X and xyz:Y of context2.