<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xhtml="http://www.w3.org/1999/xhtml"
    xmlns:hs="http://www.hyperscope.org/hyperscope/opml/public/2006/05/09"
    exclude-result-prefixes="xhtml">
    
    <xsl:param name="HS_JS_URI"
        select="'/hyperscope/src/client/lib/hs/xslt/hyperscope.xsl'" />
    
    <xsl:output indent="yes" method="xml" />

    <xsl:template match="/">
        <xsl:processing-instruction name="xml-stylesheet">type="text/xsl" href="<xsl:value-of select="$HS_JS_URI" />" </xsl:processing-instruction><xsl:text>
</xsl:text>
        <opml hs:version="1.0" version="2.0">
            <head>
                <title><xsl:value-of select="/xhtml:html/xhtml:head/xhtml:title" /></title>
            </head>
            <body>
                <xsl:for-each select="//xhtml:ol[contains(@class,'xoxo')] | //xhtml:ul[contains(@class,'xoxo')]">
                    <xsl:call-template name="outline" />
                </xsl:for-each>
            </body>
        </opml>
    </xsl:template>

    <xsl:template name="outline">
        <xsl:variable name="node_text" select="normalize-space(./preceding-sibling::*[1])" />
        <outline text="{$node_text}">
            <xsl:for-each select="./xhtml:li">
                <xsl:variable name="subnodes" 
                    select="./xhtml:ol | ./xhtml:ul"/>
                <xsl:choose>
                    <xsl:when test="$subnodes">
                        <xsl:for-each select="$subnodes">
                            <xsl:call-template name="outline" />
                        </xsl:for-each>
                    </xsl:when>
                    <xsl:when test="./xhtml:a | ./xhtml:cite/xhtml:a">
                        <outline text="{normalize-space(.)}" type="link" url="{.//xhtml:a[1]/@href}" />
                    </xsl:when>
                    <xsl:otherwise>
                        <outline text="{normalize-space(.)}" />
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </outline>
    </xsl:template>

</xsl:stylesheet>

