<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet 
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    exclude-result-prefixes="xsl"
    >
    
<xsl:output method="xml" media-type="application/xml" />
 
  <!-- Simple identity function to output the rest of the policy-->
  
 <xsl:template match="@*|*|processing-instruction()|comment()" priority="-2" >
  <xsl:copy>
    <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()" />
  </xsl:copy>

 </xsl:template>
 


<!-- remove duplicate EXTENSION elements -->

<xsl:template match="//*[local-name()='DATA-GROUP']/*[local-name()='EXTENSION']">
    <xsl:variable name="duplicates">
    <xsl:call-template name="detectDuplicateSiblings">
    	<xsl:with-param name="node" select="."/>
    </xsl:call-template>
    </xsl:variable>
	   <xsl:if test="$duplicates!='true'">
   			<xsl:copy-of select="."/>
   		</xsl:if> 
   		
</xsl:template>


<xsl:template name="detectDuplicateSiblings">
<xsl:param name="node" select="/.."/>
<xsl:variable name="A" select="$node/descendant::*[(not(text()) or not(normalize-space(string(.))='')) and count(child::*)=0]"/>
<xsl:variable name="extensionLeaves" select="following-sibling::*[local-name()='EXTENSION']/descendant::*[(not(text()) or not(normalize-space(string(.))='')) and count(child::*)=0]"/>
<xsl:variable name="isTrue">
<xsl:call-template name="loopExtensions">
	<xsl:with-param name="count" select="0"/>
	<xsl:with-param name="contextNodeToTest" select="$A"/>
	<xsl:with-param name="nodeList" select="$extensionLeaves"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$isTrue"/>
</xsl:template>


   <xsl:template name="loopExtensions">
      	<xsl:param name="count" select="/.."/>
        <xsl:param name="nodeList" select="/.."/>
        <xsl:param name="contextNodeToTest" select="/.."/>
        <xsl:variable name="isEqualDeep">
			<xsl:call-template name="recurseComparison">
				<xsl:with-param name="A" select="$contextNodeToTest"/>
				<xsl:with-param name="B" select="$nodeList[$count]"/>
				<xsl:with-param name="level" select="0"/>
			</xsl:call-template>
		</xsl:variable>
		<xsl:choose>
			<xsl:when test="$isEqualDeep='true'">true</xsl:when>
			<xsl:otherwise>
				<xsl:if test="$count&lt;count($nodeList)">
				  	<xsl:call-template name="loopExtensions">
          				<xsl:with-param name="count" select="$count + 1"/>
          				<xsl:with-param name="nodeList" select="$nodeList"/>
          				<xsl:with-param name="contextNodeToTest" select="$contextNodeToTest"/>
        			</xsl:call-template>
        		</xsl:if>
			</xsl:otherwise>
      </xsl:choose>
  </xsl:template>
 



<xsl:template name="recurseComparison">
	<xsl:param name="A" select="/.."/>
	<xsl:param name="B" select="/.."/>
	<xsl:param name="level" select="/.."/>
	<xsl:variable name="isEqual"><xsl:if test="local-name($A)=local-name($B)">true</xsl:if></xsl:variable>
<xsl:choose>
	<xsl:when test="$isEqual='true'">
		<xsl:choose>
			<xsl:when test="local-name($A/parent::*)!='datatype' and local-name($B/parent::*)!='datatype' and count($A/parent::*)!=0 ">
					<xsl:call-template name="recurseComparison">
						<xsl:with-param name="A" select="$A/parent::*"/>
						<xsl:with-param name="B" select="$B/parent::*"/>
						<xsl:with-param name="level" select="$level+1"/>
					</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>true</xsl:otherwise>
		</xsl:choose>
	</xsl:when>
	<xsl:otherwise>false</xsl:otherwise>
	</xsl:choose>

</xsl:template>


<!-- remove duplicate DATA elements -->
<xsl:template match="//*[local-name()='DATA']">
<xsl:variable name="ref" select="@*[local-name()='ref']"/>

	<xsl:if test="not($ref=following-sibling::*[local-name()='DATA']/@*[local-name()='ref'])">
			<xsl:copy-of select="."/>		
	</xsl:if>
 </xsl:template>
 
 

 
 
</xsl:stylesheet>

