Revision f656161abbc14b1a9e7f18a02a854a863411af3b authored by Lou Burnard on 01 December 2011, 18:28:57 UTC, committed by Lou Burnard on 01 December 2011, 18:28:57 UTC
1 parent 4b761f5
Raw File
mergeLang.xsl
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet 
    xmlns:xd="http://www.pnp-software.com/XSLTdoc"
    xmlns:s="http://www.ascc.net/xml/schematron" 
    xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:rng="http://relaxng.org/ns/structure/1.0"
    xmlns:teix="http://www.tei-c.org/ns/Examples"
    xmlns:tei="http://www.tei-c.org/ns/1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format"
    xmlns:edate="http://exslt.org/dates-and-times"
    xmlns:exsl="http://exslt.org/common"
    xmlns:estr="http://exslt.org/strings"
    exclude-result-prefixes="exsl estr edate teix fo a tei xs rng s xd" 
    extension-element-prefixes="edate exsl estr"
    version="1.0">

<xsl:output 
   method="xml"
   indent="yes"
   encoding="utf-8"
   cdata-section-elements="tei:eg"
   omit-xml-declaration="yes"/>

  <xsl:key name="IDENTS" use="@ident" match="tei:elementSpec"/>
  <xsl:key name="IDENTS" use="@ident" match="tei:classSpec"/>
  <xsl:key name="IDENTS" use="@ident" match="tei:macroSpec"/>

  <xsl:key name="IDENTS"   use="concat(ancestor::tei:elementSpec/@ident,'_',@ident)"
	   match="tei:elementSpec/tei:attList/tei:attDef"/>

  <xsl:key name="IDENTS"   use="concat(ancestor::tei:classSpec/@ident,'_',@ident)"
	   match="tei:classSpec/tei:attList/tei:attDef"/>

  <xsl:key name="IDENTS"   
	   use="concat(ancestor::tei:elementSpec/@ident,'_',ancestor::tei:attDef/@ident,'_',@ident)"
	   match="tei:elementSpec/tei:attList/tei:attDef/tei:valList/tei:valItem"/>

  <xsl:key name="IDENTS"   
	   use="concat(ancestor::tei:classSpec/@ident,'_',ancestor::tei:attDef/@ident,'_',@ident)"
	   match="tei:classSpec/tei:attList/tei:attDef/tei:valList/tei:valItem"/>


  <xsl:param name="verbose"/>
  <xsl:param name="newFile"/>
  <xsl:param name="newLang"/>
  <xsl:param name="source"/>
  <xsl:param name="overwrite">false</xsl:param>

  <xsl:output encoding="utf-8" indent="yes"/>

  <xsl:variable name="Original" select="/"/>

  <xsl:variable name="New" select="document($newFile)"/>

  <xsl:template match="/">
<xsl:if test="$verbose='true'">
<xsl:message>
Language: <xsl:value-of select="$newLang"/>
File: <xsl:value-of select="$newFile"/>
Source: <xsl:value-of select="$source"/>
Overwrite: <xsl:value-of select="$overwrite"/>
</xsl:message>
</xsl:if>
    <xsl:apply-templates/>
  </xsl:template>

<xsl:template match="@*|processing-instruction()|comment()|text()">
  <xsl:copy/>
</xsl:template>

