<xsl:transform version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:html="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="html">

  <xsl:param name="Id" select="'$Id: examples-extract.xsl,v 1.14 2006/03/23 16:27:52 eric Exp $'"/>
  <xsl:param name="IdStr" select="substring($Id, 2, string-length($Id)-2)"/>
  <xsl:param name="TitlePrefix" select="'Formal Definitions from '"/>

<div>
<h1>examples.xsl - extract div.exampleGroup from rq23.html</h1>
<a href="http://www.w3.org/2001/sw/DataAccess/rq23/examples-extract.xsl">xsl stylesheet</a> <a href="http://www.w3.org/2001/sw/DataAccess/rq23/">SPARQL Query spec</a>
<address>
$Id: examples-extract.xsl,v 1.14 2006/03/23 16:27:52 eric Exp $
</address>
</div>

<!--
 Document head 
 -->
<xsl:template match="/">
<html>
  <head>
    <title>SPARQL Query Tests</title>
    <link rel="stylesheet" type="text/css" href="examples-style.css" />
    <link rel="stylesheet" type="text/css" href="local.css"/>
  </head>
  <body>
    <h1>SPARQL Query Tests</h1>

    <p>This document consists of the examples from the <a href="./">SPARQL Query</a> specification. The <a href="Makefile">Makefile</a> has a target <code>examples.html</code> which generated this document. Example run:</p>

    <pre>make examples.html
xsltproc -o examples.html examples-extract.xsl Overview.html</pre>

    <xsl:apply-templates />
 </body>
</html>
</xsl:template>

<xsl:template match="*[@class='exampleGroup']">
  <xsl:text>

</xsl:text>
  <div class="exampleGroup">

  <xsl:for-each select='.//html:pre[contains(concat(" ", @class, " "), " data ")]'>
    <xsl:text>
  </xsl:text>
    <div class="dataBox"><h2>
    <xsl:call-template name="title">
      <xsl:with-param name="type" select="'data'"/>
      <xsl:with-param name="tag" select="'pre'"/>
      <xsl:with-param name="start" select="."/>
      <xsl:with-param name="list" select="./preceding-sibling::*"/>
      <xsl:with-param name="count" select="0"/>
    </xsl:call-template></h2>
    <pre class="data">
      <xsl:value-of select='.'/>
    </pre>
    <xsl:text>
  </xsl:text>
    </div>
  </xsl:for-each>

  <xsl:for-each select='.//*[contains(concat(" ", @class, " "), " queryGroup ")]'>
    <xsl:text>
  </xsl:text>
    <div class="queryGroup"><h2>
    <xsl:call-template name="title">
      <xsl:with-param name="type" select="'queryGroup'"/>
      <xsl:with-param name="tag" select="'div'"/>
      <xsl:with-param name="start" select="."/>
      <xsl:with-param name="list" select="./preceding-sibling::*"/>
      <xsl:with-param name="count" select="0"/>
    </xsl:call-template></h2>
      <pre class="query">
	<xsl:value-of select='.//html:pre[contains(concat(" ", @class, " "), " query ")]'/>
      </pre>

    <xsl:for-each select='.//html:div[contains(concat(" ", @class, " "), " result ")]'>
  <xsl:text>
    </xsl:text>
      <xsl:variable name="cur" select="."/>

      <xsl:variable name="titleStr">
        <xsl:call-template name="title">
          <xsl:with-param name="type" select="'result'"/>
          <xsl:with-param name="tag" select="'div'"/>
          <xsl:with-param name="start" select="."/>
          <xsl:with-param name="list" select="./preceding-sibling::*"/>
          <xsl:with-param name="count" select="0"/>
        </xsl:call-template>
      </xsl:variable>

      <xsl:choose>
        <xsl:when test='contains(concat(" ", html:table/@class, " "), " resultTable ")'>
          <h2><xsl:copy-of select="$titleStr"/></h2>
          <div class="result"><xsl:copy-of select="html:table"/></div>
          <!-- h2>turtl-ized <xsl:copy-of select="$titleStr"/></h2>
          <div class="result">
          <xsl:call-template name="table2turtle">
            <xsl:with-param name="ctx" select="html:table"/>
          </xsl:call-template>
          </div -->
        </xsl:when>

        <xsl:when test='contains(concat(" ", html:pre/@class, " "), " resultAsk ")'>
          <h2><xsl:copy-of select="$titleStr"/></h2>
          <div class="result"><xsl:copy-of select="html:pre"/></div>
        </xsl:when>

        <xsl:when test='contains(concat(" ", html:pre/@class, " "), " resultSet ")'>
          <h2>XML <xsl:copy-of select="$titleStr"/></h2>
          <div class="result"><xsl:copy-of select="html:pre"/></div>
        </xsl:when>

        <xsl:when test='contains(concat(" ", html:pre/@class, " "), " resultGraph ")'>
          <h2>Turtle <xsl:copy-of select="$titleStr"/></h2>
          <div class="result"><xsl:copy-of select="html:pre"/></div>
        </xsl:when>

        <xsl:otherwise><pre style="color: red"><xsl:value-of select="."/></pre></xsl:otherwise>
      </xsl:choose>

    </xsl:for-each>

    <xsl:text>
  </xsl:text>
    </div>
  </xsl:for-each>
  <xsl:text>
</xsl:text>
  </div>
</xsl:template>


