<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="
http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"
doctype-public="+//ISBN 1-931666-00-8//DTD ead.dtd (Encoded Archival Description (EAD) Version 2002)//EN"
doctype-system="ead.dtd" indent="yes"/>
<xsl:template match="* | processing-instruction() | comment()">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:key name="kNodeByPrecedingBlock" match="unittitle/node()[not(self::unitdate)]"
use="generate-id((..|preceding-sibling::*[self::unitdate][1])[last()])"/>
<xsl:template match="did">
<did>
<xsl:apply-templates select="node()|@*"/>
</did>
</xsl:template>
<xsl:template match="unittitle">
<xsl:apply-templates select=".|unitdate" mode="group"/>
</xsl:template>
<xsl:template match="unitdate">
<xsl:variable name="normal" select="@normal"/>
<xsl:variable name="type" select="@type"/>
<xsl:variable name="label" select="@label"/>
<xsl:variable name="analog" select="@encodinganalog"/>
<unitdate>
<xsl:if test="$label != ''">
<xsl:attribute name="label">
<xsl:value-of select="$label"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="$analog != ''">
<xsl:attribute name="encodinganalog">
<xsl:value-of select="$analog"/>
</xsl:attribute>
</xsl:if>
<xsl:attribute name="era">ce</xsl:attribute>
<xsl:attribute name="calendar">gregorian</xsl:attribute>
<xsl:if test="$normal != ''">
<xsl:attribute name="normal">
<xsl:value-of select="$normal"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="$type != ''">
<xsl:attribute name="type">
<xsl:value-of select="$type"/>
</xsl:attribute>
</xsl:if>
<xsl:apply-templates/>
</unitdate>
</xsl:template>
<xsl:template match="unittitle" mode="group" name="makeUnittitle">
<xsl:variable name="vGroup" select="key('kNodeByPrecedingBlock',generate-id())"/>
<xsl:variable name="label" select="@label"/>
<xsl:variable name="analog" select="@encodinganalog"/>
<xsl:if test="$vGroup">
<unittitle>
<xsl:if test="$label != ''">
<xsl:attribute name="label">
<xsl:value-of select="$label"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="$analog != ''">
<xsl:attribute name="encodinganalog">
<xsl:value-of select="$analog"/>
</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="$vGroup"/>
</unittitle>
</xsl:if>
</xsl:template>
<xsl:template match="*" mode="group">
<xsl:apply-templates select="."/>
<xsl:call-template name="makeUnittitle"/>
</xsl:template>
<xsl:template match="*/text()[normalize-space()]">
<xsl:value-of select="normalize-space()"/>
</xsl:template>
<xsl:template match="*/text()[not(normalize-space())]"/>
</xsl:stylesheet>