<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output encoding="utf-8" />

  <xsl:template match="Root">
    <import specVersion="1.0" xsi:noNamespaceSchemaLocation="RoyaltyShare_Import_v1.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
      <xsl:for-each select="Album">
        <xsl:call-template name="albumXML" />
      </xsl:for-each>
    </import>
  </xsl:template>

  <xsl:template name="albumXML">
    <album>
      <action>insert</action>
      <title><xsl:value-of select="Title" /></title>

      <xsl:call-template name="albumXML_artists" />
      <xsl:call-template name="albumXML_label" />

      <catalogNumber><xsl:value-of select="CatalogNumber:" /></catalogNumber>

      <xsl:if test="normalize-space(ClientAlbumID)">
        <clientID><xsl:value-of select="ClientAlbumID" /></clientID>
      </xsl:if>

      <xsl:call-template name="albumXML_customFields" />
      <xsl:call-template name="albumXML_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="albumXML_products" />

      <xsl:if test="Products/Product[ProductTypeID = 3]/ProductDigital/DistAllow" >
        <clearedForDistribution><xsl:value-of select="Products/Product[ProductTypeID = 3]/ProductDigital/DistAllow" /></clearedForDistribution>
      </xsl:if>

      <xsl:call-template name="albumXML_territoryRestrictions" >
        <xsl:with-param name="productDigital" select="Products/Product[ProductTypeID = 3]/ProductDigital/DistAllow" />
      </xsl:call-template>

      <xsl:call-template name="albumXML_prices" >
        <xsl:with-param name="productDigital" select="Products/Product[ProductTypeID = 3]/ProductDigital" />
      </xsl:call-template>

      <xsl:call-template name="trackXML" />

    </album>
  </xsl:template>

  <xsl:template name="albumXML_artists">
    <artists>
      <primary>
        <artist>
          <displayName><xsl:value-of select="ArtistName" /></displayName>
        </artist>
      </primary>
      <xsl:if test="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="albumXML_label">
    <xsl:if test="normalize-space(Label/LabelName)">
      <label>
        <displayName><xsl:value-of select="Label/LabelName" /></displayName>
      </label>
    </xsl:if>
  </xsl:template>

  <xsl:template name="albumXML_customFields">
    <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:template>

  <xsl:template name="albumXML_genres">
    <xsl:if test="normalize-space(GenrePrimaryName)">
      <genres>
        <primary>
          <genre><xsl:value-of select="GenrePrimaryName" /></genre>
        </primary>
        <xsl:if test="GenreSecondaryName">
          <secondary>
            <genre><xsl:value-of select="GenreSecondaryName" /></genre>
          </secondary>
        </xsl:if>
      </genres>
    </xsl:if>
  </xsl:template>

  <xsl:template name="albumXML_products">
    <products>
      <xsl:for-each select="ProductList/Product" >
        <product>
          <type>
            <xsl:call-template name="productType">
              <xsl:with-param name="productTypeID" select="ProductTypeID" />
            </xsl:call-template>
          </type>
          <upc><xsl:value-of select="UPC" /></upc>
          <releaseDate><xsl:value-of select="ReleaseDate" /></releaseDate>
        </product>
      </xsl:for-each>
    </products>
  </xsl:template>

  <xsl:template name="productType">
    <xsl:param name="productTypeID" />

    <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 = 4">Digital Track</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:otherwise></xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="albumXML_territoryRestrictions">
    <xsl:param name="productDigital" />
    <xsl:if test="normalize-space($productDigital/TerritoriesFlag)">
      <territoryRestrictions>
        <xsl:attribute name="type">
          <xsl:choose>
            <xsl:when test="$productDigital/TerritoriesFlag = 'A'">allow</xsl:when>
            <xsl:otherwise>deny</xsl:otherwise>
          </xsl:choose>
        </xsl:attribute>
        <xsl:for-each select="$productDigital/Territories/Country">
          <countryCode><xsl:value-of select="CountryCode" /></countryCode>
        </xsl:for-each>
      </territoryRestrictions>
    </xsl:if>
  </xsl:template>

  <xsl:template name="albumXML_prices">
    <xsl:param name="productDigital" />
    <xsl:if test="$productDigital/DistributionServices/DistributionService/PricingTiers[HasPricingTier = 1]">
      <prices>
        <xsl:for-each select="$productDigital/DistributionServices/DistributionService">
          <xsl:call-template name="albumXML_servicePrice" >
            <xsl:with-param name="tiers" select="PricingTiers" />
          </xsl:call-template>
        </xsl:for-each>
      </prices>
    </xsl:if>
  </xsl:template>

  <xsl:template name="albumXML_servicePrice">
    <xsl:param name="tiers" />

    <xsl:if test="$tiers/PricingTier[Current = 1]" >
      <xsl:for-each select="$tiers/PricingTier">
        <xsl:if test="Current = 1">
          <price>
            <wholesaleCode><xsl:value-of select="TierName" /></wholesaleCode>
            <service><xsl:value-of select="../../ServiceName" /></service>
          </price>
        </xsl:if>
      </xsl:for-each>
    </xsl:if>
  </xsl:template>


  <xsl:template name="trackXML">
    <tracks>
      <xsl:for-each select="Tracks/Track">
        <xsl:call-template name="trackXML_detail" />
      </xsl:for-each>
    </tracks>
  </xsl:template>

  <xsl:template name="trackXML_detail">
    <track>
      <xsl:call-template name="trackXML_productPosition" >
        <xsl:with-param name="trackID" select="TrackID" />
      </xsl:call-template>

      <title><xsl:value-of select="Title" /></title>

      <xsl:call-template name="trackXML_artists" />

      <xsl:call-template name="trackXML_writers" />

      <duration><xsl:value-of select="Master/Duration" /></duration>

      <xsl:call-template name="trackXML_parentalAdvisory" />

      <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="trackXML_mediaType" />

      <xsl:call-template name="trackXML_albumOnlyPurchase" >
        <xsl:with-param name="trackID" select="TrackID" />
      </xsl:call-template>

      <xsl:call-template name="trackXML_customFields" />

    </track>
  </xsl:template>

  <xsl:template name="trackXML_productPosition">
    <xsl:param name="trackID" />
    <xsl:if test="count(../../Products/Product) &gt; 0">
      <productPositions>
        <xsl:for-each select="../../Products/Product" >
          <xsl:for-each select="TrackList/Track">
            <xsl:if test="$trackID = TrackID">
              <productPosition>
                <type>
                  <xsl:call-template name="productType">
                    <xsl:with-param name="productTypeID" select="../../ProductTypeID" />
                  </xsl:call-template>
                </type>
                <upc><xsl:value-of select="../../UPC" /></upc>
                <discNumber><xsl:value-of select="DiscNumber" /></discNumber>
                <trackNumber><xsl:value-of select="DiscTrack" /></trackNumber>
              </productPosition>
            </xsl:if>
          </xsl:for-each>
        </xsl:for-each>
      </productPositions>
    </xsl:if>
  </xsl:template>

  <xsl:template name="trackXML_artists">
    <artists>
      <primary>
        <artist>
          <displayName><xsl:value-of select="ArtistName" /></displayName>
        </artist>
      </primary>
      <xsl:if test="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="trackXML_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="trackXML_parentalAdvisory">
    <parentalAdvisory>
      <xsl:choose>
        <xsl:when test="ParentalAdvisory = 'E'">E</xsl:when>
        <xsl:when test="ParentalAdvisory = 'C'">C</xsl:when>
        <xsl:otherwise>N</xsl:otherwise>
      </xsl:choose>
    </parentalAdvisory>
  </xsl:template>

  <xsl:template name="trackXML_mediaType">
    <mediaType>
      <xsl:choose>
        <xsl:when test="MediaType = 2">video</xsl:when>
        <xsl:otherwise>audio</xsl:otherwise>
      </xsl:choose>
    </mediaType>
  </xsl:template>

  <xsl:template name="trackXML_customFields">
    <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:template>

  <xsl:template name="trackXML_albumOnlyPurchase">
    <xsl:param name="trackID" />
    <xsl:if test="../../Products/Product[ProductTypeID = 3]/TrackList/Track[TrackID = $trackID]/AlbumOnly">
      <albumOnlyPurchase>true</albumOnlyPurchase>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>
