Listing 1 Examples of three types of code units

An example of call-by-name templates:

The definition of a call-by-name template:
<xsl:template name="template-name">
<xsl:param name="parameter1"/>
<!-- the implementation of the template is here -->
</xsl:template>

The invocation of the call-by-name template:

<xsl:call-template name="template-name">
<xsl:with-param name="parameter1" select="'value'"/>
</xsl:call-template>

An example of call-by-context templates:
The definition of a call-by-context template:

<xsl:template match="xml-node-name">
<xsl:para name="parameter1"/>
<!-- the implementation of the template is here -->
</xsl:template>

The invocation of the call-by-context templates:

<xsl:apply-templates select="context-node/xml-node-name">
<xsl:with-param name="parameter1" select="'value'"/>
</xsl:apply-templates>

An example of global variables:
The definition of a global variable:

<xsl:variable name="global-variable">
<!-- the implementation of the global variable is here -->
</xsl:variable>

The invocation of the global variable:

$global-variable




Listing 2 The C++ code to output
bool AClass::operator== ( const AClass & rhs ) const
{
if (intField1_!=rhs.intField1_) return false;
if (intField2_!=rhs.intField2_) return false;
if (strcmp(stringField_,rhs.stringField_) != 0) return false;
return true;
}



Listing 3 The code unit outputting conditional expressions
<xsl:template name="not-equal">
<xsl:param name="left"/>
<xsl:param name="right"/>
<xsl:param name="type"/>
<xsl:choose>
<xsl:when test=" $type = 'int'">
<xsl:value-of select="concat($left,'!=',$right)"/>
</xsl:when>
<xsl:when test=" $type = 'char*'">
<xsl:value-of select="concat('strcmp(',$left,$right,')!=0)')"/>
</xsl:when>
</xsl:choose>
</xsl:template>



Listing 4 The template to recompose and an example calling context

An example calling context:

<class class-name="Aclass">
<instance-variable variable-name="intField1_" variable-type="int"/>
<instance-variable variable-name="intField2_" variable-type="int"/>
<instance-variable variable-name="stringField_" variable-type="char*"/>
</class>

The assembling template:

<xsl:template mode="equality-operator" match="class">
<xsl:value-of select="concat('bool ',@class-name,'::operator == (',@class-name,' &amp; rhs ) const',$cr,'{',$cr)"/>
<xsl:for-each select="instance-variable">
<xsl:variable name ="conditionalExpression">
<xsl:call-template name="not-equal">
<xsl:with-param name="left" select="@variable-name"/>
<xsl:with-param name="right" select="concat('rhs.',@variable-name)"/>
<xsl:with-param name="type" select="@variable-type"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="concat('if (',$conditionalExpression,') return false;',$cr)"/>
</xsl:for-each>
<xsl:value-of select="concat('return true;',$cr,'}',$cr)"/>
</xsl:template>




Listing 5 The content of intermediate-wrapper.xsl

<!-- Group: The following templates are called from c-source-file.xsl
->

<!-- output the constructor of a class ->
<template name="intermediate-wrapper.constructor">
<xsl:param name="className"/>
......
</template>
......

<!-- Group: The following templates are called from c-header-file.xsl
->
<!-- output the include section of a class ->
<template name="intermediate-wrapper.inlcudes">
<xsl:param name="className"/>
......
</template>
......



Listing 6 The input XML document
<logical-model>
<data-type>
<name>Alphanumeric</name>
<description> Alphanumeric represents the character space [48..57, 65..90, 97..122] of the ASCII character set</description>
</data-type>
<data-type>
<name>LastName</name>
<description> the last name of a person can have up to 10 Alphanumeric characters</description>
<base-type>Alphanumeric</base-type>
<max-length>10</max-length>
</data-type>
</logical-model>

<intermediate-tree>
<data-type>
<name>Alphanumeric</name>
<semantics>
<unbounded-string/>
</semantics>
</data-type>
<data-type>
<name>LastName</name>
<semantics>
<bounded-string max-length="10"/>
</semantics>
</data-type>
</intermediate-tree>


