<?xml version="1.0" encoding="UTF-8"?>

<!-- stylesheet for It's Time for B2B! -->
<xsl:stylesheet version="1.0" xmlns:xalan="http://xml.apache.org/xslt" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <!-- stylesheet parameter specifying a date and time in ISO 8601:2000 format -->
    <xsl:param name="datetime"/>

    <!-- output pretty-printed results as XHTML 1.0 -->
    <xsl:output encoding="UTF-8" indent="yes" method="xml" xalan:indent-amount="4"/>

    <!-- default template converting $datetime to OAG's DATETIME format -->
    <xsl:template match="/">
        <!-- date is all characters before 'T' -->
        <xsl:variable name="date"><xsl:value-of select="substring-before($datetime, 'T')"/></xsl:variable>
        <!-- time is all characters after 'T' -->
        <xsl:variable name="time"><xsl:value-of select="substring-after($datetime, 'T')"/></xsl:variable>
        
        <!-- create a 'DATETIME' element -->
        <xsl:element name="DATETIME">
            
            <!-- create a 'qualifier' attribute whose value is hard-coded as 'SHIP' -->
            <xsl:attribute name="qualifier">SHIP</xsl:attribute>
            
            <!-- create a 'YEAR' element whose value is all the character before the first '-' of variable, date -->
            <xsl:element name="YEAR"><xsl:value-of select="substring-before($date, '-')"/></xsl:element>
            
            <!-- create a 'MONTH' element whose value is all the characters in between the first and second '-' of variable, date -->
            <xsl:element name="MONTH"><xsl:value-of select="substring-before(substring-after($date, '-'), '-')"/></xsl:element>
            
            <!-- create a 'DAY' element whose value is all the characters after the second '-' of variable, date -->
            <xsl:element name="DAY"><xsl:value-of select="substring-after(substring-after($date, '-'), '-')"/></xsl:element>
            
            <xsl:choose>
                <!-- timezone is 'WEST' -->
                <xsl:when test="contains($time, '-')">
                    <!-- basic time is all the characters before '-'; assign it to a variable, timevalue -->
                    <xsl:variable name="timevalue"><xsl:value-of select="substring-before($time, '-')"/></xsl:variable>
                    <!-- timezone is all the characters after '-' -->
                    <xsl:variable name="timezone"><xsl:value-of select="substring-after($time, '-')"/></xsl:variable>
                    
                    <!-- create an 'HOUR' element whose value is all the characters before the first ':' of variable, timevalue -->
                    <xsl:element name="HOUR"><xsl:value-of select="substring-before($timevalue, ':')"/></xsl:element>
                    <!-- create a 'MINUTE' element whose value is all the charaacters in between the first and second ':' of variable, timevalue -->
                    <xsl:element name="MINUTE"><xsl:value-of select="substring-before(substring-after($timevalue, ':'), ':')"/></xsl:element>
                    <!-- create a 'SECOND' element whose value is all characters after the second ':' of variable, timevalue -->
                    <xsl:element name="SECOND"><xsl:value-of select="substring-after(substring-after($timevalue, ':'), ':')"/></xsl:element>
                    <!-- create a 'TIMEZONE' element whose value is whatever in the variable, timezone -->
                    <xsl:element name="TIMEZONE">
                        <xsl:value-of select="'-'"/><xsl:value-of select="substring-before($timezone, ':')"/><xsl:value-of select="substring-after($timezone, ':')"/>
                    </xsl:element>
                </xsl:when>
                
                <!-- timezone is 'EAST' -->
                <xsl:when test="contains($time, '+')">
                    <!-- basic time is all the characters before '+'; assign it to a variable, timevalue -->
                    <xsl:variable name="timevalue"><xsl:value-of select="substring-before($time, '+')"/></xsl:variable>
                    <!-- timezone is all the characters after '+' -->
                    <xsl:variable name="timezone"><xsl:value-of select="substring-after($time, '+')"/></xsl:variable>
                    
                    <!-- create an 'HOUR' element whose value is all the characters before the first ':' of variable, timevalue -->
                    <xsl:element name="HOUR"><xsl:value-of select="substring-before($timevalue, ':')"/></xsl:element>
                    <!-- create a 'MINUTE' element whose value is all the charaacters in between the first and second ':' of variable, timevalue -->
                    <xsl:element name="MINUTE"><xsl:value-of select="substring-before(substring-after($timevalue, ':'), ':')"/></xsl:element>
                    <!-- create a 'SECOND' element whose value is all characters after the second ':' of variable, timevalue -->
                    <xsl:element name="SECOND"><xsl:value-of select="substring-after(substring-after($timevalue, ':'), ':')"/></xsl:element>
                    <!-- create a 'TIMEZONE' element whose value is whatever in the variable, timezone -->
                    <xsl:element name="TIMEZONE">
                        <xsl:value-of select="'+'"/><xsl:value-of select="substring-before($timezone, ':')"/><xsl:value-of select="substring-after($timezone, ':')"/>
                    </xsl:element>
                </xsl:when>
            </xsl:choose>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>
