The Generated XSLT Script

The generated XSLT script is responsible for constructing an XML target tree from a number of XML sources by filtering, combining, renaming, reordering and adding arbitrary structure. The script is a direct expression of the semantics captured in the mapping of attributes and elements with text in an DTD target document from attributes and elements with text in a set of DTD sources.

The structure of the generated script can be broken down into 5 sections:

  1. Declarations
  2. XML Sources
  3. Root Element Template
  4.  Identity Transformation Template
  5. Mappings

Declarations

The declaration section:
 
 

XML Sources

Multiple XML sources are specified by the user at runtime through stylesheet parameters. The default values are constructed using the names of the corresponding DTD files. There is also a global variable attached to each source identifying the source's root element for access throughout the stylesheet.

 
 

Root Element Template

The "Root Element Template" section specifies which template will be invoked first thus determining the root element of the result tree.  If the root element is not associated with the input XML sources either explicitly or implicitly, then the template is invoked  by name.  Otherwise, the template is invoked by applying matching  template rules in XSLT.

(i) Example of  a source element as root
 (ii) Example of a root element not associated with any sources

The Identity Transformation Template

The last template  rule is a generic identity transformation used for moving a complete tree fragment from an input source to the result tree. The tree fragment may include nodes such as elements, attributes, comments, processing-instructions and text. The identity transformation template gets instantiated whenever there is no specific applicable template rule for a given node.

Mappings

The XML Mapping Editor tool allows the user to map attributes and elements (with text) in the DTD sources to attributes and elements (with text) in the DTD target. The user can further enhance any mapping by specifying an XPATH String function that will be applied to the source text to create the corresponding target text. This section describes how these mappings are converted to XSLT code.

Mapping an Attribute to an Attribute

Mapping one or more attributes in the source to an attribute in the target may, in fact, be seen as a number of different kinds of transformations. Mapping an attribute to an attribute is achieved in the XSLT script by creating a new attribute in the target tree using the xsl:attribute element. The value of the attribute is set to the value(s) of the corresponding source attribute(s). In the example below, the attribute "name" is first mapped to "firstName" and then to "lastName" and the value is filtered appropriately.

Example of  mapping as a means of filtering text.

Mapping an Element to an Element

Mapping one or more elements with text in the source to an element with text in the target is analogous to the atribute to attribute mapping described above.

Mapping an element to an element is achieved in the XSLT script by creating a new element in the target tree with the name of the new target element and the text value of the corresponding source element. Note there is no templates created in the XSLT script for elements with text only. Instead, they are defined in-place which adds a further degree of flexibility when mapping. For example, a target element can be mapped to one kind of source element in one part of the target tree and to another kind of source element (or even an attribute) in another part of the same tree.

In the example below, the source elements "FirstName" and "LastName" are mapped to the target element "DeliverTo". The mapping is, in essence, a concatenation of the text values of the source elements "FirstName" and "LastName" into the single text value of the corresponding target element "DeliverTo".

Example of  mapping as a means of concatenating text.

Mapping an Element to an Attribute

Mapping an element to an attribute involves taking information structured as text within an element node and converting it to an attribute value. Converting an element to an attribute is achieved in the XSLT script by creating a new attribute in the target tree using the xsl:attribute element. The value of the attribute is set to the text value of the corresponding source element.

In the example below, the child node "artist" is converted to an attribute in the "CD" element.

Example of  mapping as a means of converting an element to an attribute.

Mapping an Attribute to an Element

Mapping an attribute to an element involves taking information structured as an attribute value and converting it to text value of an element node. Converting an attribute to an element is achieved in the XSLT script by creating a new element in the target tree with the same name and value as the corresponding attribute node. Note there is no templates created in the XSLT script for elements with text only. Instead, they are defined in-place which adds a further degree of flexibility when mapping. For example, a target element can be mapped to one kind of source element in one part of the target tree and to another kind of source element (or even an attribute) in another part of the same tree.

In the example below, the attribute "artist" is converted to an child node of  the "CD" element.

Example of  mapping as a means of converting an attribute to an element.

Implicit Mappings Based on Similar Content

In the current version of the XML Mapper tool, the user is only concerned with mapping those nodes containing character data (i.e., attributes and elements with mixed content). As part of the XSLT script generation,an attempt is made to automatically map structural target elements (elements containing only elements) to corresponding source elements based on their common content. If no comonality can be found, then the target element is treated as a new element with no relation to the source.