<xsl:template name="title">
  <xsl:param name="type"/>  <!-- 'data' or 'queryGroup' or ... -->
  <xsl:param name="tag"/>   <!-- 'pre' or 'div' -->
  <xsl:param name="start"/> <!-- . -->
  <xsl:param name="list"/>  <!-- ./preceding-sibling::* -->
  <xsl:param name="count"/> <!-- 0 -->
  <xsl:variable name="size" select="count($list)"/>

  <xsl:choose>
    <!-- Ran off the end of the list - resume with parent's preceding siblings. -->
    <xsl:when test="$size &lt; 1">
      <xsl:call-template name="title">
        <xsl:with-param name="type" select="$type"/>
        <xsl:with-param name="tag" select="$tag"/>
        <xsl:with-param name="start" select="$start/.."/>
        <xsl:with-param name="list" select="$start/../preceding-sibling::*"/>
        <xsl:with-param name="count" select="$count"/>
      </xsl:call-template>
    </xsl:when>

    <!-- Found a valid title for this tag. -->
    <xsl:when test="name($list[$size]) = 'h2' or name($list[$size]) = 'h3' or name($list[$size]) = 'h4'">

      <xsl:variable name="base" select="substring-before($list[$size]/text(), ' ')"/>
      <xsl:variable name="typeText">
        <xsl:choose>
          <xsl:when test="$type = 'queryGroup'">query</xsl:when>
          <xsl:when test="$type = 'result'">result</xsl:when>
          <xsl:when test="$type = 'data'">data</xsl:when>
          <xsl:otherwise>! type <xsl:value-of select="$type"/> unrecognized!</xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <!-- 
       suffix contains the section number and a count within that section.
       -->
      <xsl:variable name="suffix">
        <xsl:choose>
          <xsl:when test="$count &gt; 0">
            <xsl:variable name="typeExtensionFlag">
              <xsl:choose>
                <xsl:when test="$type = 'queryGroup'">q</xsl:when>
                <xsl:when test="$type = 'result'">r</xsl:when>
                <xsl:when test="$type = 'data'">d</xsl:when>
                <xsl:otherwise>! type <xsl:value-of select="$type"/> unrecognized!</xsl:otherwise>
              </xsl:choose>
            </xsl:variable>
            <xsl:value-of select="concat('-', $typeExtensionFlag, $count)"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="''"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <!--
       HTML block
          <span class="role">data</span> <span class="base">2.1</span><span class="suffix"/> <a href="./#WritingSimpleQueries">Writing a Simple Query</a>
       -->
      <span class="role"><xsl:value-of select="$typeText"/></span><xsl:text> </xsl:text>
      <span class="base"><xsl:value-of select="$base"/></span><span class="suffix"><xsl:value-of select="$suffix"/></span><xsl:text> </xsl:text>
      <xsl:variable name="hd" select="normalize-space($list[$size]/html:a)" />
      <xsl:variable name="anchor" select="$list[$size]/html:a/@id" />
      <a href='{concat("./#", $anchor)}'><xsl:value-of select="$hd" /></a>

    </xsl:when>

    <!-- Recurse, counting relevent divs. -->
    <xsl:otherwise>
      <xsl:variable name="burried" select="count($list[$size]/descendant-or-self::html:*[contains(concat(' ', @class, ' '), concat(' ', $type, ' ')) and (name() = 'pre' or name() = 'div')])"/>
      <xsl:call-template name="title">
        <xsl:with-param name="type" select="$type"/>
        <xsl:with-param name="tag" select="$tag"/>
        <xsl:with-param name="start" select="$start"/>
        <xsl:with-param name="list" select="$list[position() &lt; $size]"/>
        <xsl:with-param name="count" select="$count+$burried"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- compose Turtle results from an HTML table -->
<xsl:template name="table2turtle">
  <xsl:param name="ctx"/>  <!-- . -->
  <pre class="resultTurtle">
  <xsl:text>
@prefix rdf:  &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rs:      &lt;http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .

[]  &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
                rs:ResultSet ;
      </xsl:text>
  <xsl:text>rs:resultVariable</xsl:text>

  <xsl:for-each select="$ctx/html:tbody/html:tr[1]/html:th">
    <xsl:text> "</xsl:text>
    <xsl:value-of select="." />
    <xsl:text>",</xsl:text>
  </xsl:for-each>
  <xsl:text>;&#x0A;</xsl:text>

  <xsl:for-each select="$ctx/html:tbody/html:tr[position() &gt; 1]">
    <xsl:text>     rs:solution [&#x0a;</xsl:text>
    <xsl:for-each select="html:td">
      <xsl:variable name="n" select="normalize-space(../../html:tr[1]/html:th[position()])" />

      <xsl:text>     rs:binding  [</xsl:text>
      <xsl:text> rs:variable "</xsl:text>
      <xsl:value-of select="$n" />
      <xsl:text>";&#x0a;</xsl:text>
      <xsl:text>        rs:value </xsl:text>
      <xsl:value-of select="." />
      <xsl:text> ];&#x0a;</xsl:text>
    </xsl:for-each>
    <xsl:text>];&#x0a;</xsl:text>
  </xsl:for-each>
  <xsl:text>.&#x0a;</xsl:text>
  </pre>
</xsl:template>

<!-- don't pass text thru -->
<xsl:template match="text()" />

</xsl:transform>

