<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
    xpath-default-namespace="http://www.thewalters.org/ns/mss-manifest/1.0/">
    
    <xd:doc xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" scope="stylesheet">
        <xd:desc>
            <xd:p><xd:b>Created on:</xd:b> Jun 15, 2011</xd:p>
            <xd:p><xd:b>Author:</xd:b> Doug Emery</xd:p>
            <xd:p>XSL tranformation to convert a manuscript's metadata.xml file
                to an HTML file with image subject names, and paths to the
                manuscript images in manuscript order.</xd:p>
        </xd:desc>
    </xd:doc>
    
    <xsl:output method="html" indent="yes" encoding="UTF-8"
        doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
        doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
        media-type="text/html"/>
    
    <xsl:variable name="shelf_mark">
        <xsl:value-of select="*//dc:identifier" xmlns:dc="http://purl.org/dc/elements/1.1/"/>
    </xsl:variable>
    <xsl:variable name="path_shelf_mark">
        <xsl:value-of select="replace($shelf_mark,'\.','')"/>
    </xsl:variable>
    <xsl:variable name="image_path_base">
        <xsl:value-of select="concat('http://thedigitalwalters.org/Data/WaltersManuscripts/', $path_shelf_mark, '/data')"/>
    </xsl:variable>
    <xsl:variable name="no_dot_shelf_marks" select="tokenize('W.591 W.582 W.615 W.585 W.559 W.596 W.658 W.555 W.589 W.583', '\s+')"/>
    
    <xsl:template match="/">
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
            <head>
                <title>
                    <xsl:text>Image list for Walters MS. </xsl:text>
                    <xsl:value-of select="$shelf_mark"/>
                </title>
            </head>
            <body>
                <h1>
                    <xsl:text>Image list for Walters MS. </xsl:text>
                    <xsl:value-of select="$shelf_mark"/>
                </h1>
                <ul>
                    <xsl:for-each select="//node()[namespace-uri() = 'http://purl.org/dc/elements/1.1/']">
                        <li><xsl:value-of select="node-name(.)"/>: <xsl:value-of select="."/></li>
                    </xsl:for-each>
                </ul>
                <ol>
                    <xsl:for-each select="//image">
                        <xsl:variable name="thumb_path">
                            <xsl:value-of
                                select="replace(*//derivative_filename[following-sibling::derivative_class = 'thumb'], '\\', '/')"
                            />
                        </xsl:variable>
                        <li>
                            <xsl:choose>
                                <xsl:when test="matches(image_subject, '\d+[abrv]')">
                                    <xsl:text>Folio </xsl:text>
                                    <xsl:value-of select="image_subject"/>
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:value-of select="image_subject"/>
                                </xsl:otherwise>
                            </xsl:choose>
                            <br/>
                            <xsl:text>&#xA;</xsl:text>
                            <img>
                                <xsl:attribute name="src">
                                    <xsl:choose>
                                        <xsl:when test="index-of($no_dot_shelf_marks,$shelf_mark)">
                                            <xsl:value-of select="concat($image_path_base, '/', $path_shelf_mark, '/', $thumb_path)"/>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:value-of select="concat($image_path_base, '/', $shelf_mark, '/', $thumb_path)"/>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </xsl:attribute>
                                <xsl:attribute name="alt">
                                    <xsl:text>Thumbnail image of </xsl:text>
                                    <xsl:value-of select="$shelf_mark"/>
                                    <xsl:text>, </xsl:text>
                                    <xsl:value-of select="image_subject"/>
                                </xsl:attribute>
                            </img>
                            <xsl:text>&#xA;</xsl:text>
                            <ul>
                                <li>
                                    <xsl:call-template name="image_url">
                                        <xsl:with-param name="description" select="'Master image'"/>
                                        <xsl:with-param name="file_path"
                                            select="*//*[namespace-uri() = 'http://hul.harvard.edu/ois/xml/ns/jhove' and local-name() = 'repInfo']/@uri"/>

                                    </xsl:call-template>
                                </li>
                                <xsl:call-template name="find_image_url">
                                    <xsl:with-param name="image_type" select="'300'"/>
                                    <xsl:with-param name="description" select="'300 DPI TIFF image'"/>
                                </xsl:call-template>
                                <xsl:call-template name="find_image_url">
                                    <xsl:with-param name="image_type" select="'sap'"/>
                                    <xsl:with-param name="description" select="'Web format JPEG image'"/>
                                </xsl:call-template>
                                <xsl:call-template name="find_image_url">
                                    <xsl:with-param name="image_type" select="'thumb'"/>
                                    <xsl:with-param name="description" select="'Thumbnail format JPEG image'"/>
                                </xsl:call-template>
                            </ul>
                            <br/>
                        </li>
                    </xsl:for-each>
                </ol>
            </body>
        </html>
    </xsl:template>
    
    <xsl:template name="find_image_url" xmlns="http://www.w3.org/1999/xhtml">
        <xsl:param name="image_type"/>
        <xsl:param name="description"/>
        <xsl:if test="*//derivative_class = $image_type">
            <xsl:variable name="path" select="replace(*//derivative_filename[following-sibling::derivative_class = $image_type], '\\', '/')"></xsl:variable>
            <xsl:variable name="file_path">
                <xsl:choose>
                    <xsl:when test="index-of($no_dot_shelf_marks,$shelf_mark)">
                        <xsl:value-of select="concat($path_shelf_mark, '/', $path)"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="concat($shelf_mark, '/', $path)"/>
                    </xsl:otherwise>
                </xsl:choose>               
            </xsl:variable>
            <li>
                <xsl:call-template name="image_url">
                    <xsl:with-param name="description" select="$description"/>
                    <xsl:with-param name="file_path" select="$file_path"/>
                </xsl:call-template>
            </li>
        </xsl:if>
        
    </xsl:template>
    
    <xsl:template name="image_url" xmlns="http://www.w3.org/1999/xhtml">
        <xsl:param name="file_path"/>
        <xsl:param name="description"/>
        <xsl:variable name="full_path">
            <xsl:value-of select="concat($image_path_base, '/', $file_path)"/>
        </xsl:variable>
        <xsl:value-of select="$description"/><xsl:text>: </xsl:text>
        <a>
            <xsl:attribute name="href">
                <xsl:value-of select="$full_path"/>
            </xsl:attribute>
            <xsl:value-of select="$full_path"/>
        </a>
    </xsl:template>
</xsl:stylesheet>
