<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">
    <html>
      <head>
        <title>Family Tree</title>
      </head>
      <body>
        <xsl:apply-templates select="FAMILY_TREE"/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="FAMILY_TREE">
  
    <h1>Family Tree</h1>

    <h2>Families</h2>
    <xsl:apply-templates select="FAMILY"/>
    
    <h2>People</h2>
    <xsl:apply-templates select="PERSON"/>

    <h2>Sources</h2>
    <ul>
     <xsl:apply-templates select="SOURCE"/>
    </ul>
      
  </xsl:template>

  <xsl:template match="PERSON">
  
    <h3>
      <xsl:element name="a">
        <xsl:attribute name="name">
          <xsl:value-of select="@ID"/>
        </xsl:attribute>
      <xsl:value-of select="NAME"/>
      </xsl:element>
    </h3>
    
    <ul>
      <xsl:if test="BIRTH">
        <li>Born: <xsl:value-of select="BIRTH"/></li>
      </xsl:if>
      <xsl:if test="DEATH">
        <li>Died: <xsl:value-of select="DEATH"/></li>
      </xsl:if>
      <xsl:if test="BAPTISM">
        <li>Baptism: <xsl:value-of select="BAPTISM"/></li>
      </xsl:if>
      <xsl:if test="BURIAL">
        <li>Burial: <xsl:value-of select="BURIAL"/></li>
      </xsl:if>
      <xsl:apply-templates select="FATHER"/>
      <xsl:apply-templates select="MOTHER"/>
    </ul>
    
    <p>
      <xsl:apply-templates select="NOTE"/>
    </p>
      
  </xsl:template>

  <xsl:template match="FATHER">
    <li> 
      <xsl:element name="a">
        <xsl:attribute name="href">
          #<xsl:value-of select="@PERSON"/>
        </xsl:attribute>
        Father
      </xsl:element>
    </li>
  </xsl:template>

  <xsl:template match="MOTHER">
    <li> 
      <xsl:element name="a">
        <xsl:attribute name="href">
          #<xsl:value-of select="@PERSON"/>
        </xsl:attribute>
        Mother
      </xsl:element>
    </li>
  </xsl:template>

  <xsl:template match="body | body//*">
    <xsl:copy>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="SOURCE">
  
    <li>
      <xsl:element name="a">
        <xsl:attribute name="name">
          <xsl:value-of select="@ID"/>
        </xsl:attribute>
        <xsl:value-of select="."/>
      </xsl:element>
    </li>
      
  </xsl:template>

  <xsl:template match="FAMILY">
    <ul>
      <xsl:apply-templates select="HUSBAND"/>
      <xsl:apply-templates select="WIFE"/>
      <xsl:apply-templates select="CHILD"/>
    </ul>
  </xsl:template>

  <xsl:template match="HUSBAND">
    <li>Husband: <a href="#{@PERSON}">
      <xsl:value-of select="id(@PERSON)/NAME"/>
    </a></li>
  </xsl:template>

  <xsl:template match="WIFE">
    <li>Wife: <a href="#{@PERSON}">
      <xsl:value-of select="id(@PERSON)/NAME"/>
    </a></li>
  </xsl:template>

  <xsl:template match="CHILD">
    <li>Child: <a href="#{@PERSON}">
      <xsl:value-of select="id(@PERSON)/NAME"/>
    </a></li>
  </xsl:template>

</xsl:stylesheet>
