<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html"/>
    <xsl:template match="/"><!-- apply at top level of xml doc -->
        <html>
            <head>
                <link rel="stylesheet" href="photos.css" type="text/css"/>
                <title>Photo Album</title>
            </head>
            <body>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template><!-- =================  Document Root  ================= -->
    <xsl:template match="photoList">
        <table border="1">
            <tr>
                <th>Description</th>
                <th>Location</th>
                <th>Date</th>
                <th>Subjects (left to right)</th>
            </tr>
            <xsl:apply-templates select="photo">
                <xsl:sort select="dateInferred"/>
            </xsl:apply-templates>
        </table>
    </xsl:template>
    <xsl:template match="photo">
        <tr>
            <td>
                <xsl:element name="a">
                    <xsl:attribute name="href">
                        <xsl:text>photo.xql?id=</xsl:text>
                        <xsl:value-of select="id"/>
                    </xsl:attribute>
                    <xsl:value-of select="description"/>
                </xsl:element>
            </td>
            <td>
                <xsl:choose>
                    <xsl:when test="place/long">
                        <xsl:element name="a">
                            <xsl:attribute name="href">
                                <xsl:text>http://www.multimap.com/map/browse.cgi?client=public&amp;scale=5000&amp;lon=</xsl:text>
                                <xsl:value-of select="place/long"/>
                                <xsl:text>&amp;lat=</xsl:text>
                                <xsl:value-of select="place/lat"/>
                            </xsl:attribute>
                            <xsl:value-of select="place/address"/>
                        </xsl:element>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="place/address"/>
                    </xsl:otherwise>
                </xsl:choose>
            </td>
            <td>
                <xsl:choose>
                    <xsl:when test="dateInferred">
                        <xsl:element name="a">
                            <xsl:attribute name="href">
                                <xsl:text>http://www.wikipedia.org/wiki/</xsl:text>
                                <xsl:value-of select="substring(dateInferred,1,4)"/>
                            </xsl:attribute>
                            <xsl:value-of select="dateInferred"/>
                        </xsl:element>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="date"/>
                    </xsl:otherwise>
                </xsl:choose>
            </td>
            <td>
                <xsl:for-each select="subject">
                    <xsl:value-of select="person"/>
                    <xsl:if test="ageInferred">
       (<xsl:value-of select="ageInferred"/>) 
     </xsl:if>
                    <br/>
                </xsl:for-each>
            </td>
        </tr>
    </xsl:template>
</xsl:stylesheet>
