<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://www.w3.org/1999/xhtml"
  xmlns:rss="http://purl.org/rss/1.0/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
  xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
  xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:admin="http://webns.net/mvcb/"
  xmlns:cc="http://web.resource.org/cc/" xmlns:exslt="http://exslt.org/common" version="1.0"
  extension-element-prefixes="exslt" exclude-result-prefixes="rdf rss content sy dc cc admin">
  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>
  <xsl:key name="items" match="rss:item" use="@rdf:about"/>
  <xsl:template match="/">
    <html>
      <head>
        <title>
          <xsl:value-of select="rdf:RDF/rss:channel/rss:title"/>
        </title>
      </head>
      <body>
        <h1>
          <xsl:value-of select="rdf:RDF/rss:channel/rss:title"/>
        </h1>
        <p>
          <xsl:value-of select="rdf:RDF/rss:channel/rss:description"/>
        </p>
        <xsl:apply-templates select="rdf:RDF/rss:channel/rss:items/rdf:Seq/rdf:li"/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="rdf:li">
    <xsl:for-each select="key('items', @rdf:resource)">
      <div>
        <h2>
          <xsl:value-of select="rss:title"/>
        </h2>
        <xsl:variable name="tmpfile" select="concat('file:///tmp/fragment', generate-id(), '.xml')"/>
        <exslt:document href="{$tmpfile}" method="text" encoding="utf-8">
          <xsl:text><![CDATA[<root>]]></xsl:text>
          <xsl:value-of select="content:encoded"/>
          <xsl:text><![CDATA[</root>]]></xsl:text>
        </exslt:document>
        <xsl:apply-templates select="document($tmpfile)/root/*" mode="content"/>
      </div>
    </xsl:for-each>
  </xsl:template>

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

  <xsl:template match="@src[starts-with(., '/')]" mode="content">
    <xsl:attribute name="href">
      <xsl:value-of select="concat('http://www.la-grange.net', .)"/>
    </xsl:attribute>
  </xsl:template>

</xsl:stylesheet>