<xsl:template match="*">
  <xsl:copy>
    <xsl:apply-templates
	select="*|@*|processing-instruction()|comment()|text()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="tei:gloss|tei:desc">

  <xsl:variable name="Name">
    <xsl:value-of select="local-name(.)"/>
  </xsl:variable>

  <xsl:choose>
    <xsl:when test="@xml:lang and not(@xml:lang=$newLang)">
      <xsl:copy-of select="."/>
    </xsl:when>
    <xsl:when test="@xml:lang=$newLang and $overwrite='false'">
      <xsl:copy-of select="."/>
    </xsl:when>
    <xsl:when test="@xml:lang=$newLang and $overwrite='true'"/>
    <xsl:otherwise>
      <xsl:copy-of select="."/>
      <xsl:variable name="this">
	<xsl:value-of select="normalize-space(.)"/>
      </xsl:variable>

      <xsl:variable name="glossy">
	<xsl:choose>
	  <xsl:when test="tei:gloss">true</xsl:when>
	  <xsl:otherwise>false</xsl:otherwise>
	</xsl:choose>
      </xsl:variable>
      <xsl:variable name="What">
	<xsl:choose>
	  <xsl:when test="parent::tei:attDef">
	    <xsl:value-of
		select="concat(ancestor::tei:elementSpec/@ident|ancestor::tei:classSpec/@ident,'_',../@ident)"/>
	  </xsl:when>
	  <xsl:when test="parent::tei:valItem">
	    <xsl:value-of
		select="concat(ancestor::tei:elementSpec/@ident|ancestor::tei:classSpec/@ident,'_',ancestor::tei:attDef/@ident,'_',../@ident)"/>
	  </xsl:when>
	  <xsl:otherwise>
	    <xsl:value-of select="../@ident"/>
	  </xsl:otherwise>
	</xsl:choose>
      </xsl:variable>

      <xsl:for-each select="$New">
	<xsl:for-each select="key('IDENTS',$What)">
	  <xsl:choose>
	    <xsl:when test="$Name='gloss' and tei:gloss and not(tei:desc)"/>
	    <xsl:when test="$Name='gloss' and tei:gloss">
	      <xsl:for-each select="tei:gloss">
	      <xsl:call-template name="bits">
		<xsl:with-param name="Name">
		  <xsl:value-of select="$Name"/>
		</xsl:with-param>
		<xsl:with-param name="this">
		  <xsl:value-of select="$this"/>
		</xsl:with-param>
	      </xsl:call-template>
	      </xsl:for-each>
	    </xsl:when>
	    <xsl:when test="$Name='desc' and tei:desc">
	      <xsl:for-each select="tei:desc">
	      <xsl:call-template name="bits">
		<xsl:with-param name="Name">
		  <xsl:value-of select="$Name"/>
		</xsl:with-param>
		<xsl:with-param name="this">
		  <xsl:value-of select="$this"/>
		</xsl:with-param>
	      </xsl:call-template>
	      </xsl:for-each>
	    </xsl:when>
	    <xsl:when test="$Name='desc' and $glossy = 'false' and
			    tei:gloss">
	      <xsl:for-each select="tei:gloss">
	      <xsl:call-template name="bits">
		<xsl:with-param name="Name">
		  <xsl:value-of select="$Name"/>		  
		</xsl:with-param>
		<xsl:with-param name="this">
		  <xsl:value-of select="$this"/>
		</xsl:with-param>
	      </xsl:call-template>
	      </xsl:for-each>
	    </xsl:when>
	  </xsl:choose>
	</xsl:for-each>
      </xsl:for-each>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="bits">
  <xsl:param name="Name"/>
  <xsl:param name="this"/>
  <xsl:if test="@xml:lang=$newLang">
    <xsl:variable name="that">
      <xsl:choose>
	<xsl:when test="starts-with(.,'(')">
	  <xsl:value-of select="substring-before(substring-after(normalize-space(.),'('),')')"/>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:value-of select="normalize-space(.)"/>
	</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <!--<xsl:message>look for <xsl:value-of select="$What"/> giving <xsl:value-of select="$that"/></xsl:message>-->
    <xsl:if test="not($that=$this)">
      <xsl:variable name="date">
	<xsl:choose>
	  <xsl:when test="@notBefore">
	    <xsl:value-of select="@notBefore"/>
	  </xsl:when>
	  <xsl:otherwise>
	    <xsl:value-of
		select="substring-before(edate:date-time(),'T')"/>
	  </xsl:otherwise>
	</xsl:choose>
      </xsl:variable>
      <xsl:element name="{$Name}"
		   xmlns="http://www.tei-c.org/ns/1.0">
	<xsl:attribute name="version">
	  <xsl:value-of select="$date"/>
	</xsl:attribute>
	<xsl:attribute name="xml:lang">
	  <xsl:value-of select="$newLang"/>
	</xsl:attribute>
	<xsl:apply-templates/>
      </xsl:element>
    </xsl:if>
  </xsl:if>
</xsl:template>


<xsl:template match="tei:remarks">
  <xsl:choose>
    <xsl:when test="not(@xml:lang)">
      <xsl:copy-of select="."/>
    </xsl:when>
    <xsl:when test="$overwrite='false'">
      <xsl:copy-of select="."/>
    </xsl:when>
    <xsl:when test="not(@xml:lang=$newLang)">
      <xsl:copy-of select="."/>
    </xsl:when>
  </xsl:choose>
  <xsl:if test="not(preceding-sibling::tei:remarks)">
    <xsl:variable name="this">
      <xsl:value-of select="normalize-space(.)"/>
    </xsl:variable>
    <xsl:variable name="What">
      <xsl:choose>
	<xsl:when test="parent::tei:attDef">
	  <xsl:value-of select="ancestor::tei:elementSpec/@ident|ancestor::tei:classSpec/@ident"/>
	  <xsl:text>_</xsl:text>
	  <xsl:value-of select="../@ident"/>
	</xsl:when>
	<xsl:when test="parent::tei:classSpec">
	  <xsl:value-of select="../@ident"/>
	</xsl:when>
	<xsl:when test="parent::tei:elementSpec">
	  <xsl:value-of select="../@ident"/>
	</xsl:when>
	<xsl:when test="parent::tei:macroSpec">
	  <xsl:value-of select="../@ident"/>
	</xsl:when>
      </xsl:choose>
    </xsl:variable>
    <xsl:for-each select="$New">
      <xsl:for-each select="key('IDENTS',$What)/tei:remarks">
	<xsl:if test="@xml:lang=$newLang">
	<xsl:variable name="that">
	  <xsl:value-of select="normalize-space(.)"/>
	</xsl:variable>
	<xsl:if test="not($that=$this) and not($that='')">
	  <remarks xmlns="http://www.tei-c.org/ns/1.0">
	    <xsl:attribute name="xml:lang">
	      <xsl:value-of select="$newLang"/>
	    </xsl:attribute>
	    <xsl:apply-templates/>
	  </remarks>
	</xsl:if>
	</xsl:if>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:if>
