<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="2.0"  xmlns:tei="http://www.tei-c.org/ns/1.0">
    
    <xsl:output method="text"/>
    <xsl:output method="html" indent="yes" name="html"/>
    
    <xsl:template match="/">
        
        <xsl:for-each select="//tei:front">
            <xsl:result-document href="html pages/index.html" format="html">
                <html xmlns="http://www.w3.org/1999/xhtml">
                    <head>
                        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
                        <style type="text/css" media="screen">
                            @import url(style1.css );
                        </style>
                        <title><xsl:value-of select="//tei:fileDesc/tei:titleStmt/tei:title"></xsl:value-of></title>
                    </head>
                    <body>
                        <div class="search">
                            <!-- Google CSE Search Box Begins  -->
                            <form action="http://www.google.com/cse" id="searchbox_010943083280240020850:mdghz7ljls4">
                                <input type="hidden" name="cx" value="010943083280240020850:mdghz7ljls4" />
                                <input type="text" name="q" size="25" />
                                <input type="submit" name="sa" value="Search" />
                            </form>
                            <script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=searchbox_010943083280240020850%3Amdghz7ljls4&amp;lang=en"></script>
                            <!-- Google CSE Search Box Ends -->
                        </div>
                    <xsl:for-each select="tei:titlePage">
                        <xsl:apply-templates></xsl:apply-templates>
                    </xsl:for-each>
                        <xsl:for-each select="//tei:availability/tei:p">
                            <p>
                                <xsl:value-of select="."/>
                            </p>
                        </xsl:for-each>
                        
                     <xsl:for-each select="tei:div1[@type='contents']">
                         <xsl:apply-templates></xsl:apply-templates>
                     </xsl:for-each>
                </body>
                </html>
            </xsl:result-document>
        </xsl:for-each>
        
        <xsl:for-each select="//tei:div1[@type!='contents']">
            <xsl:variable name="filename"
                select="concat('html pages/',@n,'.html')" />
            
            <xsl:result-document href="{$filename}" format="html">
                <html xmlns="http://www.w3.org/1999/xhtml">
                    <head>
                        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
                        <style type="text/css" media="screen">
                            @import url(style1.css );
                        </style>
                        <title><xsl:value-of select="tei:head"></xsl:value-of></title>
                    </head>
                    <body>
                        <div class="nav">
                            <xsl:if test="./@type!='preface'">
                                &lt;&lt;&#160;<a><xsl:attribute name="href" select="concat(preceding::tei:div1[1]/@n, '.html')"></xsl:attribute>Previous Chapter</a>&#160;-&#160;
                            </xsl:if>
                            <a href="index.html">Home</a>
                            <xsl:if test="./@type!='index'">
                                &#160;-&#160;<a><xsl:attribute name="href" select="concat(following::tei:div1[1]/@n, '.html')"></xsl:attribute>Next Chapter</a>&#160;&gt;&gt;
                        </xsl:if>
                        </div>
                        <div class="search">
                            <!-- Google CSE Search Box Begins  -->
                            <form action="http://www.google.com/cse" id="searchbox_010943083280240020850:mdghz7ljls4">
                                <input type="hidden" name="cx" value="010943083280240020850:mdghz7ljls4" />
                                <input type="text" name="q" size="25" />
                                <input type="submit" name="sa" value="Search" />
                            </form>
                            <script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=searchbox_010943083280240020850%3Amdghz7ljls4&amp;lang=en"></script>
                            <!-- Google CSE Search Box Ends -->
                        </div>
                    <xsl:apply-templates></xsl:apply-templates>
                        <div class="nav">
                            <xsl:if test="./@type!='preface'">
                                &lt;&lt;&#160;<a><xsl:attribute name="href" select="concat(preceding::tei:div1[1]/@n, '.html')"></xsl:attribute>Previous Chapter</a>&#160;-&#160;
                            </xsl:if>
                            <a href="index.html">Home</a>
                            <xsl:if test="./@type!='index'">
                                &#160;-&#160;<a><xsl:attribute name="href" select="concat(following::tei:div1[1]/@n, '.html')"></xsl:attribute>Next Chapter</a>&#160;&gt;&gt;
                            </xsl:if>
                        </div>
                    </body>
                </html>
            </xsl:result-document>
        </xsl:for-each>
        
    </xsl:template>
    
    <xsl:template match="tei:teiHeader"/>
    
    <xsl:template match="tei:titlePage">
        <div>
            <a class="pageimage">
                <xsl:attribute name="id">
                    <xsl:value-of select="attribute::xml:id"/>
                </xsl:attribute>
                <xsl:attribute name="href">images/<xsl:value-of select="attribute::facs"
                /></xsl:attribute>[view page image]</a>
            <xsl:apply-templates/>
            <xsl:for-each select="//tei:availability/tei:p">
                <p>
                    <xsl:value-of select="."/>
                </p>
            </xsl:for-each>
        </div>
    </xsl:template>
    
    <xsl:template match="tei:titlePart[@type='main']">
        <h1>
            <xsl:value-of select="."/>
        </h1>
    </xsl:template>
    
    <xsl:template match="tei:titlePart[@type='sub']">
        <h2>
            <xsl:value-of select="."/>
        </h2>
    </xsl:template>
    
    <xsl:template match="tei:byline | tei:docImprint">
        <p>
            <xsl:apply-templates/>
        </p>
    </xsl:template>
    
    <xsl:template match="tei:div1/tei:head">
        <h2>
            <xsl:if test="@xml:id">
                <xsl:attribute name="id">
                    <xsl:value-of select="attribute::xml:id"/>
                </xsl:attribute>
            </xsl:if>            
            <xsl:value-of select="."/>
        </h2>
        <xsl:if test="@facs">
            <a class="pageimage">
                <xsl:attribute name="href">images/<xsl:value-of select="attribute::facs"
                /></xsl:attribute>[view page image]</a>
        </xsl:if>
    </xsl:template>
    
    <xsl:template match="tei:div2/tei:head">
        <h2>
            <xsl:if test="@xml:id">
                <xsl:attribute name="id">
                    <xsl:value-of select="attribute::xml:id"/>
                </xsl:attribute>
            </xsl:if>
            
            <xsl:value-of select="."/>
        </h2>
        <xsl:if test="@facs">
            <a class="pageimage">
                <xsl:attribute name="href">images/<xsl:value-of select="attribute::facs"
                /></xsl:attribute>[view page image]</a>
        </xsl:if>
    </xsl:template>
    
    
    
    <xsl:template match="tei:p">
        <p>
            <xsl:apply-templates/>
        </p>
    </xsl:template>
    
    <xsl:template match="tei:div1">
        <div>
            <xsl:apply-templates/>
        </div>
    </xsl:template>
    
    <xsl:template match="tei:div2">
        <div>
            <xsl:if test="@xml:id">
            <xsl:attribute name="id">
                <xsl:value-of select="attribute::xml:id"/>
            </xsl:attribute>
            </xsl:if>
            <xsl:if test="@facs">
                <a class="pageimage">
                    <xsl:attribute name="href">images/<xsl:value-of select="attribute::facs"
                    /></xsl:attribute>[view page image]</a>
            </xsl:if>
            
            <xsl:apply-templates/>
        </div>
    </xsl:template>
    
    <xsl:template match="tei:pb">
        <a class="pageimage">
            <xsl:attribute name="id">
                <xsl:value-of select="attribute::xml:id"/>
            </xsl:attribute>
            <xsl:if test="@facs">
                <xsl:attribute name="href">images/<xsl:value-of select="attribute::facs"
                /></xsl:attribute>[view page image] </xsl:if>
        </a>
    </xsl:template>
    
    <xsl:template match="tei:div1[@type='contents']//tei:ref">
        <a>
            <xsl:variable name="currenttarget">
                <xsl:value-of select="./@target"></xsl:value-of>
            </xsl:variable>
            <xsl:variable name="url" select="concat(id($currenttarget)/ancestor::tei:div1/@n, '.html')"></xsl:variable>
            <xsl:attribute name="href">
                <xsl:value-of select="$url"></xsl:value-of>
            </xsl:attribute>
            <xsl:value-of select="."/>
        </a>
    </xsl:template>
    
    <xsl:template match="tei:div1[@type='index']//tei:ref">
        <xsl:variable name="currenttarget">
            <xsl:value-of select="./@target"></xsl:value-of>
        </xsl:variable>
        <xsl:variable name="url" select="concat(id($currenttarget)/ancestor::tei:div1/@n, '.html#', $currenttarget)"></xsl:variable>
        <a>
            <xsl:attribute name="href">
                <xsl:value-of select="$url"></xsl:value-of>
            </xsl:attribute>
            <xsl:value-of select="."/>
        </a>
    </xsl:template>
    
    <xsl:template match="tei:list">
        <h3><xsl:value-of select="tei:head"/></h3>
        <ul>
            <xsl:for-each select="tei:item">
                <li><xsl:apply-templates/></li>
            </xsl:for-each>
        </ul>
    </xsl:template>
    
    <xsl:template match="tei:quote">
        <blockquote>
            <xsl:apply-templates/>
        </blockquote>
    </xsl:template>
    
    <xsl:template match="tei:supplied"> [<xsl:apply-templates/>] </xsl:template>
    
    <xsl:template match="tei:gap">
        <p> [There is a gap of <xsl:value-of select="@extent"/>&#160;<xsl:value-of select="@unit"/> because <xsl:value-of select="@reason"/>.] </p>
    </xsl:template>
    
</xsl:stylesheet>