many fixes, structure.xml all done, retaining comments with the surface forms for now

Change-Id: Ia16081bc629271ec8570c4e86ee081fb53b893aa
diff --git a/nkjp2korap.xsl b/nkjp2korap.xsl
index c001203..d10ea89 100644
--- a/nkjp2korap.xsl
+++ b/nkjp2korap.xsl
@@ -24,6 +24,9 @@
           no functionality beyond string identity is supported
           (this is just for testing)  -->
   
+  <xsl:param name="SHOW_ORTH_IN_STRUCT" as="xs:boolean" select="true()"/>
+  <!-- for debugging structure.xml production -->
+  
 
 <!--          VARIABLES (= constants...)            -->
 
@@ -51,37 +54,16 @@
 
   <xsl:variable name="collection_of_text" select="collection($sourceDir || '?' || $collection_params)" as="document-node()+"/>
   
-<!-- these two 'flags' are meant to increase the readability of the code
-     they are used for the output of the calc_offsets() function, where the 
-     returned value is a sequence, (start, end) 
-  
-  remove together with the function!
-  
-  --> 
-  <xsl:variable name="OFFSET_START" as="xs:integer" static="yes" select="1"/>
-  <xsl:variable name="OFFSET_END" as="xs:integer" static="yes" select="2"/>
-
-  <xsl:variable name="INCOMPLETE_MAP" as="xs:integer" static="yes" select="-1"/>
-<!-- this is for travering the accumulator sequences, and skipping incomplete maps -->
-
-
 <!--           MODES               -->
 
   <xsl:mode name="corpus" on-no-match="deep-skip"/>
   <xsl:mode name="text" on-no-match="deep-skip"/>
   <xsl:mode name="header-text" on-no-match="text-only-copy"/>
   <xsl:mode use-accumulators="#all"/>
-  
-  <!--<xsl:function name="f:grab_the_tail" as="map(*)">
-    <xsl:param name="seq" as="map(*)+"/>
-    <xsl:choose>
-      <xsl:when test="map:get(head($seq),map:keys(head($seq))[1])[2] eq $INCOMPLETE_MAP">
-        <xsl:sequence select="f:grab_the_tail(tail($seq))"/>
-        <xsl:message select="'skip ' || map:keys(head($seq))[1]"/>
-      </xsl:when>
-      <xsl:otherwise><xsl:sequence select="$seq"/></xsl:otherwise>
-    </xsl:choose>
-  </xsl:function>-->
+
+  <xsl:accumulator name="element-index" as="xs:integer" initial-value="0">
+    <xsl:accumulator-rule match="tei:*[ancestor-or-self::tei:text]" select="$value + 1" phase="start"/>
+  </xsl:accumulator>
   
   <xsl:accumulator name="elem-offset-seq" as="map(xs:string, item()+)+" initial-value="(map{'null':(0,0)})">
  
@@ -89,14 +71,11 @@
       <xsl:variable name="preceding_index" as="xs:integer">
         <xsl:variable name="the_tail" as="map(*)" select="head(reverse($value))"/>
         <xsl:sequence select="map:get($the_tail,map:keys($the_tail)[1])[2]"/>
-        <xsl:message select="local-name() || ' below ' || parent::node()/@xml:id ||' start, previous element: ' || map:keys($the_tail)[1]"></xsl:message>
       </xsl:variable>
       
       <xsl:variable name="our_base" as="xs:integer" select="if($preceding_index eq 0) then $preceding_index else $preceding_index + 1"/>
       <!--  for paragraphs, it's in either being initial or not    -->
