<?xml version="1.0" encoding="ISO-8859-1"?>
<!--*****************************************************************************************************************
Written by Giles Hogben,
Joint Research Center, Ispra Italy.
Note this Stylesheet uses 
****************************************************************************************************************-->
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
 xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="msxsl xs xsl"
 >
<xsl:key name="Categories" match="//*[local-name()='enumeration']" use="@value"/>


<!--***************************************************************************************************************** 
Remove duplicates and add overall categories type.
****************************************************************************************************************-->
<xsl:template match="/"> 
<xsl:variable name="elements" select="//*[@name and @name!='CATEGORY' and @name!='type']"/>
<schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3.org/2002/01/P3Pv1">	
<!--*************************Create Overall Categories Data Type************************************************-->
		<xsl:value-of select="count(//*[generate-id(.)=generate-id(key('Categories',local-name()))])"/>
		<simpleType name="allCategories">
	   		<restriction base="xs:string">
	  				<xsl:for-each select="//*[generate-id(.)=generate-id(key('Categories',local-name()))]">
			 			@br/~<enumeration>
								<xsl:attribute name="value">
									<xsl:value-of select="local-name()"/>
								</xsl:attribute>
						@br/~</enumeration>
	  				</xsl:for-each>
		 	</restriction>
		</simpleType>
		<xsl:for-each select="$elements">
			<xsl:variable name="nameStore" select="@name"/>
			<xsl:if test="generate-id(.)=generate-id(//*[@name=$nameStore])">
				@br/~<xsl:copy-of select="."/>@br/~
			</xsl:if>
		</xsl:for-each>
	</schema>
</xsl:template>


</xsl:stylesheet>