<?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:dc="http://purl.org/dc/elements/1.1/"
 xmlns:rec="http://www.w3.org/2001/02pd/rec54#"
 xmlns:xlink="http://www.w3.org/1999/xlink"
 xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/"
 xmlns="http://www.w3.org/2000/svg"
 version="1.0">

 <xsl:output method="xml" encoding="utf-8" indent="yes" doctype-public="-//W3C//DTD SVG 1.0//EN" doctype-system="http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"/>

 <xsl:template match="/">
  <xsl:processing-instruction name="xml-stylesheet">href="tr-refs.css" type="text/css"</xsl:processing-instruction>
  <svg a:scriptImplementation="Adobe"
     width="1600"
     height="1200"
     onload="init(evt)">
   <script a:scriptImplementation="Adobe" xlink:href="cites.es" type="text/ecmascript"/>
   <xsl:apply-templates select="rdf:RDF/rdf:Description[not(@rdf:about='#0_work')]">
    <xsl:sort select="count(//rdf:Description/rec:cites[@rdf:resource=current()/@rdf:about])" data-type="number" order="descending"/>
   </xsl:apply-templates>
  </svg>
 </xsl:template>

 <xsl:template match="rdf:Description">
  <xsl:variable name="uri" select="@rdf:about"/>
  <xsl:variable name="shortname">
   <xsl:choose>
    <xsl:when test="contains(substring-after(@rdf:about,'TR/'),'/')">
     <xsl:value-of select="substring-before(substring-after(@rdf:about,'TR/'),'/')"/>
    </xsl:when>
    <xsl:otherwise>
     <xsl:value-of select="substring-after(@rdf:about,'TR/')"/>
    </xsl:otherwise>
   </xsl:choose>
  </xsl:variable>
  <rdf:Description rdf:about="{$uri}">
   <dc:title><xsl:value-of select="dc:title"/></dc:title>
   <rec:shortname><xsl:value-of select="$shortname"/></rec:shortname>
   <xsl:for-each select="rec:cites">
    <rec:cites rdf:resource="{@rdf:resource}"/>
   </xsl:for-each>
   <xsl:for-each select="//rdf:Description">
    <xsl:if test="rec:cites/@rdf:resource=$uri">
     <rec:citedBy rdf:resource="{@rdf:about}"/>
    </xsl:if>
   </xsl:for-each>
   <rec:citedByCount>
    <xsl:value-of select="count(//rdf:Description/rec:cites[@rdf:resource=$uri])"/>
   </rec:citedByCount>
   <rec:citesCount>
    <xsl:value-of select="count(rec:cites)"/>
   </rec:citesCount>
  </rdf:Description>
 </xsl:template>
</xsl:stylesheet>