-      
-      <xsl:message select="@xml:id || ' start, preceding_index: ' || $preceding_index || ',  our_base: ' || $our_base"/>
-      
+
       <xsl:sequence select="
         $value,
         map {
@@ -108,12 +87,9 @@
       <xsl:variable name="preceding_index" as="xs:integer">
         <xsl:variable name="the_tail" as="map(*)" select="head(reverse($value))"/>
         <xsl:sequence select="map:get($the_tail,map:keys($the_tail)[1])[2]"/>
-        <xsl:message select="'s start, previous element: ' || map:keys($the_tail)[1]"></xsl:message>
       </xsl:variable>
-
       <xsl:variable name="our_base" as="xs:integer" select="if($preceding_index eq 0) then $preceding_index else $preceding_index + + xs:integer(f:is_preceded_by_ws(.,true()))"/>
-      <xsl:message select="@xml:id || ' start, preceding_index: ' || $preceding_index || ',  our_base: ' || $our_base"/>
-      
+
       <xsl:sequence select="
         $value,
         map {
@@ -125,12 +101,9 @@
       <xsl:variable name="preceding_index" as="xs:integer">
         <xsl:variable name="the_tail" as="map(*)" select="head(reverse($value))"/>
         <xsl:sequence select="map:get($the_tail,map:keys($the_tail)[1])[2]"/>
-        <xsl:message select="'w, previous element: ' || map:keys($the_tail)[1]"></xsl:message>
       </xsl:variable>
       <xsl:variable name="our_base" as="xs:integer" select="$preceding_index + xs:integer(f:is_preceded_by_ws(parent::tei:seg,true()))"/>
-      
-      <xsl:message select="'w, preceding_index: ' || $preceding_index || ',  our_base: ' || $our_base"/>
-      <!--<xsl:message select="('VALUE at w-end:',serialize($value, map{'method':'adaptive'}))"/>-->
+
       <xsl:sequence select="
           $value,
           map {
@@ -142,17 +115,13 @@
       <xsl:variable name="preceding_index" as="xs:integer">
         <xsl:variable name="the_tail" as="map(*)" select="head(reverse($value))"/>
         <xsl:sequence select="map:get($the_tail,map:keys($the_tail)[1])[2]"/>
-        
-        <!--<xsl:message select="('VALUE at s-end:',serialize($value, map{'method':'adaptive'}))"/>-->
-        
+
       </xsl:variable>
       <xsl:variable name="our_base" as="xs:integer">
         <xsl:variable name="incomplete" select="map:find($value,string(@xml:id))(1)" as="xs:integer+"/>
         <xsl:sequence select="$incomplete[2]"/>
       </xsl:variable>
-      
-      <xsl:message select="'s end, preceding_index: ' || $preceding_index || ',  our_base: ' || $our_base"/>
-      
+
       <xsl:sequence select="
         $value,
         map {
@@ -165,16 +134,12 @@
         <xsl:variable name="the_tail" as="map(*)" select="head(reverse($value))"/>
         <xsl:sequence select="map:get($the_tail,map:keys($the_tail)[1])[2]"/>
         
-        <!--<xsl:message select="('VALUE at p-end:',serialize($value, map{'method':'adaptive'}))"/>-->
-        
       </xsl:variable>
       <xsl:variable name="our_base" as="xs:integer">
         <xsl:variable name="incomplete" select="map:find($value,string(@xml:id))(1)" as="xs:integer+"/>
         <xsl:sequence select="$incomplete[2]"/>
       </xsl:variable>
-      
-      <xsl:message select="'p end, preceding_index: ' || $preceding_index || ',  our_base: ' || $our_base"/>
-      
+
       <xsl:sequence select="
         $value,
         map {
@@ -227,24 +192,21 @@
    <xsl:when test="local-name($node) eq 'seg'">
       <xsl:choose>
         <xsl:when test="$node/@nkjp:nps"><xsl:sequence select="fn:false()"/></xsl:when>
+        <xsl:when test="$node/preceding::tei:seg[count(@nkjp:rejected) eq 0][ancestor::tei:s[descendant::tei:seg = $node]]"><xsl:sequence select="fn:true()"/></xsl:when>
+       <!--  this is checking if we're preceding by a seg under the same tei:s, modulo choice/paren      -->
+       <!-- THIS CHECK should be streamlined for efficiency, maybe try comparing positions in the descendant axis    -->
         <xsl:otherwise>
           <xsl:sequence
             select="not($suppress_initial) and not($node[count(preceding-sibling::tei:seg) eq 0]/ancestor::tei:s[count(preceding-sibling::tei:s) eq 0]/ancestor::tei:p[count(preceding-sibling::tei:p) eq 0])"
           />  
         </xsl:otherwise>
       </xsl:choose>
-      
-        <!--<xsl:sequence
-          select="not(exists($node/@nkjp:nps)) and not($node[count(preceding-sibling::tei:seg) eq 0]/ancestor::tei:s[count(preceding-sibling::tei:s) eq 0]/ancestor::tei:p[count(preceding-sibling::tei:p) eq 0])"
-        />-->
-      <!--and not($node/ancestor::tei:s[count(preceding-sibling::tei:s) eq 0]/ancestor::tei:p[count(preceding-sibling::tei:p) eq 0])-->
     
     </xsl:when>
     <xsl:when test="local-name($node) eq 's'">
       <xsl:choose>
         <xsl:when test="exists($node/preceding-sibling::tei:s)"><xsl:sequence select="fn:true()"/></xsl:when>
         <xsl:otherwise>
-          <xsl:message select="'.                   s indented: ' || xs:string(not($suppress_initial) and exists($node/ancestor::tei:p[1]/preceding-sibling::tei:p))"></xsl:message>
           <xsl:sequence select="not($suppress_initial) and exists($node/ancestor::tei:p[1]/preceding-sibling::tei:p)"/>    
         </xsl:otherwise>
       </xsl:choose>
@@ -269,9 +231,9 @@
 <!-- NKJP-SGJP has apparently resigned from standoff representations by adding <w> everywhere;
      we reach for them, but from the level of <seg>, so we don't need to process <w> separately -->
 
-    <!--fall-thru, skipping the potential <paren> element -->
+    <!-- fall-thru, skipping the potential <paren> element and filtering out the bad guys -->
   <xsl:template match="tei:choice" mode="struct">
-    <xsl:apply-templates select="descendant::tei:seg"/>
+    <xsl:apply-templates select="descendant::tei:seg[count(@nkjp:rejected) eq 0]" mode="struct"/>
   </xsl:template>
 
   <!--             MAIN PROCESSING              -->
@@ -349,18 +311,17 @@
       <xsl:with-param name="target" select="$targetBaseDir ||  '/nkjp/morpho.xml'" as="xs:string"/>
     </xsl:call-template>
 -->
-    <!--<xsl:call-template name="create_text_header">
+    <xsl:call-template name="create_text_header">
       <xsl:with-param name="text.xml" select="$text.xml" as="document-node()"/>
       <xsl:with-param name="compoundID" select="$compoundID" as="xs:string"/>
       <xsl:with-param name="target" select="$targetBaseDir ||  '/header.xml'" as="xs:string"/>
-    </xsl:call-template>-->
+    </xsl:call-template>
 
   </xsl:template>
 
   <!--   **************************        data.xml       *******************                -->
 
   <xsl:template name="create_data">
-    <!--<xsl:param name="text.xml" as="document-node()"/>-->
     <xsl:param name="ann_segmentation.xml" as="document-node()"/>
     <xsl:param name="compoundID" as="xs:string"/>
     <xsl:param name="target" as="xs:string"/>
@@ -406,106 +367,37 @@
         <xsl:attribute name="version" select="$KorAP-XML_version"/>
 
         <xsl:element name="spanList" namespace="{$KorAP_namespace}">
-          <xsl:apply-templates select="$ann_segmentation.xml//tei:text" mode="struct">
-            <!--<xsl:with-param name="offsets" as="map(xs:string, xs:integer+)" tunnel="yes">
-              <xsl:map>
-                <xsl:for-each select="tail(fn:accumulator-after('elem-offset-seq'))">
-                  <xsl:map-entry key="map:get(., 'id')" select="map:get(., 'start'), map:get(., 'end')"/>
-                </xsl:for-each>
-              </xsl:map>
-            </xsl:with-param>-->
-          </xsl:apply-templates>          
+          <xsl:apply-templates select="$ann_segmentation.xml//tei:text" mode="struct"/>          
         </xsl:element>
       </xsl:element>
     </xsl:result-document>
   </xsl:template>
   
   <xsl:template match="tei:*" mode="struct">
-    <xsl:param name="ini" as="xs:integer" required="no" select="0"/>
-    <xsl:param name="fin" as="xs:integer" required="no" select="999999999"/>
-    <xsl:param name="index" as="xs:integer" required="no" select="1"/>
-
-    <xsl:variable name="offsets" as="map(xs:string, xs:integer+)"
-      select="map:merge(tail(fn:accumulator-after('elem-offset-seq')),map{'duplicates':'use-last'})"/>
+    <xsl:variable name="offsets" as="xs:integer+">
+      <xsl:variable name="akku"
+        select="map:merge(tail(fn:accumulator-after('elem-offset-seq')), map {'duplicates': 'use-last'})"
+        as="map(xs:string,xs:integer+)"/>  <!--test later if the merger is really needed here, given how accus work-->
+      <xsl:sequence select="map:get($akku, string(@xml:id))"/>
+    </xsl:variable>
     
     <xsl:variable name="my_name" select="local-name()" as="xs:string"/>
-    <xsl:variable name="preceding" select="preceding-sibling::*[local-name(.) eq $my_name]"/>
-    <xsl:variable name="preceding-count" select="count($preceding)"/>
-
-    <xsl:variable name="outside-preceding-count" as="xs:integer">
-      <xsl:choose>
-        <xsl:when test="self::tei:s or self::tei:p">
-          <xsl:choose>
-            <xsl:when test="$preceding-count">
-              <xsl:sequence select="
-                  sum(for $p in $preceding
-                  return
-                    count($p/descendant::*))"/>       <!--mind @nkjp:rejected -->
-            </xsl:when>
-            <xsl:otherwise>
-              <xsl:sequence select="0"/>
-            </xsl:otherwise>
-          </xsl:choose>
-        </xsl:when>
-        <xsl:otherwise>
-          <xsl:sequence select="0"/>
-        </xsl:otherwise>
-      </xsl:choose>
-    </xsl:variable>
-
-    <xsl:variable name="my_index" select="$index + 1 + $preceding-count + $outside-preceding-count"
-      as="xs:integer"/>
-
-    <xsl:variable name="start" as="xs:integer">
-      <xsl:choose>
-        <xsl:when test="self::tei:text or self::tei:body">
-          <xsl:sequence select="0"/>
-        </xsl:when>
-        <xsl:when test="self::tei:p">
-          <xsl:variable name="first_corresp"
-            select="descendant::tei:s[1]/descendant::tei:seg[1]/attribute::corresp"
-            as="attribute(corresp)"/>
-          <xsl:variable name="numbers" select="substring-after(substring-before($first_corresp,')'),',')"/>
-          <xsl:sequence select="xs:integer(substring-before($numbers,','))"/>
-        </xsl:when>
-        <xsl:when test="self::tei:s">
-          <xsl:variable name="first_corresp"
-            select="descendant::tei:seg[1]/attribute::corresp"
-            as="attribute(corresp)"/>
-          <xsl:variable name="numbers" select="substring-after(substring-before($first_corresp,')'),',')"/>
-          <xsl:sequence select="xs:integer(substring-before($numbers,','))"/>
-        </xsl:when>
-        <xsl:when test="self::tei:seg">
-          <xsl:variable name="numbers" select="substring-after(substring-before(@corresp,')'),',')"/>
-          <xsl:sequence select="xs:integer(substring-before($numbers,','))"/>
-        </xsl:when>
-        <xsl:otherwise>
-          <xsl:message terminate="yes" select="'Element not handled: ' || fn:local-name()"/>
-        </xsl:otherwise>
-      </xsl:choose>
-    </xsl:variable>
-    
-    <xsl:variable name="end" as="xs:integer" select="f:calc_content_length(.)">
-    </xsl:variable>
+    <xsl:variable name="my_index" select="fn:accumulator-before('element-index')" as="xs:integer"/>
     
     <xsl:element name="span" namespace="{$KorAP_namespace}">
       <xsl:attribute name="id" select="'s' || $my_index"/>
-      <xsl:attribute name="from" select="map:get($offsets,string(@xml:id))[1]"/>
-      <xsl:attribute name="to" select="map:get($offsets,string(@xml:id))[2]"/>
-<!--      <xsl:attribute name="accumulator" select="string-join(map:get($offsets,string(@xml:id)),',')"/>  <!-\-test-\->-->
+      <xsl:attribute name="from" select="$offsets[1]"/>
+      <xsl:attribute name="to" select="$offsets[2]"/>
       <xsl:attribute name="l" select="f:compute_nesting(.)"/>
+      <xsl:if test="local-name() eq 'seg' and $SHOW_ORTH_IN_STRUCT">
+        <xsl:comment><xsl:value-of select="fn:normalize-space(.)"/></xsl:comment>
+      </xsl:if>
       <xsl:element name="fs" namespace="http://www.tei-c.org/ns/1.0">
         <xsl:attribute name="type" select="'struct'"></xsl:attribute>  <!-- STRUCT vs. LEX for morpho -->
         <xsl:element name="f" namespace="http://www.tei-c.org/ns/1.0">
           <xsl:attribute name="name" select="'name'"/>
           <xsl:value-of select="local-name()"/>
         </xsl:element>
-        <xsl:if test="local-name() eq 'seg'">                                                     <!--test-->
-          <xsl:element name="f" namespace="http://www.tei-c.org/ns/1.0">
-            <xsl:attribute name="name" select="'orth'"/>
-            <xsl:value-of select="fn:normalize-space(.)"/>
-          </xsl:element>
-        </xsl:if>
         <xsl:if test="count(@*)">
           <xsl:element name="f" namespace="http://www.tei-c.org/ns/1.0">
             <xsl:attribute name="name" select="'attr'"/>
@@ -523,9 +415,7 @@
       </xsl:element>
     </xsl:element>
     <xsl:apply-templates mode="struct">
-      <xsl:with-param name="ini" select="$start" as="xs:integer"/>
-      <xsl:with-param name="fin" select="$end" as="xs:integer"/>
-      <xsl:with-param name="index" select="$my_index"/>
+      <!--<xsl:with-param name="index" select="$my_index"/>-->
     </xsl:apply-templates>
   </xsl:template>
 
@@ -557,7 +447,6 @@
   <xsl:template match="tei:*" mode="morpho">
     <xsl:param name="ini" as="xs:integer" required="no" select="0"/>
     <xsl:param name="fin" as="xs:integer" required="no" select="999999999"/>
-    <xsl:param name="index" as="xs:integer" required="no" select="1"/>
     <xsl:param name="ann_morphosyntax.xml" as="document-node()"/>
     <xsl:variable name="my_name" select="local-name()" as="xs:string"/>
     <xsl:variable name="preceding" select="preceding-sibling::*[local-name(.) eq $my_name]"/>
@@ -582,8 +471,7 @@
         </xsl:otherwise>
       </xsl:choose>
     </xsl:variable>
-    <xsl:variable name="my_index" select="$index + 1 + $preceding-count + $outside-preceding-count"
-      as="xs:integer"/>
+    <xsl:variable name="my_index" select="fn:accumulator-before('element-index')" as="xs:integer"/>
     
     <xsl:variable name="start" as="xs:integer">
       <xsl:choose>
@@ -616,7 +504,6 @@
     <xsl:apply-templates mode="morpho">
       <xsl:with-param name="ini" select="$start" as="xs:integer"/>
       <xsl:with-param name="fin" select="$end" as="xs:integer"/>
-      <xsl:with-param name="index" select="$my_index"/>
       <xsl:with-param name="ann_morphosyntax.xml" select="$ann_morphosyntax.xml" as="document-node()"/>
     </xsl:apply-templates>
   </xsl:template>
@@ -937,189 +824,6 @@
     </xsl:choose>
   </xsl:function>
 
-  <xsl:function name="f:calc_offsets" as="xs:integer+">
-    <xsl:param name="node" as="element()"/>
-    <xsl:param name="skip_start" as="xs:boolean" />
-    
-    <xsl:variable name="start" as="xs:integer">
-      <xsl:choose>
-        
-        <xsl:when test="$skip_start or $node/self::tei:text or $node/self::tei:body">
-          <xsl:sequence select="0"/>
-        </xsl:when>
-        
-        <!--        handle p -->
-        
-        <xsl:when test="$node/self::tei:p">
-          <xsl:variable name="my_pos" as="xs:integer" select="count($node/preceding-sibling::tei:p) + 1"/>
-          <xsl:variable name="preceding" as="node()*"
-            select="$node/ancestor::tei:body/tei:p[position() lt $my_pos]"/>
-          
-          <xsl:choose>
-            <xsl:when test="count($preceding) eq 0">
-              <xsl:sequence select="0"/>
-            </xsl:when>
-            <xsl:otherwise>
-              <xsl:sequence select="sum(f:calc_offsets($preceding[last()],true())[$OFFSET_END],1)"/>
-              
-              <!--  BUG danger: I am not sure if a "1" should rather be added after each p; let me try to handle that in the return value of the $length variable,
-              and make it sensitive to the skip_start parameter 
-              
-              I will then have to remove the ",1" from here!
-              
-              -->
-              
-              <!--              <xsl:variable name="last_corresps"
-                select="$preceding/descendant::tei:s[last()]/(descendant::tei:seg[count(@nkjp:rejected) eq 0 or @nkjp:rejected ne 'true'])[last()]/@corresp"
-                as="attribute(corresp)+"/>
-              <xsl:variable name="end_offsets" as="xs:integer+">
-                <xsl:for-each select="$last_corresps">
-                  <xsl:variable name="numbers"
-                    select="substring-after(substring-before(., ')'), ',')"/>
-                  <xsl:sequence
-                    select="xs:integer(substring-before($numbers, ',')) + xs:integer(substring-after($numbers, ','))"
-                  />
-                </xsl:for-each>
-              </xsl:variable>
-              <xsl:sequence select="sum($end_offsets, 1)"/>
-              
-              this is a non-recursive variant that may turn out to be much less cpu-intensive, not sure
-              - but if it's plugged in, it will have to be adjusted to the current form of the recursive variant,
-                because it hasn't been maintained since it got commented out
-              -->
-            </xsl:otherwise>
-          </xsl:choose>
-        </xsl:when>
-        
-        <!--        handle s -->
-        
-        <!-- the value for s gets counted since the start of the current p 
-        - so we look at the preceding s's
-                      + the preceding p's
-        -->
-        <xsl:when test="$node/self::tei:s">
-          <!--<xsl:variable name="last_corresp"
-            select="$node/descendant::tei:seg[count(@nkjp:rejected) eq 0 or @nkjp:rejected ne 'true'][last()]/attribute::corresp"
-            as="attribute(corresp)"/>
-          <xsl:variable name="numbers"
-            select="substring-after(substring-before($last_corresp, ')'), ',')"/>
-          <xsl:sequence
-            select="xs:integer(substring-before($numbers, ',')) + xs:integer(substring-after($numbers, ','))"
-          />
-          -->
-          
-          <xsl:variable name="internal_start" as="xs:integer">
-            <xsl:variable name="my_pos" as="xs:integer" select="count($node/preceding-sibling::tei:s) + 1"/>
-            <xsl:variable name="preceding" as="node()*"
-              select="$node/ancestor::tei:p[1]/tei:s[position() lt $my_pos]"/>
-            
-            <xsl:choose>
-              <xsl:when test="count($preceding) eq 0">
-                <xsl:sequence select="0"/>
-              </xsl:when>
-              <xsl:otherwise>
-                <xsl:sequence select="f:calc_offsets($preceding[last()],true())[$OFFSET_END]"/>
-                <!--<xsl:sequence select="sum(f:calc_offsets($preceding[last()],true())[$OFFSET_END],1)"/>-->
-                <!--   again, CAREFUL ABOUT THE +1, it might need to vanish         -->
-              </xsl:otherwise>
-            </xsl:choose>
-          </xsl:variable>
-          
-          <xsl:variable name="external_start" as="xs:integer" select="f:calc_offsets($node/ancestor::tei:p[1],false())[$OFFSET_START]"/>
-          
-          <xsl:sequence select="$internal_start + $external_start"/>
-        </xsl:when>
-        
-        <!--        handle seg -->        
-        
-        <xsl:when test="$node/self::tei:seg">
-          <!--   for segs, the s elements are irrelevant, and the local offset is immediately available on the @corresp       -->
-          
-          <xsl:variable name="numbers"
-            select="substring-after(substring-before($node/@corresp, ')'), ',')"/>
-          
-          <xsl:variable name="internal_start" select="xs:integer(substring-before($numbers, ','))"
-            as="xs:integer"/>
-          <xsl:variable name="external_start" as="xs:integer"
-            select="f:calc_offsets($node/ancestor::tei:p[1], false())[$OFFSET_START]"/>
-          
-          <xsl:if test="$node/self::tei:seg and count($node/@nkjp:rejected)">
-            
-            <xsl:message select="'numbers: ' || $numbers"/>
-          </xsl:if>
-          <xsl:sequence select="$internal_start + $external_start"/>
-        </xsl:when>
-      </xsl:choose>
-    </xsl:variable>
-    
-    <xsl:variable name="length" as="xs:integer">
-      <xsl:choose>
-        
-        <xsl:when test="$node/self::tei:text or $node/self::tei:body">
-          <xsl:variable name="last_corresps"
-            select="$node/descendant::tei:p/descendant::tei:s[last()]/(descendant::tei:seg[count(@nkjp:rejected) eq 0 or @nkjp:rejected ne 'true'])[last()]/@corresp"
-            as="attribute(corresp)+"/>
-          
-          <xsl:variable name="end_offsets" as="xs:integer+">
-            <xsl:for-each select="$last_corresps">
-              <xsl:variable name="numbers" select="substring-after(substring-before(., ')'), ',')"/>
-              <xsl:sequence
-                select="xs:integer(substring-before($numbers, ',')) + xs:integer(substring-after($numbers, ','))"
-              />
-            </xsl:for-each>
-          </xsl:variable>
-          
-          <xsl:sequence select="sum($end_offsets)"/>
-          
-        </xsl:when>
-        <xsl:when test="$node/self::tei:p">
-          <xsl:variable name="last_corresps"
-            select="$node/descendant::tei:s[last()]/(descendant::tei:seg[count(@nkjp:rejected) eq 0 or @nkjp:rejected ne 'true'])[last()]/@corresp"
-            as="attribute(corresp)+"/>
-          <xsl:variable name="end_offsets" as="xs:integer+">
-            <xsl:for-each select="$last_corresps">
-              <xsl:variable name="numbers" select="substring-after(substring-before(., ')'), ',')"/>
-              <xsl:sequence
-                select="xs:integer(substring-before($numbers, ',')) + xs:integer(substring-after($numbers, ','))"
-              />
-            </xsl:for-each>
-          </xsl:variable>
-          <xsl:sequence select="sum($end_offsets)"/>
-        </xsl:when>
-        
-        
-        
-        
-        <xsl:when test="$node/self::tei:s">
-          <xsl:variable name="last_corresp"
-            select="$node/descendant::tei:seg[count(@nkjp:rejected) eq 0 or @nkjp:rejected ne 'true'][last()]/attribute::corresp"
-            as="attribute(corresp)"/>
-          <xsl:variable name="numbers"
-            select="substring-after(substring-before($last_corresp, ')'), ',')"/>
-          <xsl:sequence
-            select="xs:integer(substring-before($numbers, ',')) + xs:integer(substring-after($numbers, ','))"
-          />
-        </xsl:when>
-        <xsl:otherwise>
-          <xsl:variable name="numbers"
-            select="substring-after(substring-before($node/@corresp, ')'), ',')"/>
-          <xsl:if test="$node/self::tei:seg and count($node/@nkjp:rejected)">
-            <!--     REMOVE THIS     -->
-            <xsl:message select="'rejected: ' || $numbers"/>
-          </xsl:if>
-          <xsl:sequence
-            select="xs:integer(substring-before($numbers, ',')) + xs:integer(substring-after($numbers, ','))"
-          />
-        </xsl:otherwise>
-      </xsl:choose>
-    </xsl:variable>
-    
-    <xsl:message select="local-name($node) || '[' || count($node/preceding-sibling::*[local-name() eq local-name($node)])+1 || '] length: ' || $length || '    skip_start: ' || $skip_start"/>
-    
-    <xsl:sequence select="$start, $start + $length -1 + xs:integer($skip_start)"/>
-  </xsl:function>
-
-
 </xsl:stylesheet>
 
 <!-- template for serializing maps in messages <xsl:message select="('map:',serialize($map, map{'method':'adaptive'}))"/> -->
\ No newline at end of file