<?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(.)='DATA'">
               	<xsl:element name="datatype">
					<xsl:call-template name="StrSplit">
						<xsl:with-param name="str" select="substring-after(@ref,'#')"/>
					</xsl:call-template>
				</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="StrSplit">
        <xsl:param name="str" select="/.."/>			
        	<xsl:choose>
        		<xsl:when test="contains($str,'.')">
                	<xsl:element name="{substring-before($str,'.')}">
						<xsl:call-template name="StrSplit">
    	                    <xsl:with-param name="str" select="substring-after($str,'.')" />
        	        	</xsl:call-template>
        			</xsl:element>
				</xsl:when>
        		<xsl:otherwise>
					<xsl:element name="{$str}">
						<xsl:copy-of select="./*[local-name()='CATEGORIES']"/>
						<xsl:copy-of select="./text()"/>
					</xsl:element>
        		</xsl:otherwise>
        	</xsl:choose>
</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="*">
      <!-- remove element prefix (if any) -->
      <xsl:element name="{local-name()}">
        <!-- process attributes -->
        <xsl:for-each select="@*">
          <!-- remove attribute prefix (if any) -->
          <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>