</xsl:template>

<xsl:template match="tei:exemplum">
  <xsl:choose>
    <xsl:when test="not(@xml:lang)">
      <xsl:copy-of select="."/>
    </xsl:when>
    <xsl:when test="not(@xml:lang=$newLang)">
      <xsl:copy-of select="."/>
    </xsl:when>
  </xsl:choose>
    <xsl:variable name="What">
      <xsl:choose>
	<xsl:when test="parent::tei:attDef">
	  <xsl:value-of select="ancestor::tei:elementSpec/@ident|ancestor::tei:classSpec/@ident"/>
	  <xsl:text>_</xsl:text>
	  <xsl:value-of select="../@ident"/>
	</xsl:when>
	<xsl:when test="parent::tei:classSpec">
	  <xsl:value-of select="../@ident"/>
	</xsl:when>
	<xsl:when test="parent::tei:elementSpec">
	  <xsl:value-of select="../@ident"/>
	</xsl:when>
	<xsl:when test="parent::tei:macroSpec">
	  <xsl:value-of select="../@ident"/>
	</xsl:when>
      </xsl:choose>
    </xsl:variable>
  <xsl:if test="not(preceding-sibling::tei:exemplum)">
    <xsl:for-each select="$New">
      <xsl:for-each select="key('IDENTS',$What)">
	<xsl:for-each select="teix:egXML[@xml:lang=$newLang]">
	  <exemplum xmlns="http://www.tei-c.org/ns/1.0"
		    xml:lang="{$newLang}">
	    <egXML xmlns="http://www.tei-c.org/ns/Examples">
	    <xsl:if test="@n">
	      <xsl:attribute name="corresp">
		<xsl:text>#bibl</xsl:text>
		<xsl:value-of select="$newLang"/>
		<xsl:text>_</xsl:text>
		<xsl:value-of select="@n"/>
	      </xsl:attribute>
	    </xsl:if>
	      <xsl:apply-templates mode="iden"/>
	    </egXML>
	  </exemplum>
	</xsl:for-each>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:if>
</xsl:template>

<xsl:template 
    match="@xml:id" 
    mode="iden">
  <xsl:attribute name="xml:id">
    <xsl:value-of select="concat($newLang,'_')"/>
    <xsl:value-of select="."/>
  </xsl:attribute>
</xsl:template>

<xsl:template 
    match="@active|@children|@from|@mergedIn|@mutual|@origin|@parent|@ref|@render|@replacementPattern|@resp|@scheme|@since|@spanTo|@target|@targets|@who|@wit"
    mode="iden">
  <xsl:choose>
    <xsl:when test="starts-with(.,'#')">
      <xsl:attribute name="{local-name(.)}">
	<xsl:call-template name="mangle">
	  <xsl:with-param name="text">
	    <xsl:value-of select="substring-after(.,'#')"/>
	  </xsl:with-param>
	</xsl:call-template>
      </xsl:attribute>
    </xsl:when>
    <xsl:otherwise>
      <xsl:copy-of select="."/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template 
    match="@*|text()|comment()|processing-instruction()"  
    mode="iden">
 <xsl:copy-of select="."/>
</xsl:template>


<xsl:template match="*" mode="iden">
  <xsl:copy>
    <xsl:apply-templates 
	select="*|@*|processing-instruction()|comment()|text()"
	mode="iden"/>
  </xsl:copy>
</xsl:template>

<xsl:template name="mangle">
  <xsl:param name="text"/>
  <xsl:text>#</xsl:text>
  <xsl:value-of select="concat($newLang,'_')"/>
  <xsl:choose>
    <xsl:when test="contains($text,' #')">
      <xsl:value-of select="substring-before($text,'#')"/>
      <xsl:call-template name="mangle">
	<xsl:with-param name="text">
	  <xsl:value-of select="substring-after($text,'#')"/>
	</xsl:with-param>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$text"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


</xsl:stylesheet>
back to top