<!-- SPARQLMobileResults: Convert SPARQL Query Results XML format into HTML that 
     uses JQuery Mobile libraries to display on mobile phones.  

Bob DuCharme 2011-10-01 No warrantee expressed or implied.
-->
<xsl:stylesheet version="1.0"
                xmlns:s="http://www.w3.org/2005/sparql-results#" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"  method="html" />

  <xsl:variable name="JQMRelease">1.0b3</xsl:variable>


  <xsl:template match="s:sparql">
    <!-- It pains me to do the following, but apparently
         this is how you make HTML 5 documents with XSLT.  -->
    <xsl:text disable-output-escaping='yes'>&lt;!DOCTYPE html></xsl:text>
    <html>
      <xsl:apply-templates/>
    </html>
  </xsl:template>


  <xsl:template match="s:head">
    <head>
      <meta charset="utf-8"/>
      <meta name="viewport" content="width=device-width, initial-scale=1"/> 
      <title>SPARQL Query Results</title> 
      <link rel="stylesheet"  href="http://code.jquery.com/mobile/{$JQMRelease}/jquery.mobile-{$JQMRelease}.min.css" />  
      <script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
      <script src="http://code.jquery.com/mobile/{$JQMRelease}/jquery.mobile-{$JQMRelease}.min.js"></script>
    </head> 
  </xsl:template>

  <xsl:template match="s:results">
    <div data-role="page" class="type-interior">
      <div data-role="content">
        <div class="content-primary">

          <xsl:apply-templates/>

        </div>
      </div>
    </div>
  </xsl:template>


  <xsl:template match="s:result">

    <div data-role="collapsible" data-collapsed="true">
      <h1>
        <xsl:value-of select="/s:sparql/s:head/s:variable[1]/@name"/>:
        <xsl:value-of select="s:binding[1]"/>
      </h1>

      <table style="border-collapse: collapse;" width="100%">
        <xsl:apply-templates select="s:binding[position() > 1]"/>
      </table>

    </div>

  </xsl:template>


  <xsl:template match="s:binding">
    <xsl:variable name="namePosition" select="position()+1"/>
    <tr>
      <td class="ui-bar ui-bar-c">
    <xsl:value-of select="/s:sparql/s:head/s:variable[$namePosition]/@name"/>: <xsl:apply-templates/>
    </td>
    </tr>
  </xsl:template>

  <xsl:template match="s:literal">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="s:uri">
    <a href="{.}"><xsl:value-of select="."/></a>
  </xsl:template>

</xsl:stylesheet>
