<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
  <xsl:output omit-xml-declaration="yes" />
  <xsl:template match="album">
    <album>
	  <action>insert</action>
	  <title><xsl:value-of select="Title" /></title>
	  <xsl:call-template name="artists" />
	  <xsl:call-template name="label" />
	  <catalogNumber><xsl:value-of select="CatalogNumber" /></catalogNumber>
	  <xsl:if test="normalize-space(clientID)">
		<clientID><xsl:value-of select="ClientID" /></clientID>
	  </xsl:if>
	  <xsl:if test="normalize-space(custom1)">
		<custom1><xsl:value-of select="Custom1" /></custom1>
	  </xsl:if>
	  <xsl:if test="normalize-space(custom2)">
		<custom2><xsl:value-of select="Custom2" /></custom2>
	  </xsl:if>
	  <xsl:if test="normalize-space(custom3)">
		<custom3><xsl:value-of select="Custom3" /></custom3>
	  </xsl:if>
	  <xsl:call-template name="genres" />
	  <xsl:if test="normalize-space(CLine)">
		<cline><xsl:value-of select="CLine" /></cline>
	  </xsl:if>
	  <xsl:if test="normalize-space(PLine)">
		<pline><xsl:value-of select="PLine" /></pline>
	  </xsl:if>

	  <xsl:call-template name="products" />
	  <xsl:call-template name="territory_restrictions" />
	  <xsl:call-template name="prices" />
	  <xsl:call-template name="tracks" />
	</album>
  </xsl:template>


  <xsl:template name="artists">
	<artists>
	  <primary>
	    <artist>
		  <displayName><xsl:value-of select="ArtistName" /></displayName>
  	  </artist>
	  </primary>
	  <xsl:if test="count(AdditionalArtistList/AdditionalArtist)" >
	    <contributors>
		  <xsl:for-each select="AdditionalArtistList/AdditionalArtist">
		    <artist>
			  <displayName><xsl:value-of select="Name" /></displayName>
  			<role><xsl:value-of select="Role/Role" /></role>
		    </artist>
  		</xsl:for-each>
	    </contributors>
	  </xsl:if>
	</artists>
  </xsl:template>


  <xsl:template name="writers" >
	<xsl:if test="normalize-space(Master/Song/ComposerName)" >
	<writers>
	    <writer>
		  <displayName><xsl:value-of select="Master/Song/ComposerName" /></displayName>
		  <role>composer</role>
	    </writer>
	</writers>
    </xsl:if>
  </xsl:template>


  <xsl:template name="genres">
	<genres>
	  <primary>
		<genre><xsl:value-of select="GenrePrimaryName" /></genre>
	  </primary>
	  <secondary>
		<genre><xsl:value-of select="GenreSecondaryName" /></genre>
	  </secondary>
	</genres>
  </xsl:template>


  <xsl:template name="label">
	<label><displayName><xsl:value-of select="Label/LabelName" /></displayName></label>
  </xsl:template>


  <xsl:template name="products">
	<xsl:if test="count(ProductList/Product)" >
	  <products>
		<xsl:for-each select="ProductList/Product">
		  <product>
			<type><xsl:value-of select="type" /><xsl:call-template name="product_type" /></type>
			<upc><xsl:value-of select="UPC" /></upc>
			<xsl:if test="normalize-space(ReleaseDate)">
			  <releaseDate><xsl:value-of select="ReleaseDate" /></releaseDate>
			</xsl:if>
			<productID><xsl:value-of select="ProductCode" /></productID>
		  </product>
		</xsl:for-each>
	  </products>
	</xsl:if>
  </xsl:template>


  <xsl:template name="tracks">
	<tracks>
    <xsl:for-each select="TrackList/Track">
	  <track>
	    <xsl:call-template name="product_position" />
        <title><xsl:value-of select="Title" /></title>
	    <xsl:call-template name="artists" />
	    <xsl:call-template name="writers" />
        <duration><xsl:value-of select="Master/Duration" /></duration>
	    <xsl:call-template name="parental_advisory" />
	    <xsl:if test="normalize-space(ClientTrackID)">
  		  <clientID><xsl:value-of select="ClientTrackID" /></clientID>
	    </xsl:if>
        <isrc><xsl:value-of select="Master/ISRC" /></isrc>
	    <xsl:call-template name="media_type" />
	    <xsl:call-template name="album_only" />
	    <xsl:if test="normalize-space(Custom1)">
    	  <custom1><xsl:value-of select="Custom1" /></custom1>
	    </xsl:if>
	    <xsl:if test="normalize-space(Custom2)">
  		  <custom2><xsl:value-of select="Custom2" /></custom2>
	    </xsl:if>
	    <xsl:if test="normalize-space(Custom3)">
  		  <custom3><xsl:value-of select="Custom3" /></custom3>
	    </xsl:if>
	  </track>
	</xsl:for-each>
	</tracks>
  </xsl:template>


  <xsl:template name="product_position" >
	<xsl:if test="count(ProductList/Product)" >
	  <productPositions>
		<xsl:for-each select="ProductList/Product">
		  <productPosition>
			<type><xsl:value-of select="type" /><xsl:call-template name="product_type" /></type>
			<upc><xsl:value-of select="UPC" /></upc>
			<discNumber><xsl:value-of select="DiscNumber" /></discNumber>
			<trackNumber><xsl:value-of select="DiscTrack" /></trackNumber>
		  </productPosition>
		</xsl:for-each>
	  </productPositions>
	</xsl:if>
  </xsl:template>


  <xsl:template name="territory_restrictions" >
	<xsl:for-each select="ProductList/Product">
	  <xsl:if test="ProductDigital/Territories/Country">
	    <xsl:variable name="restrictionType"><xsl:choose>
	      <xsl:when test="ProductDigital/TerritoriesFlag = 'A'">allow</xsl:when>
	      <xsl:when test="ProductDigital/TerritoriesFlag = 'D'">deny</xsl:when>
	    </xsl:choose></xsl:variable>

	    <territoryRestrictions type="{$restrictionType}">
	      <xsl:for-each select="ProductDigital/Territories/Country">
		    <countryCode><xsl:value-of select="CountryCode" /></countryCode>
		  </xsl:for-each>
	    </territoryRestrictions>
	  </xsl:if>
	</xsl:for-each>
  </xsl:template>


  <xsl:template name="prices" >
	<xsl:for-each select="ProductList/Product">
	  <xsl:if test="ProductDigital/DistributionServices/DistributionService/PricingTiers[CurrentPriceTierID &gt; 0]">
	    <prices>
	      <xsl:for-each select="ProductDigital/DistributionServices/DistributionService/PricingTiers[CurrentPriceTierID &gt; 0]">
			<xsl:call-template name="price_tier">
			  <xsl:with-param name="tier_id"><xsl:value-of select="CurrentPriceTierID" /></xsl:with-param>
			</xsl:call-template>
		  </xsl:for-each>
        </prices>
	  </xsl:if>
	</xsl:for-each>
  </xsl:template>


  <xsl:template name="price_tier" >
	<xsl:param name="tier_id" />
    <xsl:for-each select="PricingTier[ServicePriceTierID = $tier_id]">
      <price>
        <wholesaleCode><xsl:value-of select="TierCode" /></wholesaleCode>
        <service>
          <xsl:choose>
            <xsl:when test="ServiceID = 262">Amazon</xsl:when>
            <xsl:when test="ServiceID = 11">iTunes</xsl:when>
          </xsl:choose>
        </service>
      </price>
	</xsl:for-each>
  </xsl:template>


  <xsl:template name="album_only" >
	<xsl:for-each select="ProductList/Product[ProductTypeID = 3]">
	  <xsl:if test="normalize-space(AlbumOnly)">
	    <albumOnlyPurchase><xsl:value-of select="AlbumOnly" /></albumOnlyPurchase>
	  </xsl:if>
	</xsl:for-each>
  </xsl:template>


  <xsl:template name="parental_advisory" >
	<xsl:if test="normalize-space(ParentalAdvisory)">
	  <parentalAdvisory><xsl:value-of select="ParentalAdvisory" /></parentalAdvisory>
	</xsl:if>
  </xsl:template>


  <xsl:template name="media_type" >
	<mediaType><xsl:choose>
	  <xsl:when test="MediaType = 1">audio</xsl:when>
	  <xsl:when test="MediaType = 2">video</xsl:when>
	</xsl:choose></mediaType>
  </xsl:template>


  <xsl:template name="product_type" >
    <xsl:choose>
	  <xsl:when test="ProductTypeID = 1">Vinyl</xsl:when>
	  <xsl:when test="ProductTypeID = 2">CD</xsl:when>
	  <xsl:when test="ProductTypeID = 3">Digital</xsl:when>
	  <xsl:when test="ProductTypeID = 5">VHS</xsl:when>
	  <xsl:when test="ProductTypeID = 6">Cassette</xsl:when>
	  <xsl:when test="ProductTypeID = 7">EP</xsl:when>
	  <xsl:when test="ProductTypeID = 9">DVD</xsl:when>
	  <xsl:when test="ProductTypeID = 11">CD Single</xsl:when>
	  <xsl:when test="ProductTypeID = 12">Cassette Single</xsl:when>
	  <xsl:when test="ProductTypeID = 13">DVD/CD Set</xsl:when>
	  <xsl:when test="ProductTypeID = 14">Double CD</xsl:when>
	  <xsl:when test="ProductTypeID = 15">Ringtone</xsl:when>
	</xsl:choose>
  </xsl:template>


</xsl:stylesheet>
