<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:output indent="yes" method="xml"/>
    <xsl:template match="gedCom">
        <GEDCOM>
            <xsl:apply-templates select="HEAD"/>
            <xsl:apply-templates select="FAM"/>
            <xsl:apply-templates select="INDI"/>
            <xsl:apply-templates select="INDI/BIRT"/>
            <xsl:apply-templates select="INDI/DEAT"/>
            <xsl:apply-templates select="FAM/MARR"/>
        </GEDCOM>
    </xsl:template>
    
    <!--ignore the submission record.  I don't need this information-->
    <xsl:template match="SUBM"/>
    
    <!--This is required by the DTD-->
    <xsl:template match="HEAD">
        <HeaderRec>
            <FileCreation Date=""/>
            <Submitter>
                <Link Ref="" Target=""/>
            </Submitter>
        </HeaderRec>
    </xsl:template>
    
    <xsl:template match="FAM">
        <FamilyRec Id="{normalize-space(text())}">
            
            <!--Each family should of course only have one husband and one wife, 
                but I couldn't figure out how to use an if statement instead of for-each -->
            <xsl:for-each select="HUSB">
                <HusbFath>
                    <Link Target="IndividualRec" Ref="{normalize-space(text())}"/>
                </HusbFath>
            </xsl:for-each>
            
            <xsl:for-each select="WIFE">
                <WifeMoth>
                    <Link Target="IndividualRec" Ref="{normalize-space(text())}" />
                </WifeMoth>
            </xsl:for-each>
            
            <xsl:for-each select="CHIL">
                <Child>
                    <Link Ref="{normalize-space(text())}" Target="IndividualRec"/>
                </Child>
            </xsl:for-each>
        </FamilyRec>
    </xsl:template>
    
    <xsl:template match="INDI">
        <IndividualRec Id="{normalize-space(text())}">
            <IndivName><xsl:value-of select="normalize-space(NAME)"/></IndivName>
            <Gender><xsl:value-of select="normalize-space(SEX)"/></Gender>
            <DeathStatus></DeathStatus>
            <Note><xsl:value-of select="normalize-space(NOTE)"/></Note>
        </IndividualRec>
    </xsl:template>
    
   <xsl:template match="BIRT">
        <EventRec Id="B{normalize-space(parent::node()/text())}" Type="birth" VitalType="birth">
          
            <!--<Participant>
                <Link Target="IndividualRec" Ref="{normalize-space()}"  />
                <Role>father</Role>
            </Participant>-->
            
            <!--<Participant>
                <Link Target="IndividualRec" Ref="{normalize-space()}"  />
                <Role>mother</Role>
            </Participant>
            -->
                
            <Participant>
                <Link Target="IndividualRec" Ref="{normalize-space(parent::node()/text())}"  />
                <Role>child</Role>
            </Participant>
            
            <Date Calendar="Julian">
                <xsl:value-of select="normalize-space(DATE)"/>
            </Date>
            <Place>
                <PlaceName>
                    <xsl:value-of select="normalize-space(PLAC)"/>
                   
                </PlaceName>
            </Place>
        </EventRec>
    </xsl:template>
    
   <xsl:template match="MARR">
        <EventRec Id="M{normalize-space(parent::node()/text())}" Type="marriage" VitalType="marriage">
            <xsl:for-each select="parent::node()/HUSB">
                <Participant>
                    <Link Target="IndividualRec" Ref="{normalize-space(parent::node()/HUSB)}"  />
                    <Role>husband</Role>
                </Participant>
            </xsl:for-each>
            
            <xsl:for-each select="parent::node()/WIFE">
                <Participant>
                    <Link Target="IndividualRec" Ref="{normalize-space(parent::node()/WIFE)}"  />
                    <Role>wife</Role>
                </Participant>
            </xsl:for-each>
            
            <Date Calendar="Julian">
                <xsl:value-of select="normalize-space(DATE)"/>
            </Date>
            
            <Place>
                <PlaceName>
                    <xsl:value-of select="normalize-space(PLAC)"/>
                </PlaceName>
            </Place>
            
        </EventRec>
    </xsl:template>
    
    <!--<xsl:template match="DIV">
    </xsl:template> -->
    
    <xsl:template match="DEAT">
        <EventRec Id="D{normalize-space(parent::node()/text())}" Type="death" VitalType="death">
            <Participant>
                <Link Target="IndividualRec" Ref="{normalize-space(parent::node()/text())}"  />
            </Participant>
            
            <Date Calendar="Julian">
                <xsl:value-of select="normalize-space(DATE)"/>
            </Date>
            <Place>
                <PlaceName>
                    <xsl:value-of select="normalize-space(PLAC)"/>
                </PlaceName>
            </Place>
        </EventRec>
    </xsl:template>
    
</xsl:stylesheet>
