# Automatic transformation of XML namespaces/Transformations/Order kinds of transformers

For down-up transformer (see below) we first select primary nodes from the set of document nodes.
An element is having a certain namespace, if it is either an element of this namespace or has an attribute of this namespace.
Rationale: This twist about namespaced attributes is useful for such usages as XLink. Example:
may transform into
In this example the link is “raised”, above the element it is applied to, to a “wrapper” element (<a> in this example).
Next stage, feed every primary node, having a namespace which is a source namespace of the transformer, to the transformer. This constructs a new document.
After this the stage is repeated.
Note: This facilitates for example embedding a complete XHTML document into our XML. The document transformer will receive only the root of the XHTML not its fragments which it may not understand.

##### The entire document transformer

The entire document transformer is just applying once the transformer to the entire document.

##### Simple Sequential transformer

While there are elements or attributes of the source namespace, repeatedly run the transformer.
Rationale: It can be used for example to implement XInclude (however, it is just an example, as XInclude can be implemented in another ways).
TODO: Is it at all needed? A script can do repetition by itself, if it needs it.

##### Subdocument sequential transformer

The transformer is applied to the first element having the source NS. After this the step is repeated until there are no elements (nor attributes) of the source namespace.
Note for implementers: You can start searching for the next element from the point fed to the last transformer.

##### Up-down transformer

It is essentially the same as subdocument sequential transformer, so remove.
Find all elements of the source NS which are not descendants of an element of the source NS.
Run the transformer for every subtree designated by these elements.
Repeat these steps until there are no elements of the source namespace.

##### Down-up transformer

Find first primary element which has no primary descendants with associated namespace in the set of script source namespaces. Transform its subtree.
Repeat while there exists such an element.
Rationale: Suppose we have a DocBook document with embedded SVG and SVG again has embedded DocBook. Suppose we want to transform to XHTML. Then we need to repeat transformations for newly produced subdocuments.

##### Plain text transformer

The relevant document element is required (otherwise produce an error) to contain only text (or CDATA) nodes. The content of the element is passed verbatim (without XML formatting) to the script.
If the element is of the relevant namespace, this element is removed and the output of the script is added to its parent. If it isn't then just all attributes of the relevant namespace are removed, the output of the scripts becomes the element content.

##### Arbitrary fragment transformer

The transformer is applied to an arbitrary XML text fragment (with closed tags, processing instructions, and comments, and certainly no half of entities) including at least one element having the source NS. The fragment is replaced by the output of the transformer. It is allowed to be applied to fragments not having ancestor elements of the source NS. TODO: Specify how to specify and use this transformer.
Rationale: This kind of transformers is intended to be used for incremental transformation of changed XML files.

##### Arbitrary tag transformer

The transformer is applied to an arbitrary tag of a source NS. TODO: Specify how to specify and use this transformer. TODO: Does it make sense to apply it to a more wide kind of XML fragmens?