<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:kiln="http://www.kcl.ac.uk/artshums/depts/ddh/kiln/ns/1.0"
                xmlns:tei="http://www.tei-c.org/ns/1.0"
                xmlns:xi="http://www.w3.org/2001/XInclude"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="2.0">

  <xsl:import href="cocoon://_internal/url/reverse.xsl">
      <xsl:stylesheet xmlns:xs="http://www.w3.org/2001/XMLSchema"
                      xmlns:map="http://apache.org/cocoon/sitemap/1.0"
                      exclude-result-prefixes="#all"
                      version="2.0">
         <xsl:function name="kiln:url-for-match" as="xs:string">
            <xsl:param name="match-id" as="xs:string"/>
            <xsl:param name="parameters"/>
            <xsl:param name="cocoon-context"/>
            <xsl:variable name="url">
               <xsl:choose>
                  <xsl:when test="$match-id = 'kiln-introspection-match-url'">
                     <xsl:text>cocoon://</xsl:text>
                     <xsl:text>_internal/introspection/url/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-rdf-harvest-all-display'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/rdf/harvest/all.html</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-rdf-harvest-all'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/rdf/harvest/all.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-rdf-harvest-display'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/rdf/harvest/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.html</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-rdf-harvest'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/rdf/harvest/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-rdf-query'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/rdf/query/graph/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-rdf-generate-authority'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/rdf/generate/authority/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-rdf-generate-epidoc'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/rdf/generate/epidoc/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-rdf-generate-index'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/rdf/generate/indices/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-rdf-generate-tei'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/rdf/generate/tei/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-rdf-generate-static'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/rdf/generate/rdf/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-rdf-query-from-file'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/rdf/construct/graph/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-solr-site-list'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/solr/index/site-list.html</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-solr-index-all'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/solr/index/all.html</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-solr-index-indices'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/solr/index/indices/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.html</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-solr-index'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/solr/index/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/</xsl:text>
                     <xsl:value-of select="$parameters[3-1]"/>
                     <xsl:text>.html</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-solr-add-all-indices'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/solr/add/indices/</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-solr-add-all'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/solr/add/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-solr-add-tei-eats'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/solr/add/tei-eats/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-solr-add-tei-eats-authority'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/solr/add/tei-eats-authority/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/</xsl:text>
                     <xsl:value-of select="$parameters[3-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-solr-add-indices'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/solr/add/indices/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-solr-add-index'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/solr/add/indices/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/</xsl:text>
                     <xsl:value-of select="$parameters[3-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-solr-add-bibliographic-concordance'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/solr/add/concordance/authority/bibliography.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-solr-add'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/solr/add/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/</xsl:text>
                     <xsl:value-of select="$parameters[3-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-solr-preprocess-tei-eats'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/solr/preprocess/tei-eats/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-solr-preprocess-eatsml'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/solr/preprocess/eatsml/entities.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-solr-preprocess-eatsml-authority'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/solr/preprocess/eatsml/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/entities.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-solr-preprocess'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/solr/preprocess/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/</xsl:text>
                     <xsl:value-of select="$parameters[3-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-admin-home-page'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-admin-system-status'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/system-status.html</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-admin-introspection-index'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/introspection/</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-admin-introspection-match'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/introspection/match/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.html</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-admin-introspection-template-empty'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/introspection/template/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.html</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-admin-introspection-template-xslt'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/introspection/template/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.xsl</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-admin-introspection-xslt'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/introspection/xslt/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.xsl</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-admin-introspection-match-url'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/introspection/url/</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-admin-schematron-validation'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/schematron/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.html</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-admin-resource-check'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>admin/resource-check/images/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.html</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-admin-image-resource-check'">
                     <xsl:text>cocoon://</xsl:text>
                     <xsl:text>admin/resource-check/images/tei/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-admin-image-reference-check'">
                     <xsl:text>cocoon://</xsl:text>
                     <xsl:text>admin/resource-check/image-refs/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-admin-image-file-check'">
                     <xsl:text>cocoon://</xsl:text>
                     <xsl:text>admin/resource-check/image-files.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-preprocess-tei-no-language'">
                     <xsl:text>cocoon://</xsl:text>
                     <xsl:text>internal/tei/preprocess/no-language/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-preprocess-epidoc-no-language'">
                     <xsl:text>cocoon://</xsl:text>
                     <xsl:text>internal/epidoc/preprocess/no-language/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-preprocess-tei-language'">
                     <xsl:text>cocoon://</xsl:text>
                     <xsl:text>internal/tei/preprocess/language/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/</xsl:text>
                     <xsl:value-of select="$parameters[3-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-preprocess-epidoc-language'">
                     <xsl:text>cocoon://</xsl:text>
                     <xsl:text>internal/epidoc/preprocess/language/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/</xsl:text>
                     <xsl:value-of select="$parameters[3-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-preprocess-authority-language'">
                     <xsl:text>cocoon://</xsl:text>
                     <xsl:text>internal/authority/preprocess/language/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/</xsl:text>
                     <xsl:value-of select="$parameters[3-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-epidoc-custom-html'">
                     <xsl:text>cocoon://</xsl:text>
                     <xsl:text>internal/epidoc/custom/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/</xsl:text>
                     <xsl:value-of select="$parameters[3-1]"/>
                     <xsl:text>.html</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-search-query'">
                     <xsl:text>cocoon://</xsl:text>
                     <xsl:text>internal/search/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-index-extract'">
                     <xsl:text>cocoon://</xsl:text>
                     <xsl:text>internal/index/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/</xsl:text>
                     <xsl:value-of select="$parameters[3-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-typed-metadata'">
                     <xsl:text>cocoon://</xsl:text>
                     <xsl:text>internal/metadata/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/</xsl:text>
                     <xsl:value-of select="$parameters[3-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-metadata'">
                     <xsl:text>cocoon://</xsl:text>
                     <xsl:text>internal/metadata/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-images-gif'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>images/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.gif</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-images-jpeg-thumbnail'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>images/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>-thumb.jpg</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-images-jpeg'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>images/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.jpg</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-images-png'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>images/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.png</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-home-page'">
                     <xsl:text>/</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-language-home-page'">
                     <xsl:text>/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-search'">
                     <xsl:text>/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/search/</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-tei-index-display'">
                     <xsl:text>/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/texts/</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-tei-display-html'">
                     <xsl:text>/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/texts/</xsl:text>
                     <xsl:value-of select="$parameters[3-1]"/>
                     <xsl:text>.html</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-tei-display-xml'">
                     <xsl:text>/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/texts/</xsl:text>
                     <xsl:value-of select="$parameters[3-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-tei-display-pdf'">
                     <xsl:text>/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/texts/</xsl:text>
                     <xsl:value-of select="$parameters[3-1]"/>
                     <xsl:text>.pdf</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-tei-display-epub'">
                     <xsl:text>/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/texts/</xsl:text>
                     <xsl:value-of select="$parameters[3-1]"/>
                     <xsl:text>.epub</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-epidoc-index-display'">
                     <xsl:text>/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/inscriptions/</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-epidoc-zip'">
                     <xsl:text>/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/inscriptions/zip/</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-epidoc-display-html'">
                     <xsl:text>/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/inscriptions/</xsl:text>
                     <xsl:value-of select="$parameters[3-1]"/>
                     <xsl:text>.html</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-epidoc-display-xml'">
                     <xsl:text>/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/inscriptions/</xsl:text>
                     <xsl:value-of select="$parameters[3-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-indices-type-display'">
                     <xsl:text>/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/indices/</xsl:text>
                     <xsl:value-of select="$parameters[3-1]"/>
                     <xsl:text>/</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-index-display-html'">
                     <xsl:text>/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/indices/</xsl:text>
                     <xsl:value-of select="$parameters[3-1]"/>
                     <xsl:text>/</xsl:text>
                     <xsl:value-of select="$parameters[4-1]"/>
                     <xsl:text>.html</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-concordance-bibliography'">
                     <xsl:text>/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/concordances/bibliography/</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-concordance-bibliography-item'">
                     <xsl:text>/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>/concordance/bibliography/</xsl:text>
                     <xsl:value-of select="$parameters[3-1]"/>
                     <xsl:text>.html</xsl:text>
                  </xsl:when>
                  <xsl:when test="$match-id = 'local-tei-backend-xml'">
                     <xsl:text>/</xsl:text>
                     <xsl:text>text/</xsl:text>
                     <xsl:value-of select="$parameters[2-1]"/>
                     <xsl:text>.xml</xsl:text>
                  </xsl:when>
               </xsl:choose>
            </xsl:variable>
            <xsl:variable name="full-url">
               <xsl:if test="$cocoon-context and not(starts-with($url, 'cocoon://'))">cocoon:/</xsl:if>
               <xsl:value-of select="$url"/>
            </xsl:variable>
            <xsl:value-of select="$full-url"/>
         </xsl:function>
      </xsl:stylesheet>
   </xsl:import>

  <xsl:param name="subdirectory"/>

  <xsl:template match="insert">
      <xsl:copy>
         <xsl:apply-templates select="tei:TEI/tei:text/tei:body/tei:div[@xml:id]"/>
      </xsl:copy>
  </xsl:template>

  <xsl:template match="tei:div">
      <index name="{tei:head}" xml:id="{@xml:id}">
         <xi:include href="{kiln:url-for-match('local-solr-add-index', ($subdirectory, @xml:id), 1)}"/>
      </index>
  </xsl:template>

</xsl:stylesheet>