first touch: make sure that I can grab at the data and send it where I want it to go

Change-Id: Ide29bf5f041df7b2838aecd3ea445abf7095c9f9
diff --git a/nkjp2korap.xsl b/nkjp2korap.xsl
index 5b19e00..b8c156e 100644
--- a/nkjp2korap.xsl
+++ b/nkjp2korap.xsl
@@ -1,47 +1,173 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
-                xmlns:xs="http://www.w3.org/2001/XMLSchema"
-                xmlns:nkjp="http://www.nkjp.pl/ns/1.0"
-                xmlns:tei="http://www.tei-c.org/ns/1.0"
-                xmlns:f="func" xmlns:map="http://www.w3.org/2005/xpath-functions/map"
-                exclude-result-prefixes="xs f map nkjp tei" version="3.0" expand-text="yes">
+  xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:nkjp="http://www.nkjp.pl/ns/1.0"
+  xmlns:tei="http://www.tei-c.org/ns/1.0" xmlns:f="func"
+  xmlns:map="http://www.w3.org/2005/xpath-functions/map" exclude-result-prefixes="xs f map nkjp tei"
+  version="3.0" expand-text="yes">
 
-  <xsl:param name="corpusID"/>
-  <xsl:param name="docID"/>
-  <xsl:param name="textID"/>
-  <xsl:param name="targetDir" select="'test/resources/nkjp2korap_sample1/KOT/'"/> 
+  <xsl:param name="corpusID" as="xs:string"/>
+  <xsl:param name="docID" as="xs:string"/>
+  <xsl:param name="textID" as="xs:string"/>
+
+  <xsl:param name="sourceDir" select="'test/resources/nkjp2korap_sample1/KOT/'" as="xs:string"/>
   <!-- the select is a fallback, so that it's clear to the reviewer what I'm passing in the param -->
-  
-  <xsl:template name="xsl:initial-template">
-    
-    <xsl:variable name="text.xml" as="document-node()" select="doc($targetDir || 'text.xml')"/>
 
-    <!-- Template to create the data.xml file -->
-    <xsl:result-document encoding="UTF-8" method="xml" indent="yes" xpath-default-namespace="http://ids-mannheim.de/ns/KorAP"
-      href="{'test/output/' || $corpusID || '/' || $docID || '/' || $textID || '/data.xml'}">
+  <xsl:variable name="targetTextDir"
+    select="'test/output/' || $corpusID || '/' || $docID || '/' || $textID || '/'" as="xs:string"/>
+  <xsl:variable name="targetCorpusDir" select="'test/output/' || $corpusID || '/'" as="xs:string"/>
+
+  <xsl:variable name="systemDoctypeI5"
+    select="'http://corpora.ids-mannheim.de/idsxces1/DTD/ids.xcesdoc.dtd'" as="xs:string"
+    static="true"/>
+
+  <xsl:variable name="publicDoctypeI5" select="'-//IDS//DTD IDS-XCES 1.0//EN'" as="xs:string"
+    static="true"/>
+
+  <xsl:variable name="KorAP_namespace" select="'http://ids-mannheim.de/ns/KorAP'" static="true"
+    as="xs:string"/>
+
+
+  <xsl:mode name="corpus" on-no-match="deep-skip"/>
+  <xsl:mode name="text" on-no-match="deep-skip"/>
+
+
+  <xsl:template name="xsl:initial-template">
+    <xsl:variable name="text.xml" as="document-node()" select="doc($sourceDir || 'text.xml')"/>
+
+    <xsl:call-template name="create_data">
+      <xsl:with-param name="text.xml" select="$text.xml" as="document-node()"/>
+    </xsl:call-template>
+
+    <xsl:call-template name="create_text_header">
+      <xsl:with-param name="text.xml" select="$text.xml" as="document-node()"/>
+    </xsl:call-template>
+
+    <xsl:call-template name="create_corpus_header">
+      <xsl:with-param name="text.xml" select="$text.xml" as="document-node()"/>
+    </xsl:call-template>
+  </xsl:template>
+
+  <!--   **************************        data.xml       *******************                -->
+
+  <xsl:template name="create_data">
+    <xsl:param name="text.xml" as="document-node()"/>
+    <!-- create the data.xml file -->
+    <xsl:result-document encoding="UTF-8" method="xml" indent="yes"
+      xpath-default-namespace="{$KorAP_namespace}" href="{$targetTextDir || 'data.xml'}">
+
       <xsl:processing-instruction name="xml-model">href=&quot;text.rng&quot; type=&quot;application/xml&quot; schematypens=&quot;http://relaxng.org/ns/structure/1.0&quot;</xsl:processing-instruction>
-      <xsl:element name="raw_text">
+      <xsl:element name="raw_text" namespace="{$KorAP_namespace}">
         <xsl:attribute name="docid" select="$corpusID || '_' || $docID || '.' || $textID"/>
-        <metadata file="metadata.xml"/>
-        <text>
+        <xsl:element name="metadata" namespace="{$KorAP_namespace}">
+          <xsl:attribute name="file" select="'metadata.xml'"/>
+        </xsl:element>
+
+        <xsl:element name="text" namespace="{$KorAP_namespace}">
           <xsl:value-of select="$text.xml//*[local-name() = 'ab']"/>
