<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="text" encoding="UTF-8" indent="no" omit-xml-declaration="yes"/>
	<xsl:variable name="field_name_list"	     select="document('field_list.xml')//field_list/field"/>
	<xsl:variable name="action_description_list" select="document('action_descriptions.xml')//action_description/action"/>
    <xsl:variable name="event_type_list"   select="document('event_tracking.xml')//event_tracking/event_type"/>
    <xsl:variable name="source_type_list"  select="document('event_tracking.xml')//event_tracking/source_type"/>
	<xsl:template match="/">
		<!-- header fields -->
			<xsl:for-each select="//record[1]/@*">
            <xsl:variable name="current_field_tag" select="name(.)"></xsl:variable>
			<xsl:variable name="current_field_name"	select="$field_name_list[@name=$current_field_tag]"></xsl:variable>
        	<xsl:choose>
                <!-- Delete unwanted column for PP 1.8 by Joy Zhou -->  
                <xsl:when test="$current_field_tag='storage_resend_count'"></xsl:when>
                <xsl:when test="$current_field_tag='msg_delivery_time'"></xsl:when>
          		<xsl:when test="$current_field_tag='msg_destination'"></xsl:when>
          		<xsl:when test="$current_field_tag='msg_subject'"></xsl:when> 
                <xsl:when test="$current_field_tag='activity_time'">
                <!-- Show Date&Time column for PP 1.8 by Joy Zhou -->
               	<xsl:value-of select="$field_name_list[@name='storage_entry_id']"/>,<xsl:value-of select="$field_name_list[@name='activity_time']"/>,</xsl:when>
            	<xsl:when test="$current_field_name"><xsl:value-of select="$current_field_name"/>,</xsl:when>
				<xsl:otherwise><xsl:value-of select="$current_field_tag"/>,</xsl:otherwise> 
          	</xsl:choose>
			<!--<xsl:if test="position() != last()">,</xsl:if>-->
			<xsl:if test="position()=last()"><xsl:text>,
</xsl:text>	</xsl:if>
		</xsl:for-each>
		
		<!--/ heder fields -->
		
		<!-- content fields -->
		<xsl:for-each select="//record">
        	<xsl:variable name="row_num" select="position()"/>
       		<xsl:for-each select="@*">
            	<xsl:variable name="current_field_tag" select="name(.)"></xsl:variable>
        		<xsl:variable name="current_value" select="."></xsl:variable>
         		<xsl:choose>
                    <!-- Delete unwanted column for PP 1.8 by Joy Zhou -->  
       				<xsl:when test="$current_field_tag='storage_resend_count'"></xsl:when>
                	<xsl:when test="$current_field_tag='msg_delivery_time'"></xsl:when>
          			<xsl:when test="$current_field_tag='msg_destination'"></xsl:when>
          			<xsl:when test="$current_field_tag='msg_subject'"></xsl:when>
                    <xsl:when test="$current_field_tag='activity_time'">
                    <!-- Show Date&Time column for PP 1.8 by Joy Zhou -->
                    <xsl:value-of select="$row_num"/>,<xsl:choose><xsl:when test="name(.)=&quot;filter_action&quot;"><xsl:call-template name="escape_special"><xsl:with-param name="value" select="$action_description_list[@id=$current_value]"/></xsl:call-template>,</xsl:when>
							<xsl:otherwise>
							<xsl:call-template name="escape_special">
							<xsl:with-param name="value" select="."/>
							</xsl:call-template>,</xsl:otherwise></xsl:choose>
                    </xsl:when>
                	<xsl:otherwise>
                		<xsl:choose>
                          	<!-- Add Number column for PP 1.8 by Joy Zhou -->  
            				<xsl:when test="$current_field_tag='storage_entry_id'">
                   			<xsl:value-of select="$row_num"/>,</xsl:when>
           					
                            <xsl:when test="name(.)=&quot;filter_action&quot;"><xsl:call-template name="escape_special"><xsl:with-param name="value" select="$action_description_list[@id=$current_value]"/></xsl:call-template>,</xsl:when>
							<xsl:when test="name(.)=&quot;EventType&quot;"><xsl:call-template name="escape_special"><xsl:with-param name="value" select="$event_type_list[@id=$current_value]"/></xsl:call-template>,</xsl:when>
							<xsl:when test="name(.)=&quot;SourceType&quot;"><xsl:call-template name="escape_special"><xsl:with-param name="value" select="$source_type_list[@id=$current_value]"/></xsl:call-template>,</xsl:when>
							<xsl:otherwise>
							<xsl:call-template name="escape_special">
							<xsl:with-param name="value" select="."/>
							</xsl:call-template>,</xsl:otherwise>
                    	</xsl:choose>
               
                    </xsl:otherwise>
                </xsl:choose>
                
                <!--<xsl:if test="position()!=last()">,</xsl:if>-->
				<xsl:if test="position()=last()"><xsl:text>,
</xsl:text></xsl:if>

			</xsl:for-each>
		</xsl:for-each>
		<!--/ content fields-->
	</xsl:template>	
	
	<xsl:template name="escape_special">
		<xsl:param name="value"/>
		<xsl:choose>
			<xsl:when test="contains($value, ',') or contains($value, '&#13;') or contains($value, '&#10;')">"<xsl:call-template name="escape_quot"><xsl:with-param name="substr" select="$value"/></xsl:call-template>"</xsl:when>
			<xsl:otherwise><xsl:value-of select="$value"/></xsl:otherwise>
		</xsl:choose>	
	</xsl:template>
    
   

	
	<xsl:template name="escape_quot">
		<xsl:param name="substr"/>
		<xsl:choose>
			<xsl:when test="contains($substr, '&quot;')">
				<xsl:variable name="str_before_quot" select="substring-before($substr, '&quot;')"/>
				<xsl:variable name="str_after_quot" select="substring-after($substr, '&quot;')"/>
                <xsl:value-of select="$str_before_quot"/>""<xsl:text/>	 
                <xsl:call-template name="escape_quot">
				<xsl:with-param name="substr" select="$str_after_quot"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$substr"/>
			</xsl:otherwise>	
		</xsl:choose>
	</xsl:template>
    
   

</xsl:stylesheet>
