<?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"
    exclude-result-prefixes="xhtml">
    
    <xsl:output indent="yes" method="xml" />

    <xsl:template match="/">
        <opml version="1.1">
            <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">
        <outline text="{normalize-space(./preceding-sibling::*[1])}">
            <xsl:for-each select="./xhtml:li">
                <xsl:variable name="subnodes" 
                    select="./xhtml:ol[contains(@class,'xoxo')] | ./xhtml:ul[contains(@class,'xoxo')]"/>
                <xsl:choose>
                    <xsl:when test="$subnodes">
                        <xsl:call-template name="outline" />
                    </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>

