Online documentation - Websydian v6.0

Users Guide | Patterns Reference | WebsydianExpress | Search

 

TransacXML and Namespaces



Introduction

XML namespaces is a mechanism for avoiding name clashes in XML documents that uses elements and attributes defined by different sources. A namespace is thus a collection of elements and attributes and the namespace is identified by an URI reference, and each element or attribute in the namespace is identified by the namespace and the name of the element/attribute. The latter is also called the local name. The combination of the namespace and the local name is called the expanded name.

In XML documents the expanded name is not used to reference elements or attributes. Instead a prefix is used in combination with the local name and the prefix is then mapped to a namespace. Thus the prefix is nothing more than an alias for the namespace.

If the element Order belongs to the namespace http://www.websydian.com/namespaces/order then this will be reflected in the XML document like this:

<p:Order xmlns:p="http://www.websydian.com/namepsaces/order">...</p:Order>

The prefix name can be any name and is not used to identify the element. It is solely the local name (Order) and the namespace name (http://www.websydian.com/namespaces/order) that identifies the element.

For more information on namespaces please refer to the following sites:

Namespaceaware Objects in TransacXML

TransacXML fully supports XML namespaces in an easy and very intuitive way as the developer only has to take the namespace into account when the XML model is defined. For each element defined in the XML model both the local name of the element and the namespace it belongs to is specified (if it belongs to a namespace).

When building applications that process or creates XML documents the developer does not need to worry about namespaces as these are defined in the model and TransacXML will then generate the correct code to access or create the XML elements and attributes in the correct namespaces.

Entity: NamespaceAware

This entity is used when defining a complex element that belongs to a namespace. When inheriting from the NamespaceAware entity two source code objects will be scoped to the inheriting entity:

Field: NamespaceAware

This field is used when defining a simple element or an attribute that belongs to a namespace. When inheriting from the NamespaceAware field two source code objects will be scoped to the inheriting field:

Inheritance of Namespaces

In TransacXML the namespace of an element is inherited by its scoped elements. This applies both to complex elements and simple elements.

However, please note that attributes do not inherit namespaces from the element they belong to. This is in correspondence with the XML namespace specification where attributes to an element by default does not belong to the namespace of the element (unless explicitly specified) and that default namespaces do not apply to attributes.

This means that if the top element in the XML model belongs to namespace N, then all descendent elements (complex elements and simple elements) will also by default belong to the N namespace. However, all the attributes defined in the XML model will not belong to any namespace.

This behavior makes it very easy to model XML elements belonging to a namespace. Please see the next section for an example.

To override an inherited namespace definition simply inherit from the NamespaceAware object and specify a namespace in the scoped source code Namespace. A blank value means that the object (and all descendent objects) do not belong to any namespace.

Example

The figure below shows an XML model of an Order XML document as displayed by the object browser in CA Plex.

When TransacXML generates an XML document based on the above model the output will be as follows:

<Order OrderID="432423">

    <OrderDescription>Order to Acme Industries INC.</OrderDescription>

    <OrderHeader OrderHeaderID="454323">

        ....

    </OrderHeader>

</Order>

Now, if we would like to define the above document in a namespace, then the following steps must be performed:

  1. Define the top element as namespace aware:
    Order is a ENT NamespaceAware
  2. Enter the namespace name in the source code object Order.Namespace; e.g: http://www.websydian.com/namespaces/order.
  3. Enter the prefix to be used in the source code object Order.Prefix; e.g: p. The prefix is used when TransacXML generates XML documents.

After the above modifications the XML document will now be generated with namespaces:

<p:Order OrderID="432423" xmlns:p="http://www.websydian.com/namespaces/order">

    <p:OrderDescription>Order to Acme Industries INC.</p:OrderDescription>

    <p:OrderHeader OrderHeaderID="454323">

        ....

    </p:OrderHeader>

</p:Order>

Note that complex elements and simple elements (e.g. OrderHeader and OrderDescription) inherits the namespace, whereas attributes does not (e.g. OrderID and OrderHeaderID). This behavior is in accordance with the standard uses of namespaces where attributes typically is not defined to be part of the namespace of the element that the attribute belongs to.