view summa/bilingual/merge.xsl @ 54:77921d86a058

towards new format, pipeline
author ht
date Tue, 29 Dec 2009 18:40:48 +0000
parents d16e7ecf99ad
children bfebbcb9ddf1
line wrap: on
line source

<?xml version='1.0'?>
<!DOCTYPE doc SYSTEM "../../../../lib/xml/xsl.dtd" >
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" exclude-result-prefixes="x" xmlns:x="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml">
 
 <!-- Merge wrapped latin and prepped english to produce two-column
      bilingual for a single Summa Question -->
 <xsl:param name="lf">xyzzy</xsl:param>
 <xsl:variable name="latin" select="document($lf,/)/*"/>
 <xsl:key name="lq" match="x:div[@class='q']" use="@id"/>
 <xsl:variable name="di" select="/x:div/@id"/>
 <xsl:variable name="qi" select="concat('q',substring-after($di,'_Q'))"/>
 <xsl:variable name="la" select="key('lq',$qi,$latin)"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>
 
 <xsl:template match="x:div[@class='div4' and not(@id)]">
  <!-- Wrap a prooemium -->
  <xsl:copy>
   <xsl:call-template name="doBi">
    <xsl:with-param select="@*|node()" name="english"/>
    <xsl:with-param select="$la/x:div[@class='p']/x:p" name="latin"/>
   </xsl:call-template>   
  </xsl:copy>
 </xsl:template>
 
 <xsl:template match="x:div[@class='div4' and @id]">
  <!-- Wraps a single article within a question -->
  <xsl:variable name="n" select="count(preceding-sibling::x:div[@id])+1"/>
  <xsl:variable name="ld" select="$la/x:div[@class='a'][position()=$n]"/>
  <xsl:message><xsl:value-of select="@id"/>,<xsl:value-of select="$n"/>,<xsl:value-of select="x:h4"/>,<xsl:value-of select="count(x:p)"/>,<xsl:value-of select="$ld/x:h3"/>,<xsl:value-of select="count($ld/x:p)"/></xsl:message>
  <xsl:choose>
   <xsl:when test="count(x:p)=count($ld/x:p)">
    <xsl:copy>
     <xsl:apply-templates select="@*|x:h4"/>
     <xsl:for-each select="x:p">
      <xsl:variable name="i" select="position()"/>
      <xsl:call-template name="doBi">
       <xsl:with-param select="." name="english"/>
       <xsl:with-param select="$ld/x:p[position()=$i]" name="latin"/>
      </xsl:call-template>
     </xsl:for-each>
    </xsl:copy>
   </xsl:when>
   <xsl:otherwise>
    <xsl:message>Can't merge article <xsl:value-of select="@id"/>, <xsl:value-of select="count(x:p)"/>!=<xsl:value-of select="count($ld/x:p)"/></xsl:message>
    <!-- Try to split on keywords in English -->
    <xsl:variable name="pb" select="count(x:p/x:b[starts-with(.,'Objection') or (.='On the contrary,') or (.='I answer that,') or starts-with(.,'Reply to ')])"/>
    <xsl:choose>
     <xsl:when test="$pb=count($ld/x:p)">
      <xsl:for-each-group select="x:p" group-starting-with="x:p[x:b[starts-with(.,'Objection') or (.='On the contrary,') or (.='I answer that,') or starts-with(.,'Reply to ')]]">
       <xsl:variable name="i" select="position()"/>
       <xsl:call-template name="doBi">
        <xsl:with-param select="current-group()" name="english"/>
        <xsl:with-param select="$ld/x:p[position()=$i]" name="latin"/>
      </xsl:call-template>
      </xsl:for-each-group>
     </xsl:when>
     <xsl:otherwise>
      <xsl:message>Still can't merge article <xsl:value-of select="@id"/>, <xsl:value-of select="$pb"/>!=<xsl:value-of select="count($ld/x:p)"/></xsl:message>
     </xsl:otherwise>
    </xsl:choose>
   </xsl:otherwise>
  </xsl:choose>  
 </xsl:template>
 
 <xsl:template name="doBi">
  <xsl:param name="english"/>
  <xsl:param name="latin"/>
  <div class="bilingual">
   <xsl:choose>
    <xsl:when test="count($english)=1">
     <xsl:for-each select="$english">
      <xsl:copy>
       <xsl:attribute name="class">english</xsl:attribute>
       <xsl:apply-templates select="@*[not(local-name()='class')]|node()"/>
      </xsl:copy>
     </xsl:for-each>
    </xsl:when>
    <xsl:otherwise>
     <div class="english">
      <xsl:apply-templates select="$english"/>
     </div>
    </xsl:otherwise>
   </xsl:choose>
   <xsl:for-each select="$latin">
      <xsl:copy>
       <xsl:attribute name="class">latin</xsl:attribute>
       <xsl:apply-templates select="@*[not(local-name()='class')]|node()"/>
      </xsl:copy>
     </xsl:for-each>
  </div>
 </xsl:template>
</xsl:stylesheet>