Data templates and transformations > Data transformation process reference > Special forms (union) > Data driven example

Data driven example
Note:
This example illustrates the transformation process used in general XML to XML transformation (transformation is not limited to StoryTeller).
In this example the data source contains a header element followed by three different types of elements in arbitrary order, and you want to preserve that order for presentation.
One solution is to create a common element for any data driven input element. This common element represents a generic event, and each instance of this element contains a single one of the data driven elements as sub element. This element can also contain all common attributes.
Data source
<data>
<message>
<header owner="John Smelter" number="+4202468999"/>
<call number="+4207654321" cost="3.23" duration="125"/>
<sms number="+4201234567" cost="0.15" text="Hello!"/>
<sms number="+4207654321" cost="0.12" text="Hi!"/>
<mms number="+4207654321" cost="0.45" bytes="12432"/>
<mms number="+4201234567" cost="0.75" bytes="32457"/>
<sms number="+4201234567" cost="0.15" text="Cheers!"/>
<call number="+4207654321" cost="1.67" duration="67"/>
<sms number="+4201234567" cost="0.15" text="Good bye!"/>
<mms number="+4207654321" cost="0.45" bytes="12345"/>
</message>
</data>
This is the data source to process. The call, sms and mms elements come in an arbitrary order, and you want to preserve the order for presentation.
Expected result
<data>
<message>
<header owner="John Smelter" number="+4202468999"/>
<event number="+4207654321" cost="3.23">
<call duration="125"/>
</event>
<event number="+4201234567" cost="0.15">
<sms text="Hello!"/>
</event>
<event number="+4207654321" cost="0.12">
<sms text="Hi!"/>
</event>
<event number="+4207654321" cost="0.45">
<mms bytes="12432"/>
</event>
...
<event number="+4207654321" cost="0.45">
<mms bytes="12345"/>
</event>
<footer events="9" total="7.120000000000001"/>
</message>
</data>
 
Data template
<data>
<message>
<header owner="?" number="?"/>
<event number="?" cost="?">
<call duration="?"/>
<sms text="?"/>
<mms bytes="?"/>
</event>
<footer events="?" total="?"/>
</message>
</data>
The generic element event is added to the template, and this element contains the common attributes @number and @cost. A new footer element is also added. This element contains summary data: number of events (@events) and sum of all costs (@total).
Data transformation
<tdt:transformation>
<tdt:rule path="/data/message">
<tdt:value key=".">/data/message</tdt:value>
<tdt:value key="$events">*[self::call|self::sms|self::mms]</tdt:value>
</tdt:rule>
<tdt:rule path="/data/message/header">
<tdt:value key=".">header</tdt:value>
<tdt:value key="recurse">.</tdt:value>
</tdt:rule>
<tdt:rule path="/data/message/event">
<tdt:value key=".">$events</tdt:value>
<tdt:value key="recurse">.</tdt:value>
</tdt:rule>
<tdt:rule path="/data/message/event/call">
<tdt:value key=".">self::call</tdt:value>
<tdt:value key="recurse">.</tdt:value>
</tdt:rule>
<tdt:rule path="/data/message/event/sms">
<tdt:value key=".">self::sms</tdt:value>
<tdt:value key="recurse">.</tdt:value>
</tdt:rule>
<tdt:rule path="/data/message/event/mms">
<tdt:value key=".">self::mms</tdt:value>
<tdt:value key="recurse">.</tdt:value>
</tdt:rule>
<tdt:rule path="/data/message/footer">
<tdt:value key="@total">sum($events/@cost)</tdt:value>
<tdt:value key="@events">count($events)</tdt:value>
</tdt:rule>
</tdt:transformation>
The data transformation should do as follows:
1
Evaluate <header> (design driven mode).
2
Evaluate <event> to emulate a data driven mode (retrieve all call, sms and mms elements)
a
Fill the common attributes @number and @cost.
b
Pick a single corresponding sub-element (call, sms or mms) with its attributes.
3
Evaluate <footer> (design driven mode).
 
OpenText StreamServe 5.6.2 Updated: 2018-01-26