<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="xml" indent="no"/>

<xsl:template match="person[@gender = 'M']">
  <xsl:apply-templates select="." mode="man"/>
</xsl:template>

<xsl:template match="person[@gender = 'F']">
  <xsl:apply-templates select="." mode="woman"/>
</xsl:template>

<xsl:template match="person" mode="man">
  <man name="{name}">
    <sons>
      <xsl:apply-templates select="children/person[@gender = 'M']"
mode="man"/>
    </sons>
    <daughters>
      <xsl:apply-templates select="children/person[@gender = 'F']"
mode="woman"/>
    </daughters>
  </man>
</xsl:template>

<xsl:template match="person" mode="woman">
  <woman name="{name}">
    <sons>
      <xsl:apply-templates select="children/person[@gender = 'M']"
mode="man"/>
    </sons>
    <daughters>
      <xsl:apply-templates select="children/person[@gender = 'F']"
mode="woman"/>
    </daughters>
  </woman>
</xsl:template>

</xsl:stylesheet>

