<xsl:stylesheet
xmlns="http://www.loc.gov/METS/"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:xlink="http://www.w3.org/TR/xlink"
                                     version="1.0"
xmlns:date="http://exslt.org/dates-and-times"
extension-element-prefixes="date">

<xsl:import href="date.parse-date.template.xsl" />



<!-- 
Brian Tingle @ucop.edu   CDL Digital Object to METS XSLT

Copyright (c) 2002 The Regents of the University of California
Permission is hereby granted, without written agreement and without
license or royalty fees, to use, copy, modify, and distribute this XSLT
style sheet for any purpose, provided that the above copyright notice
and the following two paragraphs appear in all copies of this
document.

IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
ARISING OUT OF THE USE OF THIS COMPUTER FILE, EVEN IF THE
UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.

THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE COMPUTER
FILE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE
UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.  
-->

<xsl:output encoding="UTF-8"/>

<!-- matches everything,  leaving it marked up, 
 changing the namespace -->

<xsl:template match="*">
<xsl:element namespace="http://www.loc.gov/METS/" name="{name()}">
<xsl:for-each select="@*">
<xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>

<xsl:template match="*" mode="m2">
<xsl:element namespace="http://sunsite.berkeley.edu/MOA2/" name="{name()}">
<xsl:for-each select="@*">
<xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
</xsl:for-each>
<xsl:apply-templates mode="m2"/>
</xsl:element>
</xsl:template>

<!-- ArchObj to mets 
	** 	main template  **
-->
<xsl:template match="ArchObj">
<mets:mets 
xmlns:mets="http://www.loc.gov/METS/"
xmlns:moa2="http://sunsite.berkeley.edu/MOA2/"
xmlns:xlink="http://www.w3.org/TR/xlink" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.loc.gov/METS/ http://www.loc.gov/standards/mets/mets.xsd http://sunsite.berkeley.edu/MOA2/ http://sunsite.berkeley.edu/METS/moa2md.xsd" >
<xsl:for-each select="@*">
<xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
</xsl:for-each>
<xsl:apply-templates select="DescMD | AdminMD" />
<xsl:apply-templates select="/ArchObj/FileGrp//File" mode="FileXY"/>
<xsl:call-template name="fileSec"/>
<xsl:apply-templates select="StructMap"/>
</mets:mets>
</xsl:template>

<!-- descriptive metadata -->
<xsl:template match="DescMD">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="GDM">
<dmdSec ID="{@ID}">
<mdWrap MDTYPE="OTHER"><xmlData>
<xsl:element namespace="http://sunsite.berkeley.edu/MOA2/" name="GDM">
<xsl:for-each select="@*">
<xsl:if test="not(name()='ID')">
<xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
</xsl:if>
</xsl:for-each>
<xsl:apply-templates mode="m2"/>
</xsl:element>
</xmlData></mdWrap>
</dmdSec>
</xsl:template>

<xsl:template match="DMDRef">
<dmdSec>
<xsl:attribute name="ID">DMDRef<xsl:value-of select="position()"/>
</xsl:attribute>
<mdRef>
<xsl:attribute name="xlink:href"><xsl:value-of select="."/></xsl:attribute>
<xsl:for-each select="@*">
	<xsl:choose>
		<xsl:when test="(name() = 'DMDTYPE') and  (. = 'FINDAID') ">
<xsl:attribute name="MDTYPE">EAD</xsl:attribute>
		</xsl:when> 
		<xsl:when test="name() = 'DMDTYPE' ">
<xsl:attribute name="MDTYPE"><xsl:value-of select="."/></xsl:attribute>
		</xsl:when>
		<xsl:otherwise>
<xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
		</xsl:otherwise>
	</xsl:choose>
</xsl:for-each>
</mdRef>
</dmdSec>
</xsl:template>

<xsl:template match="DMD">
<xsl:apply-templates/>
</xsl:template>

<!-- fileSec -->
<!-- this named template makes up the mets:fileSec from moa2:FileGrp -->
<xsl:template name="fileSec">
<fileSec>
<xsl:apply-templates select="/ArchObj/FileGrp" mode="fileSec"/> 
</fileSec>
</xsl:template>

<!-- file templates -->

<xsl:template match="FileGrp | File | FLocat | FContent"> 
</xsl:template>

<xsl:template match="FContent" mode="fileSec">
<xsl:element name="{name()}">
<xsl:for-each select="@*">
<xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
</xsl:for-each>
<xsl:apply-templates select="*" mode="fileSec"/>
</xsl:element>
</xsl:template>

<xsl:template match="File" mode="FileXY">
<amdSec><techMD ID="xy-{@ID}"><mdWrap MDTYPE="OTHER"><xmlData>
<xsl:element namespace="http://sunsite.berkeley.edu/MOA2/" name="xy">
<xsl:for-each select=" @X | @Y | @UNIT | @USE">
<xsl:choose>
<xsl:when test="name()='ID'">
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:element>
</xmlData></mdWrap></techMD>
</amdSec>
</xsl:template>

<xsl:template match="File" mode="fileSec">
<file>
<xsl:for-each select="@*">
<xsl:choose>
	<xsl:when test="name()='X'"></xsl:when>
	<xsl:when test="name()='Y'"></xsl:when>
	<xsl:when test="name()='UNIT'"></xsl:when>
	<xsl:when test="name()='USE'"></xsl:when>
	<xsl:when test="name()='CREATED'">
