<?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"
  xmlns:p3p="http://www.w3.org/2002/01/P3Pv1"
  exclude-result-prefixes="msxsl xs xsl p3p">


  
<!--***************************************************************************************************************** 
Main search and replace template
****************************************************************************************************************-->
<xsl:template match="@*|*">
  		<xsl:choose>
			<xsl:when test="local-name(.)='datatype'">
               <xsl:element name="data">
					<xsl:attribute name="ref">
						<xsl:variable name="tempStr">#<xsl:call-template name="StrConcat">
								<xsl:with-param name="dot"/>
							</xsl:call-template>
						</xsl:variable>
						<xsl:value-of select="$tempStr"/>
					</xsl:attribute>
					<xsl:copy-of select=".//*[local-name(.)='CATEGORIES']"/>
				</xsl:element>
			</xsl:when>
			<xsl:otherwise>
				<xsl:copy>
					<xsl:apply-templates select="@* | * | node()"/>
				</xsl:copy>
			</xsl:otherwise>
		</xsl:choose>
</xsl:template>


<!--***************************************************************************************************************** 
Split the ref attributes into nested XML elements and add the categories and text() nodes at the bottom.
****************************************************************************************************************-->
 <xsl:template name="StrConcat">
	 <xsl:param name="dot" select="/.."/>
	 <xsl:for-each select="./*">
		<xsl:if test="local-name()!='CATEGORIES'">
	 		<xsl:value-of select="$dot"/>
			<xsl:value-of select="local-name()"/>
			<xsl:call-template name="StrConcat">
				<xsl:with-param name="dot">.</xsl:with-param>
			</xsl:call-template>
    	</xsl:if>
	</xsl:for-each>
</xsl:template>

<!--
Note - to remove extraneous namespace prefixes, you can use the following but I have not done this so as to avoid a multi-pass process

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="*">
      <xsl:element name="{local-name()}">
        <xsl:for-each select="@*">
          <xsl:attribute name="{local-name()}">
            <xsl:value-of select="."/>
          </xsl:attribute>
        </xsl:for-each>
        <xsl:apply-templates/>
      </xsl:element>
  </xsl:template>
</xsl:stylesheet>

 -->


</xsl:stylesheet>