Listing 7 Changed logical model
<logical-model>
<data-type>
<name>Alphanumeric</name>
<description> Alphanumeric represents the character space [48..57, 65..90, 97..122] of the ASCII character set</description>
</data-type>
<data-type>
<name>LastName</name>
<description> the last name of a person can have up to 10 Alphanumeric characters</description>
<base-type>Alphanumeric</base-type>
<facet>
<value>10</value>
<facettype>
<name>Max Length</name>
</facettype>
</facet> 
</data-type>
</logical-model>


Listing 8 The input document
<class name="Object">
<fields>
<optional flagName="optFlag">
<fields>
<field name="opt_F"/>
</fields>
</optional>
<field name="nonOpt_F"/>
</fields>
</class>


Listing 9 The two output documents
C++ Marshaling code segment:

write(object.optFlag); 
if ( object.optFlag ) {
write(object.opt1_F);
delete object.opt1_F;
}
write(object.nonOpt_F);
delete object.nonOpt_F;

C++ De-marshaling code segment:

read(object.optFlat) {
if ( object.optFlag ) {
read(object.opt1_F);
}
read(object.nonOpt_F);



Listing 10 The sample implementation of builder patterns
The director templates:

<xsl:template mode="director" match="class">
<xsl:param name="callBack"/>
<xsl:apply-templates mode="director" match="fields/*">
<xsl:with-param name="callback" select="$callBack"/>
</xsl:apply-templates>
</xsl:template>

<xsl:template mode="director" match="optional">
<xsl:param name="callBack"/>
<xsl:apply-templates mode="template-pointer" select="$callBack">
<xsl:with-param name="flagName" select="@flagName"/>
<xsl:with-param name="style" select="'optional'"/>
</xsl:apply-templates>
<xsl:value-of select="'{'"/>
<xsl:apply-template mode="director" match="fields/*">
<xsl:with-param name="callback" select="$callBack"/>
</xsl:apply-templates>
<xsl:value-of select="'}'"/>
</xsl:template>

<xsl:template mode="director" match="field">
<xsl:param name="callBack"/>
<xsl:apply-templates mode="template-pointer" select="$callBack">
<xsl:with-param name="fieldName" select="@name"/>
<xsl:with-param name="style" select="'field'"/>
</xsl:apply-templates>
</xsl:template>

The callback builder templates:
The builder to output marshalling code:

<xsl:template mode="template-pointer" match="tp:marshalling" xmlns:tp="template-pointer-simulator-prefix">
<xsl:param name="fieldName"/>
<xsl:param name="flagName"/>
<xsl:param name="style"/>
<xsl:choose>
<xsl:when test="$style = 'optional'">
<xsl:value-of select="concat('write(object.',$flagName,');',$cr)"/>
<xsl:value-of select="concat('if (object.',$flagName,')',$cr)"/>
</xsl:when>
<xsl:when test="$style = 'field'">
<xsl:value-of select="concat('write(object.',$fieldName,');',$cr)"/>
<xsl:value-of select="concat('delete object.',$fieldname,';',$cr)"/>
</xsl:when>
</xsl:choose>
</xsl:template> 

The builder to output de-marshalling code:

<xsl:template mode="template-pointer" match="tp:de-marshalling" xmlns:tp="template-pointer-simulator-prefix">
<xsl:param name="fieldName"/>
<xsl:param name="flagName"/>
<xsl:param name="style"/>
<xsl:choose>
<xsl:when test="$style = 'optional'">
<xsl:value-of select="concat('read(object.',$flagName,');',$cr)"/>
<xsl:value-of select="concat('if (object.',$flagName,')',$cr)"/>
</xsl:when>
<xsl:when test="$style = 'field'">
<xsl:value-of select="concat('read(object.',$fieldName,');',$cr)"/>
</xsl:when>
</xsl:choose>
</xsl:template> 

The XML file for simulating template pointers:

<model xmlns:tp="template-pointer-simulator-prefix">
<pointers>
<tp:marshalling/>
<tp:de-marshalling/>
</pointers>
</model>

The invocation of the director template in order to output marshalling code:

<xsl:apply-templates mode="director" select="class" xmlns:tp="template-pointer-simulator-prefix">
<xsl:with-param name="callback" select="pointers/tp:marshalling"/>
</xsl:apply-templates>