<?xml version="1.0" encoding="UTF-8"?>
<!--
    Mangled/Copied from xslt example found at http://www.opensearch.org/Documentation/Stylesheet

    I removed everything that didn't deal with atom but kept the cool url and people templates

    Original authors:
        Copyright (c) 2005-2006 A9.com, Inc. or its affiliates.
        Author: Michael Fagan, parts by Joel Tesler
 -->
<xsl:stylesheet version="1.0"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:xhtml="http://www.w3.org/1999/xhtml"
    exclude-result-prefixes="xsl atom">

    <xsl:output method="html" encoding="UTF-8"/>
    
    <xsl:variable name="t-entrylink">view full entry</xsl:variable>
    <xsl:variable name="t-categories">categories:</xsl:variable>

    <xsl:template match="/">
        <body>
        <div>
            <xsl:apply-templates />
        </div>
        </body>
    </xsl:template>

    <xsl:template match="atom:entry">
        <xsl:variable name="url">
            <xsl:call-template name="resolvelink">
                <xsl:with-param name="url" select="(atom:link[not(@rel) or @rel='alternate' or @rel='']/@href | link | guid[@isPermaLink='true'])[1]" />
            </xsl:call-template>
        </xsl:variable>

        <!-- item title -->
        <a href="{$url}">
            <xsl:call-template name="showtext">
                <xsl:with-param name="node" select="(atom:title | title)[1]" />
            </xsl:call-template>
        </a>

        <!-- item authors -->
        <xsl:if test="(author | atom:author | atom:contributor)">
            <xsl:text> by </xsl:text>
            <xsl:apply-templates select="author | atom:author | atom:contributor" />
        </xsl:if>

        <!-- item description -->
        <xsl:if test="atom:content[not(@type) or @type='' or @type='text' or @type='html' or @type='xhtml'] | description | atom:summary[not(@type) or @type='' or @type='text' or @type='html' or @type='xhtml']">
            <p class="desc">
                <xsl:choose>
                    <xsl:when test="atom:content[(not(@type) or @type='' or @type='text' or @type='html' or @type='xhtml') and not(@src)]">
                        <xsl:call-template name="showtext">
                            <xsl:with-param name="node" select="atom:content[(not(@type) or @type='' or @type='text' or @type='html' or @type='xhtml') and not(@src)]" />
                        </xsl:call-template>
                    </xsl:when>
                    <xsl:when test="description | atom:summary[not(@type) or @type='' or @type='text' or @type='html' or @type='xhtml']">
                        <xsl:call-template name="showtext">
                            <xsl:with-param name="node" select="description | atom:summary[not(@type) or @type='' or @type='text' or @type='html' or @type='xhtml']" />
                        </xsl:call-template>
                    </xsl:when>
                </xsl:choose>

                <xsl:if test="atom:content/@src">
                    <xsl:if test="atom:summary"><br /></xsl:if>
                    <a>
                        <xsl:attribute name="href">
                            <xsl:call-template name="resolvelink">
                                <xsl:with-param name="url" select="atom:content/@src" />
                            </xsl:call-template>
                        </xsl:attribute>
                        <xsl:text>view full entry</xsl:text>
                    </a>
                </xsl:if>
            </p>
        </xsl:if>
        <!-- item categories -->
        <xsl:if test="(atom:category | category)">
            <p><xsl:text>categories: </xsl:text><xsl:apply-templates select="atom:category | category" /></p>
        </xsl:if>
        <!-- item source -->
        <xsl:variable name="maybesourceurl" select="(atom:source/link[@rel='alternate']/@href | source/@url)[1]" />
        <xsl:variable name="sourceurl">
            <xsl:if test="$maybesourceurl">
                <xsl:call-template name="resolvelink">
                    <xsl:with-param name="url" select="$maybesourceurl" />
                </xsl:call-template>
            </xsl:if>
        </xsl:variable>
        <xsl:variable name="maybesourcename">
            <xsl:choose>
                <xsl:when test="atom:source/title[not(@type) or @type='' or @type='text' or @type='html' or @type='xhtml']">
                    <xsl:value-of select="atom:source/title[@type='' or @type='text' or @type='html' or @type='xhtml'][1]" />
                </xsl:when>
                <xsl:when test="string-length(source)&gt;0">
                    <xsl:value-of select="source[1]" />
                </xsl:when>
            </xsl:choose>
        </xsl:variable>
        <xsl:if test="string-length($sourceurl)&gt;0 or string-length($maybesourcename)&gt;0">
            <p><xsl:text>from </xsl:text>
                <xsl:variable name="sourcename">
                    <xsl:choose>
                        <xsl:when test="$maybesourcename">
                            <xsl:value-of select="$maybesourcename" />
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="$sourceurl" />
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:choose>
                    <xsl:when test="$sourceurl"><a href="{$sourceurl}"><xsl:value-of select="$sourcename" /></a></xsl:when>
                    <xsl:otherwise><xsl:value-of select="$sourcename" /></xsl:otherwise>
                </xsl:choose>
            </p>
        </xsl:if>
        <!-- item enclosure -->
        <xsl:if test="(atom:link[@rel='enclosure']/@href | enclosure/@url)">
            <xsl:apply-templates select="atom:link[@rel='enclosure'] | enclosure" />
        </xsl:if>
        <!-- item rights -->
        <xsl:variable name="itemrights" select="atom:rights[not(@type) or @type='' or @type='text' or @type='html' or @type='xhtml'][1]" />
        <!-- item license -->
        <xsl:if test="(atom:link[@rel='license']/@href | license/@url)">
            <p><xsl:apply-templates select="atom:link[@rel='license'] | license" /></p>
        </xsl:if>
    </xsl:template>

    <xsl:template match="atom:link[@rel='enclosure'] | enclosure">
        <xsl:variable name="encurl">
            <xsl:call-template name="resolvelink">
                <xsl:with-param name="url" select="(@href | @url)[1]" />
            </xsl:call-template>
        </xsl:variable>
        <p><a href="{$encurl}">
                <xsl:choose>
                    <xsl:when test="@title">
                        <xsl:value-of select="@title" />
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:text>enclosure</xsl:text>
                    </xsl:otherwise>
                </xsl:choose>
            </a>
            <xsl:if test="@type"> (<xsl:value-of select="@type" />)
            </xsl:if>
        </p>
    </xsl:template>

    <xsl:template match="atom:link[@rel='license'] | license">
        <xsl:variable name="licenseurl">
            <xsl:call-template name="resolvelink">
                <xsl:with-param name="url" select="(@href | @url)[1]" />
            </xsl:call-template>
        </xsl:variable>
        <a href="{$licenseurl}"><xsl:value-of select="/atom:entry/atom:rights" /></a>
    </xsl:template>

    <xsl:template match="atom:category | category">
        <xsl:variable name="name">
            <xsl:choose>
                <xsl:when test="not(namespace-uri())"><xsl:value-of select="." /></xsl:when>
                <xsl:when test="@label"><xsl:value-of select="@label" /></xsl:when>
                <xsl:when test="@term"><xsl:value-of select="@term" /></xsl:when>
            </xsl:choose>
        </xsl:variable>

        <xsl:if test="string-length($name)&gt;0">
            <xsl:variable name="category">
                <xsl:choose>
                    <xsl:when test="not(namespace-uri())"><xsl:value-of select="." /></xsl:when>
                    <xsl:otherwise><xsl:value-of select="@term" /></xsl:otherwise>
                </xsl:choose>
            </xsl:variable>
            <xsl:variable name="maybeurl" select="(@domain | @scheme)[1]" />
            <xsl:variable name="url">

                <xsl:if test="starts-with($maybeurl, 'http')">
                    <xsl:value-of select="concat($maybeurl, '#', $category)" />
                </xsl:if>
            </xsl:variable>
            <xsl:choose>
                <xsl:when test="string-length($url)&gt;0"><a href="{$url}"><xsl:value-of select="$name" /></a></xsl:when>
                <xsl:otherwise><xsl:value-of select="$name" /></xsl:otherwise>
            </xsl:choose>
            <xsl:text>; </xsl:text>

        </xsl:if>
    </xsl:template>

    <!-- outputs a 'person' (next 4 templates) -->
    <xsl:template match="author">
        <xsl:call-template name="person">
            <xsl:with-param name="email" select="substring-before(concat(normalize-space(.),' '), ' ')" />
            <xsl:with-param name="name">
                <xsl:if test="substring-after(., '(')">
                    <xsl:value-of select="normalize-space(substring-before(substring-after(., '('), ')'))" />
                </xsl:if>
            </xsl:with-param>
        </xsl:call-template>
    </xsl:template>
    <xsl:template match="atom:author | atom:contributor">
        <xsl:call-template name="person">
            <xsl:with-param name="link">
                <xsl:if test="atom:uri">
                    <xsl:call-template name="resolvelink">
                        <xsl:with-param name="url" select="atom:uri" />
                    </xsl:call-template>
                </xsl:if>
            </xsl:with-param>
            <xsl:with-param name="email" select="atom:email" />
            <xsl:with-param name="name" select="atom:name" />
        </xsl:call-template>
    </xsl:template>
    <xsl:template name="person">
        <xsl:param name="email" />
        <xsl:param name="link" />
        <xsl:param name="name" />
        <xsl:variable name="showname">
            <xsl:choose>
                <xsl:when test="string-length($name)&gt;0">
                    <xsl:value-of select="$name" />
                </xsl:when>
                <xsl:when test="string-length($email)&gt;0">
                    <xsl:value-of select="$email" />
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$link" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="showlink">
            <xsl:choose>
                <xsl:when test="string-length($link)">
                    <xsl:value-of select="$link" />
                </xsl:when>
                <xsl:when test="string-length($email)">mailto:
                    <xsl:value-of select="$email" />
                </xsl:when>
            </xsl:choose>
        </xsl:variable>
        <xsl:if test="string-length($showname)&gt;0">
            <xsl:choose>
                <xsl:when test="string-length($showlink)&gt;0">
                    <a href="{$showlink}"><xsl:value-of select="$showname" /></a>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$showname" />
                </xsl:otherwise>
            </xsl:choose>
            <xsl:text>; </xsl:text>
        </xsl:if>
    </xsl:template>

    <!-- outputs text/(x)html; based on code from jtesler -->
    <xsl:template name="showtext">
        <xsl:param name="node" />
        <xsl:choose>
            <xsl:when test="name($node)='description' or $node/@type='html'">
                <div class="x-escape"><xsl:value-of select="$node" disable-output-escaping="yes" /></div>
            </xsl:when>
            <xsl:when test="$node/@type='xhtml'">
                <xsl:apply-templates select="$node/xhtml:div" mode="stripXhtml" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$node" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <!-- These templates are used for outputting the xhtml output.  We need to
    Strip xhtml: from all the nodes.  We must also convert any href and src
    attributes from relative to absolute if there is an xml:base -->
    <xsl:template match="xhtml:*" mode="stripXhtml">
        <xsl:element name="{local-name()}">
            <xsl:if test="@href"><xsl:attribute name="href"><xsl:call-template name="resolvelink"><xsl:with-param name="url" select="@href"/></xsl:call-template></xsl:attribute></xsl:if>
            <xsl:if test="@src"><xsl:attribute name="src"><xsl:call-template name="resolvelink"><xsl:with-param name="url" select="@src"/></xsl:call-template></xsl:attribute></xsl:if>
            <xsl:apply-templates select="@* | node()" mode="stripXhtml" />
        </xsl:element>
    </xsl:template>

    <xsl:template match="node() | @*" mode="stripXhtml">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" mode="stripXhtml" />
        </xsl:copy>
    </xsl:template>

    <!-- Since we already processed href and src nodes up above, don't process them again here -->
    <xsl:template match="@href | @src" mode="stripXhtml" priority="1" />

    <!-- returns absolute links, given absolute ones or relative ones with base ones -->
    <xsl:template name="resolvelink">
        <xsl:param name="url" />
        <xsl:param name="node" select="$url" />
        <xsl:choose>
            <xsl:when test="(contains($url,':') and (not(contains($url,'/')) or contains(substring-before($url,':'), substring-before($url,':')))) or not($url)">
                <xsl:value-of select="$url" />
            </xsl:when><!-- url is absolute already -->

            <xsl:otherwise>
                <xsl:variable name="basenode" select="($node/ancestor-or-self::*[@xml:base])[last()]" />
                <xsl:variable name="base">
                        <xsl:call-template name="resolvelink">
                            <xsl:with-param name="url" select="$basenode/@xml:base" />
                            <xsl:with-param name="node" select="($basenode/ancestor::*[@xml:base])[last()]" />
                        </xsl:call-template>
                </xsl:variable>
                <xsl:variable name="protocol">
                    <xsl:if test="contains($base, '://')">
                        <xsl:value-of select="concat(substring-before($base, '://'), '://')" />
                    </xsl:if>
                </xsl:variable>
                <xsl:variable name="basenoprot">
                    <xsl:choose>
                        <xsl:when test="string-length($protocol)">
                            <xsl:value-of select="substring-after($base, '://')" />
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="$base" />
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:variable name="trailingslash">
                    <xsl:if test="substring($basenoprot,string-length($basenoprot),1)='/'">true</xsl:if>
                </xsl:variable>
                <xsl:variable name="usebase">
                    <xsl:value-of select="$protocol" />
                    <xsl:choose>
                        <xsl:when test="not(string-length($trailingslash)) and not(contains($basenoprot, '/'))">
                            <xsl:value-of select="$basenoprot" />/
                        </xsl:when>
                        <xsl:when test="not(string-length($trailingslash)) and contains($basenoprot, '/') and $url != '' and not(starts-with($url, '#')) and not(starts-with($url, '?'))">
                            <xsl:call-template name="uponelevel">
                                <xsl:with-param name="url" select="$basenoprot" />
                            </xsl:call-template>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="$basenoprot" />
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:choose>
                    <xsl:when test="starts-with($url,'/')"><xsl:value-of select="concat(substring-before($base, '://'), '://', substring-before(substring-after($usebase, '://'), '/'), $url)" /></xsl:when>
                    <xsl:when test="starts-with($url,'../')">

                        <xsl:call-template name="resolvelink">
                            <xsl:with-param name="url" select="substring-after($url, '../')" />
                            <xsl:with-param name="base">
                                <xsl:value-of select="concat(substring-before($base,'://'), '://')" />
                                <xsl:call-template name="uponelevel"><xsl:with-param name="url" select="substring-after(substring($usebase, 0, string-length($usebase)-1), '://')" /></xsl:call-template>
                            </xsl:with-param>
                        </xsl:call-template>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:variable name="useurl">
                            <xsl:choose>
                                <xsl:when test="starts-with($url, './')">
                                    <xsl:value-of select="substring-after($url, './')" />
                                </xsl:when>
                                <xsl:otherwise><xsl:value-of select="$url" />
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:variable>
                        <xsl:value-of select="concat($usebase, $useurl)" />
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="uponelevel">
        <xsl:param name="url" />
        <xsl:variable name="firstpart" select="substring-before($url, '/')" />
        <xsl:variable name="afterslash" select="substring-after($url, '/')" />
        <xsl:variable name="secondpart">
            <xsl:if test="contains($afterslash, '/')">
                <xsl:call-template name="uponelevel">
                    <xsl:with-param name="url" select="$afterslash" />
                </xsl:call-template>
            </xsl:if>
        </xsl:variable>
        <xsl:value-of select="concat($firstpart, '/', $secondpart)" />
    </xsl:template>
</xsl:stylesheet>
