<beginfold id='1'><?xml</beginfold id='1'> version="1.0" encoding="iso-8859-15"<endfold id='1'>?></endfold id='1'>

<beginfold id='2'><!--</beginfold id='2'>
	This file is freely distributable, created by Wilbert Berendsen (wbsoft@xs4all.nl)

	This is just a simple XSLT file that converts some HTML pages to a XBEL bookmarklist.
	It is included here to test the new XSLT highlighting by Peter Lammich.
	
	TODO: add better test file.
<endfold id='2'>--></endfold id='2'>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:h="http://www.w3.org/1999/xhtml">
<xsl:output method="xml" encoding="iso-8859-15" indent="yes"
 doctype-public="+//IDN python.org//DTD XML Bookmark Exchange Language 1.0//EN//XML"
 doctype-system="http://www.python.org/topics/xml/dtds/xbel-1.0.dtd"
/>

<beginfold id='2'><!--</beginfold id='2'> main <endfold id='2'>--></endfold id='2'>
<xsl:template match="/">
<xbel><folder><title>Linux at Home Links</title>
<xsl:for-each select="//*[@id='maincontents']//h:li">
  <xsl:variable name="f" select="document(h:a/@href)/h:html"/>
  <folder>
  <title><xsl:value-of select="h:a/h:strong"/></title>
  <desc><xsl:value-of select="normalize-space(h:a/text())"/></desc>
  <xsl:for-each select="$f//h:div[@id='maincontents']">
  <xsl:call-template name="getbookmarks"/>
  </xsl:for-each>
  
  </folder>
</xsl:for-each>
</folder></xbel>
</xsl:template>
<beginfold id='2'><!--</beginfold id='2'> end of main <endfold id='2'>--></endfold id='2'>

<beginfold id='2'><!--</beginfold id='2'> get bookmarks from a page <endfold id='2'>--></endfold id='2'>
<xsl:template name="getbookmarks">

<xsl:choose>
  <beginfold id='2'><!--</beginfold id='2'> harvest links from p or li elements <endfold id='2'>--></endfold id='2'>
  <xsl:when test="self::h:li or self::h:p">
    <xsl:variable name="t" select="normalize-space()"/>
    <xsl:for-each select=".//h:a[1]">
      <xsl:call-template name="bookmark">
        <xsl:with-param name="desc" select="$t"/>
      </xsl:call-template>
    </xsl:for-each>
    <xsl:for-each select=".//h:a[position()!=1]">
      <xsl:call-template name="bookmark"/>
    </xsl:for-each>
  </xsl:when>


  <xsl:otherwise>
    <xsl:for-each select="*">
      <xsl:call-template name="getbookmarks"/>
    </xsl:for-each>
  </xsl:otherwise>

</xsl:choose>

</xsl:template>


<xsl:template name="bookmark">
  <xsl:param name="href" select="@href"/>
  <xsl:param name="desc" select="''"/>
  <xsl:param name="title" select="normalize-space()"/>

  <xsl:variable name="realdesc">
    <xsl:choose>
      <xsl:when test="starts-with($desc,$title)">
        <xsl:choose>
          <xsl:when test="starts-with(translate(substring-after($desc,$title),',.;','...'),'.')">
            <xsl:value-of select="substring($desc,string-length($title) + 2)"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="substring-after($desc,$title)"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$desc"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  
  <bookmark>
    <xsl:attribute name="href">
      <xsl:choose>
        <xsl:when test="starts-with($href,'http://') or starts-with($href,'ftp://') or starts-with($href,'mailto:')">
          <xsl:value-of select="$href"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="concat('http://www.xs4all.nl/~wbsoft/linux/links/',$href)"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:attribute>
    <title><xsl:value-of select="normalize-space($title)"/></title>
    <xsl:if test="normalize-space($realdesc) != ''">
      <desc><xsl:value-of select="normalize-space($realdesc)"/></desc>
    </xsl:if>
  </bookmark>
</xsl:template>



</xsl:stylesheet>
