<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:d="http://www.dlese.org/Metadata/fields"
    exclude-result-prefixes="xsi d" 
    version="1.0">

<!--creation date: 2005-09-19-->
<!--author: Katy Ginger-->
<!--purpose: To transform fields files to OPML groups files-->

	<xsl:output method="xml" indent="yes" encoding="UTF-8"/>

	<xsl:template match="*|/">
		<xsl:element name="opml">

			<xsl:element name="head">
				<xsl:element name="title">
					<xsl:value-of select="d:metadataFieldInfo/d:field/@name"/>
				</xsl:element>
				<xsl:element name="concept">
					<xsl:attribute name="id">
						<xsl:value-of select="d:metadataFieldInfo/d:field/@id"/>
					</xsl:attribute>
					<xsl:attribute name="language">
						<xsl:value-of select="d:metadataFieldInfo/d:field/@language"/>
					</xsl:attribute>
					<xsl:attribute name="metaFormat">
						<xsl:value-of select="d:metadataFieldInfo/d:field/@metaFormat"/>
					</xsl:attribute>
					<xsl:attribute name="metaVersion">
						<xsl:value-of select="d:metadataFieldInfo/d:field/@metaVersion"/>
					</xsl:attribute>
					<xsl:attribute name="text">
						<xsl:value-of select="d:metadataFieldInfo/d:field/@name"/>
					</xsl:attribute>
					<xsl:attribute name="audience">none</xsl:attribute>
					<xsl:attribute name="path">
						<xsl:value-of select="d:metadataFieldInfo/d:field/@path"/>
					</xsl:attribute>
					<xsl:attribute name="deftn">
						<xsl:value-of select="d:metadataFieldInfo/d:field/d:definition"/>
					</xsl:attribute>
				</xsl:element><!--ends concept element-->
			</xsl:element> <!--ends head element-->

			<xsl:element name="body">
				<!--determine which outline element to write based on whether the terms element exists in the field file-->
				<!--if the terms element exists in the fields file, write the outline element with the attributes vocab, text, id, and deftn-->
				<!--if the terms element does not exist in the fields files, write the outline element with the attributes of text, abbrev and deftn-->
				<xsl:choose>
					<xsl:when test="//*[name()='terms']">
					<!--tests for the presence of the terms element in the fields file-->
						<xsl:apply-templates select="d:metadataFieldInfo/d:field/d:terms/d:termAndDeftn"/>
					</xsl:when>
					<xsl:otherwise>
					<!--assumes the terms element does not exist in the fields file-->
						<xsl:element name="outline">
							<xsl:attribute name="text">see concept</xsl:attribute>
						</xsl:element>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:element> <!--ends body element-->
			
		</xsl:element> <!--ends opml element-->
	</xsl:template>


<!--termAnd Deftn template-->
	<xsl:template match="d:termAndDeftn">
		<xsl:element name="outline">
			<xsl:attribute name="vocab">
				<xsl:value-of select="./@vocab"/>
			</xsl:attribute>

			<xsl:attribute name="id">
				<xsl:value-of select="./@id"/>
			</xsl:attribute>

			<xsl:attribute name="text">
	<!--		<xsl:apply-templates select="./@vocab" mode="parseString"/> -->
	<!-- the line above does not work because its an attribute???; so use call template instead-->
	
				<!--see the parseString template-->
				<xsl:call-template name="parseString"/> <!--see the parseString template-->
			</xsl:attribute>

			<xsl:attribute name="deftn">
				<xsl:value-of select="."/>
			</xsl:attribute>
		</xsl:element> 
	</xsl:template>

<!--parseString template-->
<!--assumes that words within the string are separated by colons-->
		<xsl:template match="node()" mode="parseString" name="parseString">
<!--note: in templates, params must be declared before variables-->
<!--note: for the with-param tag to work above, a param tag must exist and be present here in the template being called-->
		<xsl:param name="string" select="normalize-space(./@vocab)"/>
<!--		<xsl:param name="string" select="normalize-space(.)"/>-->
		<xsl:choose> 
			<xsl:when test="contains($string, ':')">
				<!--test to see if string contains a colon; if so, the string is a vocab term like: 'DLESE:Learning materials:Lesson plan'-->
				<!--peel off string part after first colon and then call the template recursively on the rest of the string-->
				<xsl:call-template name="parseString">
					<xsl:with-param name="string" select="normalize-space(substring-after($string, ':'))"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$string"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>


</xsl:stylesheet>
<!--	*** LICENSE INFORMATION *****
		Copyright 2002, 2003, 2004, 2005 DLESE Program Center
		University Corporation for Atmospheric Research (UCAR)
		P.O. Box 3000, Boulder, CO 80307, United States of America
		email: support@dlese.org. 
This XML stylesheet is free software; you can redistribute them and/or modify them under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.  These XML instance documents are distributed in the hope that they will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this project; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -->

