<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet 
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.org/2002/01/P3Pv1"
    exclude-result-prefixes="xsl">
    
<!-- 
****************************************************************************************************
NOTE that this transform only works if you do not already have data elements in both formats. 
Otherwise it will create copies.
Given time, a template could be added which would recognize duplicates but it's not really a use case for this transform anyway.
 ****************************************************************************************************
 -->
  <!-- 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()" />
  </xsl:copy>
 </xsl:template>
 
 <!--
****************************************************************************************************
 Transform new data elements to old data elements and add the transformations alongside
 Works by getting the leaves and then going back up to the top (as there can be multiple leaves now.
****************************************************************************************************
  -->
  
<xsl:template match="//*[local-name()='EXTENSION'][count(child::*[local-name()='datatype'])!=0]">
 <xsl:for-each select=".//*[count(child::*[local-name()!='CATEGORIES'])=0 and count(ancestor::*[local-name()='CATEGORIES'])=0]">
 		<xsl:element name="DATA">
			<xsl:attribute name="ref">
				<xsl:variable name="tempStr"><xsl:call-template name="StrConcat">
						<xsl:with-param name="dot">.</xsl:with-param>
					</xsl:call-template></xsl:variable>
				<xsl:value-of select="concat('#',substring-after($tempStr,'.'))"/>
			</xsl:attribute>
				<xsl:if test="count(.//*[local-name()='CATEGORIES'])!=0">
					<xsl:copy-of select=".//*[local-name()='CATEGORIES']"/>										
				</xsl:if>
				<xsl:value-of select="."/>
			</xsl:element>
</xsl:for-each>
<xsl:copy-of select="."/>	
</xsl:template>
 
  <!--
   ****************************************************************************************************
   Transform old data elements and add the transformations alongside 
    ****************************************************************************************************
   -->
 <xsl:template match="//*[local-name()='DATA']" >
 	<xsl:element name="EXTENSION">
 		<xsl:element name="datatype">
 					<xsl:if test="@*[local-name()='optional']">
 						<xsl:copy-of select="@*[local-name()='optional']"/>
 					</xsl:if>
					<xsl:variable name="refAttribute" select="@*[local-name()='ref']"/>
					
					<xsl:call-template name="StrSplit">
						<xsl:with-param name="str" select="substring-after($refAttribute,'#')"/>
					</xsl:call-template>
		</xsl:element>
	</xsl:element>
	<xsl:copy-of select="."/>
 </xsl:template>



               
<!--***************************************************************************************************************** 
reconsitute the ref attributes - for backward (P3P1.1 -> P3P1.0) transform
****************************************************************************************************************-->
 <xsl:template name="StrConcat">
	 <xsl:param name="dot" select="/.."/>
		<xsl:if test="local-name()!='datatype'">
					<xsl:for-each select="parent::*">
	 					<xsl:call-template name="StrConcat">
	 						<xsl:with-param name="dot">.</xsl:with-param>
	 					</xsl:call-template>
	 				</xsl:for-each>
	 					<xsl:value-of select="$dot"/>
	 			<!-- Change the business.name attribute if there is one -->
	 				<xsl:choose>
	 					<xsl:when test="local-name()='orgname'">name</xsl:when>
	 					<xsl:otherwise>
	 						<xsl:value-of select="local-name()"/>
	 					</xsl:otherwise>
	 				</xsl:choose>
	 					
    	</xsl:if>
    		
	
</xsl:template>
                

<!--
 ****************************************************************************************************
String Split for forward (P3P1.0 -> P3P1.1) transform 
 ****************************************************************************************************
-->
 <xsl:template name="StrSplit">
        <xsl:param name="str" select="/.."/>
        <xsl:param name="parent" select="/.."/>
        	<xsl:choose>
        		<xsl:when test="contains($str,'.') and substring-before($str,'.')!='ymd' and substring-before($str,'.')!='hms'">
                	<xsl:element name="{substring-before($str,'.')}">
						<xsl:call-template name="StrSplit">
    	                    <xsl:with-param name="str" select="substring-after($str,'.')" />
    	                    <xsl:with-param name="parent" select="$str" />
        	        	</xsl:call-template>
        			</xsl:element>
				</xsl:when>
<!-- Change the business name to Orgname so it doesn't expect children like firstname, prefix etc... --> 
	 			<xsl:when test="$parent='business.name'">
	 				<xsl:element name="orgname">
						<xsl:for-each select="./*[local-name()='CATEGORIES']">
							<xsl:copy-of select="."/>
						</xsl:for-each>
						<xsl:copy-of select="./text()"/>
					</xsl:element>
	 			</xsl:when>
        		<xsl:otherwise>
        			<xsl:element name="{$str}">
						<xsl:for-each select="./*[local-name()='CATEGORIES']">
							<xsl:copy-of select="."/>
						</xsl:for-each>
						<xsl:copy-of select="./text()"/>
					</xsl:element>
        		</xsl:otherwise>
        	</xsl:choose>
</xsl:template>



 
 



</xsl:stylesheet>