<xsl:variable name="date"><xsl:value-of select="."/></xsl:variable>
		<xsl:attribute name="CREATED">
    <xsl:call-template name="date:parse-date">
      <xsl:with-param name="date-time" select="$date" />
      <xsl:with-param name="format" select="'M/d/yyyy'" />
      <xsl:with-param name="hour" select="0" />
    </xsl:call-template>
    <xsl:call-template name="date:parse-date">
      <xsl:with-param name="date-time" select="$date" />
      <xsl:with-param name="format" select="'MM/d/yyyy'" />
      <xsl:with-param name="hour" select="0" />
    </xsl:call-template>
		</xsl:attribute>
	</xsl:when>
	<xsl:when test="name()='ADMID'">
<xsl:attribute name="ADMID"><xsl:value-of select="."/> xy-<xsl:value-of select="../@ID"/></xsl:attribute></xsl:when>
	<xsl:otherwise>
<xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
	</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:apply-templates select="*" mode="fileSec"/>
</file>
</xsl:template>

<xsl:template match="FileGrp" mode="fileSec">
<xsl:element name="fileGrp" namespace="http://www.loc.gov/METS/">
<xsl:for-each select="@*">
<xsl:choose>
	<xsl:when test="name()='VERSDATE'">
<xsl:variable name="date"><xsl:value-of select="."/></xsl:variable>
        <xsl:attribute name="VERSDATE">
    <xsl:call-template name="date:parse-date">
      <xsl:with-param name="date-time" select="$date" />
      <xsl:with-param name="format" select="'M/d/yyyy'" />
      <xsl:with-param name="hour" select="0" />
    </xsl:call-template>
    <xsl:call-template name="date:parse-date">
      <xsl:with-param name="date-time" select="$date" />
      <xsl:with-param name="format" select="'MM/d/yyyy'" />
      <xsl:with-param name="hour" select="0" />
    </xsl:call-template>
        </xsl:attribute>
    </xsl:when>
	<xsl:otherwise>
<xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
	</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:apply-templates select="*" mode="fileSec"/>
</xsl:element>
</xsl:template>

<xsl:template match="FLocat" mode="fileSec">
<FLocat>
<xsl:attribute name="xlink:href"><xsl:value-of select="."/></xsl:attribute>
<xsl:for-each select="@*">
<xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
</xsl:for-each>
</FLocat>
</xsl:template>

<!-- AdminMD admSec -->
<xsl:template match="AdminMD">
<amdSec>
<xsl:for-each select="@*">
	<xsl:choose>
		<xsl:when test="name() = 'ID'" />
		<xsl:otherwise>
<xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
		</xsl:otherwise>
	</xsl:choose>
</xsl:for-each>
<xsl:apply-templates/>
</amdSec>
</xsl:template>

<xsl:template match="FileMgmt">
<techMD>
<xsl:attribute name="ID"><xsl:value-of select="../@ID"/></xsl:attribute>
<mdWrap MDTYPE="OTHER"><xmlData>
<xsl:element namespace="http://sunsite.berkeley.edu/MOA2/" name="FileMgmt">
<xsl:for-each select="@*">
<xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
</xsl:for-each>
<xsl:apply-templates mode="m2"/>
</xsl:element></xmlData></mdWrap></techMD>
</xsl:template>

<xsl:template match="Rights">
<rightsMD>
<xsl:attribute name="ID"><xsl:value-of select="../@ID"/></xsl:attribute>
<mdWrap MDTYPE="OTHER"><xmlData>
<xsl:element namespace="http://sunsite.berkeley.edu/MOA2/" name="Rights">
<xsl:for-each select="@*">
<xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
</xsl:for-each>
<xsl:apply-templates mode="m2"/>
</xsl:element></xmlData></mdWrap></rightsMD>
</xsl:template>

<xsl:template match="Source">
<digiprovMD>
<xsl:attribute name="ID"><xsl:value-of select="../@ID"/></xsl:attribute>
<mdWrap MDTYPE="OTHER"><xmlData>
<xsl:element namespace="http://sunsite.berkeley.edu/MOA2/" name="Source">
<xsl:for-each select="@*">
<xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
</xsl:for-each>
<xsl:apply-templates mode="m2"/>
</xsl:element></xmlData></mdWrap></digiprovMD>
</xsl:template>


<!-- structMap StructMap -->
<xsl:template match="StructMap">
<structMap>
<xsl:for-each select="@*">
<xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
</xsl:for-each>
<xsl:apply-templates/>
</structMap>
</xsl:template>

<xsl:template match="div">
<div>
<xsl:for-each select="@*">
<xsl:choose>
<xsl:when test="name() = 'N'">
<xsl:attribute name="ORDER"><xsl:value-of select="."/></xsl:attribute>
</xsl:when>
<xsl:when test="name() = 'DESCMD'">
<xsl:attribute name="DMDID"><xsl:value-of select="."/></xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:apply-templates/>
</div>
</xsl:template>

<xsl:template match="fptr">
<fptr>
<xsl:if test="@FILEID and not (@TAGID)">
    <xsl:attribute name="FILEID"><xsl:value-of select="@FILEID"/>
    </xsl:attribute>
</xsl:if>
<xsl:if test="@ID">
    <xsl:attribute name="ID"><xsl:value-of select="@FILEID"/>
    </xsl:attribute>
</xsl:if>
<xsl:if test="@TAGID">
    <area FILEID="{@FILEID}" BEGIN="{@TAGID}" BETYPE="IDREF"/>
</xsl:if>
</fptr>
</xsl:template>


</xsl:stylesheet>