-        </text>
+        </xsl:element>
       </xsl:element>
     </xsl:result-document>
-    
-    <xsl:result-document encoding="UTF-8" method="xml" indent="yes" xpath-default-namespace="http://ids-mannheim.de/ns/KorAP"
-      href="{'test/output/' || $corpusID || '/' || $docID || '/' || $textID || '/header.xml'}" 
-      doctype-public="-//IDS//DTD IDS-XCES 1.0//EN" doctype-system="http://corpora.ids-mannheim.de/idsxces1/DTD/ids.xcesdoc.dtd">
-      
-      <idsHeader type="document" pattern="text" status="new" version="1.1" TEIform="teiHeader">
-        <xsl:copy-of select="$text.xml//tei:TEI/tei:teiHeader/tei:fileDesc"/>
-      </idsHeader>
-      
-      <xsl:copy>
-        
-      </xsl:copy>
-    </xsl:result-document>
-
   </xsl:template>
 
+  <!--   **************************        CORPUS header       *******************                -->
+  <xsl:template name="create_corpus_header">
+    <xsl:param name="text.xml" as="document-node()"/>
+
+    <!-- create the corpus-level header.xml file -->
+    <xsl:result-document encoding="UTF-8" method="xml" indent="yes"
+      href="{$targetCorpusDir || 'header.xml'}">
+
+      <!--doctype-public="{$publicDoctypeI5}"
+          doctype-system="{$systemDoctypeI5}">
+        these are, sadly, useless
+      -->
+
+      <idsHeader type="corpus" pattern="text" status="new" version="1.1" TEIform="teiHeader">
+        <xsl:apply-templates select="$text.xml//tei:TEI/tei:teiHeader/tei:fileDesc" mode="corpus"/>
+      </idsHeader>
+    </xsl:result-document>
+  </xsl:template>
+
+  <xsl:template match="tei:fileDesc" mode="corpus">
+    <fileDesc>
+      <xsl:apply-templates mode="corpus"/>
+    </fileDesc>
+  </xsl:template>
+
+  <xsl:template match="tei:title" mode="corpus">
+    <c.title>
+      <xsl:apply-templates/>
+    </c.title>
+  </xsl:template>
+
+  <xsl:template match="tei:titleStmt" mode="corpus">
+    <titleStmt>
+      <korpusSigle>
+        <xsl:value-of select="$corpusID"/>
+      </korpusSigle>
+      <xsl:apply-templates mode="corpus"/>
+    </titleStmt>
+  </xsl:template>
+
+
+  <!--   **************************        TEXT header      *******************                -->
+
+  <xsl:template name="create_text_header">
+    <xsl:param name="text.xml" as="document-node()"/>
+
+    <!-- create the local header.xml file -->
+    <xsl:result-document encoding="UTF-8" method="xml" indent="yes"
+      xpath-default-namespace="http://ids-mannheim.de/ns/KorAP"
+      href="{$targetTextDir || 'header.xml'}">
+
+      <idsHeader type="document" pattern="text" status="new" version="1.1" TEIform="teiHeader">
+        <xsl:apply-templates select="$text.xml//tei:TEI/tei:teiHeader/tei:fileDesc" mode="text"/>
+      </idsHeader>
+    </xsl:result-document>
+  </xsl:template>
+
+  <xsl:template match="tei:fileDesc" mode="text">
+    <fileDesc>
+      <xsl:apply-templates mode="text"/>
+    </fileDesc>
+  </xsl:template>
+
+  <xsl:template match="tei:title" mode="text">
+    <t.title>
+      <xsl:apply-templates/>
+    </t.title>
+  </xsl:template>
+
+  <xsl:template match="tei:titleStmt" mode="text">
+    <titleStmt>
+      <korpusSigle>
+        <xsl:value-of select="$corpusID || '/' || $textID"/>
+      </korpusSigle>
+      <xsl:apply-templates mode="text"/>
+    </titleStmt>
+  </xsl:template>
+
+
+
+
+  <!-- this template can be called by the XSPEC test; TODO: find a way to call the main() template directly -->
+  <!-- I have not fully handled the param transmission, which would have to be kludged in just for the sake of XSPec,
+       because I'm disabling this for now, due to XSpec design issues; relevant links, a.o.:
+       
+  https://stackoverflow.com/questions/64933277/what-is-the-cause-of-error-cannot-execute-xslresult-document-while-evaluating
+  https://www.balisage.net/Proceedings/vol25/html/Galtman01/BalisageVol25-Galtman01.html
+  
+  In short: the internal design of XSpec forces kludges when one wants to use xsl:result-document in their stylesheets. But I don't
+  want to be strangled by kludges at the beginning of work, I've already lost quite a bit of time on this investigation,
+  I will therefore "just code" and then can think of externalizing bits of templates if we want to play with tests. For now,
+  I don't want to have to handle context items is a special way inside variables, etc., because I'm not sure it's worth it.
+  
+  -->
+  <!--<xsl:template name="test_full">
+    <xsl:param name="corpusID"/>
+    <xsl:param name="docID"/>
+    <xsl:param name="textID"/>
+    <xsl:call-template name="xsl:initial-template"/>
+  </xsl:template>-->
+
 </xsl:stylesheet>