<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
   Name:  DistributionInformation.xsl
   Author:  James W. Allor, Matthew J. McCready
   Date:  20050920
   Description:  XSLT stylesheet that transforms the Distribution Information
                 from the MIF to the MRF.
                 Please reference the MIF Specification and the GPMS, Table 1.
   Modification History:
      Initial      Date      Change Request ID      Description
      JWA         20050920   N/A                 Initial Version 
      MMV         20080828   N/A                 Modified to only transform elements for the 
                                                  Digital form element only if the Format_Name
                                                  element is present. This was done by including
                                                  an additional 
                                                  <xsl:if test="name()='Format_Name'"> test
-->
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" indent="yes" encoding="ISO-8859-1"/>
  <xsl:template name="Distribution_Information">
   <xsl:element name="Distribution_Information">
    <xsl:call-template name="Distributor"/>
    <xsl:copy-of select="Resource_Description"/>
    <xsl:copy-of select="Distribution_Liability"/>
    <xsl:call-template name="Standard_Order_Process"/>
    <xsl:copy-of select="Technical_Prerequisites"/>
   </xsl:element>
  </xsl:template>
  
  <xsl:template name="Distributor">
   <xsl:element name="Distributor">
    <xsl:for-each select="Contact_Organization[@section='distributor']">
     <xsl:copy>
      <xsl:apply-templates select="."/>
     </xsl:copy>
    </xsl:for-each>

    <xsl:element name="Contact_Address">
     <xsl:for-each select="Address_Type[@section='distributor']">
      <xsl:copy>
       <xsl:apply-templates select="."/>
      </xsl:copy>
     </xsl:for-each>
     <xsl:for-each select="Address[@section='distributor']">
      <xsl:sort select="@gid1" data-type="number"/>
      <xsl:sort select="@gid1" data-type="text"/>
      <xsl:copy>
       <xsl:apply-templates select="."/>
      </xsl:copy>
     </xsl:for-each>
     <xsl:for-each select="City[@section='distributor'], State_or_Province[@section='distributor'], Postal_Code[@section='distributor'], Country[@section='distributor']">
      <xsl:copy>
       <xsl:apply-templates select="."/>
      </xsl:copy>
     </xsl:for-each>
    </xsl:element>

    <xsl:for-each select="Contact_Voice_Telephone[@section='distributor']">
     <xsl:sort select="@gid1" data-type="number"/>
     <xsl:sort select="@gid1" data-type="text"/>
     <xsl:copy>
      <xsl:apply-templates select="."/>
     </xsl:copy>
    </xsl:for-each>

    <xsl:for-each select="Contact_Facsimile_Telephone[@section='distributor']">
     <xsl:copy>
      <xsl:apply-templates select="."/>
     </xsl:copy>
    </xsl:for-each>

    <xsl:for-each select="Contact_Electronic_Mail_Address[@section='distributor']">
     <xsl:copy>
      <xsl:apply-templates select="."/>
     </xsl:copy>
    </xsl:for-each>
   </xsl:element>
  </xsl:template>

  <xsl:template name="Standard_Order_Process">
   <xsl:for-each-group select="Non-Digital_Form, Format_Name, File_Decompression_Technique,
Transfer_Size, Network_Resource_Name, Offline_Media, Recording_Format, Fees, Ordering_Instructions" group-by="@gid1">
    <xsl:sort select="@gid1" data-type="number"/>
    <xsl:sort select="@gid1" data-type="text"/>
    <xsl:element name="Standard_Order_Process">
     <xsl:for-each select="current-group()">
      <xsl:if test="name()='Non-Digital_Form'">
       <xsl:copy>
        <xsl:apply-templates select="."/>
       </xsl:copy>
      </xsl:if>
     </xsl:for-each>

     <xsl:for-each-group select="current-group()" group-by="@gid1">
      <xsl:sort select="@gid1" data-type="number"/>
      <xsl:sort select="@gid1" data-type="text"/>
      <xsl:for-each-group select="current-group()" group-by="@gid2">
       <xsl:sort select="@gid2" data-type="number"/>
       <xsl:sort select="@gid2" data-type="text"/>
       
        <xsl:if test="name()='Format_Name'">
        <xsl:element name="Digital_Form">
        <xsl:for-each select="current-group()">
	 <xsl:if test="name()='Format_Name'">
	  <xsl:copy>
	   <xsl:apply-templates select="."/>
	  </xsl:copy>
          </xsl:if> 
	
         <xsl:if test="name()='File_Decompression_Technique'">
	  <xsl:copy>
	   <xsl:apply-templates select="."/>
	  </xsl:copy>
	 </xsl:if>
         
         <xsl:if test="name()='Transfer_Size'">
	  <xsl:copy>
	   <xsl:apply-templates select="."/>
	  </xsl:copy>
	 </xsl:if>
	</xsl:for-each>

        <xsl:element name="Digital_Transfer_Option">
         <xsl:for-each-group select="current-group()" group-by="@gid3">
	  <xsl:sort select="@gid3" data-type="number"/>
	  <xsl:sort select="@gid3" data-type="text"/>
          <xsl:copy>
	   <xsl:apply-templates select="."/>
	  </xsl:copy>
	 </xsl:for-each-group>
         
         <xsl:for-each select="current-group()">
	  <xsl:if test="name()='Offline_Media' or name()='Recording_Format'">
	   <xsl:copy>
	    <xsl:apply-templates select="."/>
	   </xsl:copy>
          </xsl:if>
	 </xsl:for-each>
	</xsl:element>
       <!--  </xsl:if> -->
       </xsl:element><!-- Digital Form -->
         </xsl:if>
      </xsl:for-each-group>

      <xsl:for-each select="current-group()">
       <xsl:if test="name()='Fees' or name()='Ordering_Instructions'">
	<xsl:copy>
	 <xsl:apply-templates select="."/>
	</xsl:copy>
       </xsl:if>
      </xsl:for-each>
     </xsl:for-each-group>
    </xsl:element>
   </xsl:for-each-group>
  </xsl:template>
</xsl:stylesheet>
