<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xsl:output method="text" indent="no" omit-xml-declaration="yes"/>

	<xsl:param name="ignoreDisallowedData">TRUE</xsl:param>

	<xsl:template match="text()"/>

	<xsl:template match="MUSICNET_COMPONENT">
        <xsl:apply-templates/>
        <xsl:apply-templates select="." mode="check-cardinality"/>
        <xsl:apply-templates select="." mode="check-territory-metadata"/>
    </xsl:template>

    <!-- Various Metadata Specific checks -->
    <xsl:include href="GenreCodes.xsl"/>
    <xsl:include href="ParentalGuidelineRatings.xsl"/>
    <xsl:include href="ParentalContentLabels.xsl"/>
	<xsl:include href="Languages.xsl"/>
	<xsl:include href="MetadataCardinality.xsl"/>

    <xsl:template match="METADATA[TYPE='LABEL']">
	<xsl:variable name="vMetaLabel" select="translate(VALUE,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
	<xsl:variable name="vLabel" select="translate(../LABEL_CODE,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
	<!-- It has to match the LABEL_CODE attribute -->
	<xsl:if test="not($vMetaLabel=$vLabel)">
		Error: LABEL metadata does not match LABEL_CODE on <xsl:value-of select="../MUSICNET_COMPONENT_CODE"/>
	</xsl:if>
    </xsl:template>

    <xsl:template match="METADATA[TYPE='DURATION']">
	<!-- format min:sec -->
	<xsl:if test="not(translate(VALUE, '0123456789','9999999999')='9:99' or
			translate(VALUE, '0123456789','9999999999')='99:99' or
			translate(VALUE, '0123456789','9999999999')='999:99')">
		Error: wrong format of DURATION on <xsl:value-of select="../MUSICNET_COMPONENT_CODE"/> value: <xsl:value-of select="VALUE"/>
	</xsl:if>
    </xsl:template>

    <xsl:template match="METADATA[TYPE='RELEASE_DATE']">
		<!-- format YYYY or YYYYMM or YYYYMMDD -->
		<xsl:if test="not(translate(VALUE, '0123456789','9999999999')='9999' or
				  translate(VALUE, '0123456789','9999999999')='999999' or
				  translate(VALUE, '0123456789','9999999999')='99999999')">
			Error: wrong format of RELEASE_DATE on <xsl:value-of select="../MUSICNET_COMPONENT_CODE"/> value: <xsl:value-of select="VALUE"/>
		</xsl:if>
    </xsl:template>

    <xsl:template match="METADATA[TYPE='TITLE']">
	<xsl:if test="VALUE=''">
		Error: METADATA TITLE has an empty value on <xsl:value-of select="../MUSICNET_COMPONENT_CODE"/>
	</xsl:if>
    </xsl:template>

    <xsl:template match="ARTIST">
	<xsl:if test="VALUE=''">
		Error: ARTIST name/value has an empty value on <xsl:value-of select="../MUSICNET_COMPONENT_CODE"/>
	</xsl:if>
    </xsl:template>

    <xsl:template match="PRICE[TYPE='LABEL_COST']">
    	<!-- Ensure price date ranges are valid for each element and each group of elements for same
	     territory and currency -->
	<xsl:variable name="priceEffFromDate" select="number(translate(EFFECTIVE_FROM_DATE,'-',''))"/>
	<!-- assign eff to date to variable or max date if not found -->
	<xsl:variable name="priceEffToDate"><xsl:if test="EFFECTIVE_TO_DATE"><xsl:value-of select="number(translate(EFFECTIVE_TO_DATE,'-',''))"/></xsl:if><xsl:if test="not(EFFECTIVE_TO_DATE)"><xsl:value-of select="number(99991231)"/></xsl:if></xsl:variable>
	<xsl:if test="$priceEffToDate &lt; $priceEffFromDate">
		Error: Price EFFECTIVE_TO_DATE cannot be less than EFFECTIVE_FROM_DATE.  Comp Code is: <xsl:value-of select="../MUSICNET_COMPONENT_CODE"/>
	</xsl:if>

	<xsl:variable name="price" select="."/>
	<xsl:variable name="territory" select="TERRITORY_CODE"/>
	<xsl:variable name="currency" select="CURRENCY_CODE"/>
	<xsl:variable name="scope"><xsl:if test="SCOPE"><xsl:value-of select="SCOPE"/></xsl:if><xsl:if test="not(SCOPE)">DMS_PRICE</xsl:if></xsl:variable>
	<xsl:for-each select="../PRICE[TYPE='LABEL_COST'][TERRITORY_CODE=$territory][CURRENCY_CODE=$currency]">
		<xsl:if test="$price != .">
			<xsl:variable name="scope2"><xsl:if test="SCOPE"><xsl:value-of select="SCOPE"/></xsl:if><xsl:if test="not(SCOPE)">DMS_PRICE</xsl:if></xsl:variable>
			<!-- Make sure price info is for same scope -->
			<xsl:if test="$scope = $scope2">
				<xsl:variable name="priceEffFromDate2" select="number(translate(EFFECTIVE_FROM_DATE,'-',''))"/>
				<!-- assign eff to date to variable or max date if not found -->
				<xsl:variable name="priceEffToDate2"><xsl:if test="EFFECTIVE_TO_DATE"><xsl:value-of select="number(translate(EFFECTIVE_TO_DATE,'-',''))"/></xsl:if><xsl:if test="not(EFFECTIVE_TO_DATE)"><xsl:value-of select="number(99991231)"/></xsl:if></xsl:variable>

				<xsl:if test="not($priceEffFromDate &gt; $priceEffToDate2 or $priceEffToDate &lt; $priceEffFromDate2)">
					Error: Price effective date periods overlap for price elements with same scope territory and currency.  Comp Code is: <xsl:value-of select="../MUSICNET_COMPONENT_CODE"/>
				</xsl:if>
			</xsl:if>
		</xsl:if>
	</xsl:for-each>
    </xsl:template>

    <!-- Ensure date ranges are valid for any right type -->
    <xsl:template match="STREAM_RIGHT|DOWNLOAD_RIGHT|_CD_BURN_RIGHT|TRANSFER2PD_RIGHT|FREE_STREAM_RIGHT|DIGITAL_AVAILABILITY|LABEL_APPR_IND|PUBLISHER_APPR_IND|PUBLISHER_OVERRIDE_CODE">
	<xsl:variable name="beginDate"><xsl:if test="BEGIN_DATE"><xsl:value-of select="number(translate(BEGIN_DATE,'-',''))"/></xsl:if><xsl:if test="not(BEGIN_DATE)"><xsl:value-of select="number(00010101)"/></xsl:if></xsl:variable>
	<xsl:variable name="stopDate"><xsl:if test="STOP_DATE"><xsl:value-of select="number(translate(STOP_DATE,'-',''))"/></xsl:if><xsl:if test="not(STOP_DATE)"><xsl:value-of select="number(99991231)"/></xsl:if></xsl:variable>
	<xsl:if test="$stopDate &lt; $beginDate">
		Error: STOP_DATE must not be less than BEGIN_DATE on: <xsl:value-of select="../MUSICNET_COMPONENT_CODE"/>  Begin Date: <xsl:value-of select="BEGIN_DATE"/>  End Date: <xsl:value-of select="STOP_DATE"/>
	</xsl:if>
	<!-- See if any matching right/territory follow this one.  That is ambigious and thus not permitted. -->
	<xsl:if test="count(following-sibling::*[name()=name(current()) and TERRITORY_CODE=current()/TERRITORY_CODE]) > 0">
            Error: More than one right <xsl:value-of select="name()"/> for territory <xsl:value-of select="./TERRITORY_CODE"/> for <xsl:value-of select="../MUSICNET_COMPONENT_CODE"/>.
	</xsl:if>
    </xsl:template>

</xsl:stylesheet>
