<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
        doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" encoding="UTF-8"/>
    <xsl:preserve-space elements="*"/>
    <xsl:strip-space elements="note noteText"/>
    <xsl:template match="author"/>
    <xsl:template match="filename"/>
    <xsl:template match="encyclopedia"/>
    <xsl:template match="title" mode="xref">
        <xsl:variable name="temp">
            <xsl:text>a</xsl:text>
            <xsl:value-of select="../number"/>
            <xsl:text>_main.html</xsl:text>
        </xsl:variable>
        <a href="{$temp}" target="_blank">
            <xsl:apply-templates/>
        </a>
    </xsl:template>
    <xsl:template match="number" mode="xref"> (<xsl:apply-templates/>)</xsl:template>
    <xsl:template match="number"/>
    <xsl:template match="xref">
        <xsl:apply-templates select="title" mode="xref"/>
        <xsl:apply-templates select="number" mode="xref"/>
    </xsl:template>
    <xsl:template match="/">
        <html>
            <head>
                <title>Commentary</title>
                <meta http-equiv="Content-Type" content="application/xhtml+xml;charset=UTF-8"/>
                <link href="aa.css" rel="stylesheet" type="text/css"/>
            </head>
            <body>
                <h1>Cultural Commentary</h1>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="tale | story | poem">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="lg">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="p | intro | line | bookTitle | title">
        <xsl:apply-templates select="note | bookTitle"/>
    </xsl:template>
    <xsl:template match="note">
        <p>
            <xsl:apply-templates/>
        </p>
    </xsl:template>
    <xsl:template match="noteText/line">
        <xsl:apply-templates/>
        <xsl:if test="following-sibling::line"> / </xsl:if>
    </xsl:template>
    <xsl:template match="noteText">
        <xsl:variable name="temp">
            <xsl:text>comment</xsl:text>
            <xsl:number count="noteText" level="any"/>
        </xsl:variable>
        <xsl:variable name="temp1">
            <xsl:choose>
                <xsl:when test="/tale">
                    <xsl:text>a</xsl:text>
                    <xsl:value-of select="/tale/number"/>
                    <xsl:text>_text.html#comment</xsl:text>
                    <xsl:number count="noteText" level="any"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="/*/filename"/>
                    <xsl:text>_text.html#comment</xsl:text>
                    <xsl:number count="noteText" level="any"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <strong>
            <a name="{$temp}" href="{$temp1}" target="text">
                <sup>
                    <xsl:number count="noteText" level="any"/>
                </sup>
            </a>
            <xsl:text> </xsl:text>
            <xsl:apply-templates/>
            <xsl:text> </xsl:text>
        </strong>
    </xsl:template>
    <xsl:template match="noteExplanation">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="citation">
        <em>
            <xsl:apply-templates/>
        </em>
    </xsl:template>
    <xsl:template match="url">
        <a href="{.}" target="_blank"><xsl:value-of select="."/></a>
    </xsl:template>
    <xsl:template match="gloss">
        <xsl:text>‘</xsl:text>
        <xsl:apply-templates/>
        <xsl:text>’</xsl:text>
    </xsl:template>
    <xsl:template match="quote">
        <xsl:text>“</xsl:text>
        <xsl:apply-templates/>
        <xsl:text>”</xsl:text>
    </xsl:template>
    <xsl:template match="morphology">
        <em>
            <xsl:apply-templates/>
        </em>
    </xsl:template>
    <xsl:template match="emphasis">
        <em>
            <xsl:apply-templates/>
        </em>
    </xsl:template>
    <xsl:template match="articleTitle">
        <xsl:text>“</xsl:text>
        <xsl:apply-templates/>
        <xsl:text>”</xsl:text>
    </xsl:template>
    <xsl:template match="ling">
        <xsl:apply-templates select="lingText"/>
    </xsl:template>
    <xsl:template match="noteText/text()[position()=last()]">
        <xsl:choose>
            <xsl:when test="../@chop &gt;'0'">
                <xsl:variable name="temp">
                    <xsl:value-of select="."/>
                </xsl:variable>
                <xsl:variable name="temp1">
                    <xsl:value-of select="../@chop"/>
                </xsl:variable>
                <xsl:value-of select="substring($temp,1,string-length($temp)-$temp1)"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="."/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>
