<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="/pool/xslt_ht.xslt" type="application/xml"?>
<xsl:stylesheet
  xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
  xmlns:d = "http://herbaer.de/xmlns/20051201/doc"
  xmlns:lt = "http://herbaer.de/xmlns/20151212/loctext/"
  xmlns = "http://herbaer.de/xmlns/20151212/loctext/"
  exclude-result-prefixes = "d lt"
  version = "1.0"
>
<d:info xmlns="http://herbaer.de/xmlns/20051201/doc">
  <title>lt_merge.xslt</title>
  <subtitle>Übersetzte Texte einfügen</subtitle>
  <date>2015-12-17</date>
  <author>
    <personname>
      <firstname>Herbert</firstname>
      <surname>Schiemann</surname>
    </personname>
    <email>h.schiemann@herbaer.de</email>
  </author>
</d:info>

<d:section xmlns="http://herbaer.de/xmlns/20051201/doc" role="stylesheet">
<para>
Diese Transformation fügt weitere Sprachversionen in
<tag class="element">lt:v</tag>-Elemente ein.
</para>
</d:section>

<d:para>
Dateipfad des Dokuments mit den neuen Sprachversionen
</d:para>
<xsl:param name="p_fnnew" select="''"/>

<d:para>
Werden existierende Sprachvarianten ersetzt?
Mögliche Werte:
<d:literal>replace</d:literal> oder <d:literal>keep</d:literal>
</d:para>
<xsl:param name="p_replace" select="'replace'"/>

<d:para>
Das Wurzelelement der neuen Sprachversionen
</d:para>
<xsl:variable name="g_newlang" select="document($p_fnnew)/lt:g"/>

<d:para>
Zeilenende: $txt.break
</d:para>
<xsl:include href="/pool/txt.xslt"/>

<d:para>Die Wurzel</d:para>
<xsl:template match="/">
  <xsl:apply-templates select="comment() | processing-instruction() | *"/>
</xsl:template>

<d:para>Elemente werden kopiert</d:para>
<xsl:template match="*">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates select="comment() | processing-instruction() | text() | *"/>
  </xsl:copy>
</xsl:template>

<d:para>Kommentare, Verarbeitungsanweisungen und Text werden kopiert</d:para>
<xsl:template match="comment() | processing-instruction() | text()">
  <xsl:copy-of select="."/>
</xsl:template>

<d:para>Attribute werden kopiert</d:para>
<xsl:template match="@*">
  <xsl:copy-of select="."/>
</xsl:template>

<d:para>Verschiedene Sprachversionen eines Textes</d:para>
<xsl:template match="lt:v">
  <xsl:variable name="id">
    <xsl:choose>
      <xsl:when test="@id">
        <xsl:value-of select="@id"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="count (preceding::lt:v) + 1"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="nt" select="$g_newlang/lt:v[@id = $id]"/>
  <v>
    <xsl:apply-templates select="@*"/>
    <xsl:choose>
      <xsl:when test="$p_replace = 'keep'">
        <xsl:apply-templates select="lt:t"/>
        <xsl:apply-templates select="$nt/lt:d">
          <xsl:with-param name="t" select="."/>
        </xsl:apply-templates>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="lt:t">
          <xsl:with-param name="nt" select="$nt"/>
        </xsl:apply-templates>
        <xsl:apply-templates select="$nt/lt:d"/>
      </xsl:otherwise>
    </xsl:choose>
  </v>
</xsl:template>

<d:para>
Existierende Sprachvarianten werden kopiert
</d:para>
<xsl:template match="lt:t">
  <xsl:param name="nt"/>
  <xsl:variable name="l" select="@l"/>
  <xsl:choose>
    <xsl:when test="$nt and $nt/lt:d[@l = $l]"/>
    <xsl:otherwise>
      <!--
      <xsl:copy-of select = "."/>
      -->
      <t>
        <xsl:apply-templates select="@*"/>
        <xsl:apply-templates select="text()"/>
      </t>
      <xsl:value-of select="$txt.break"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<d:para>
Neue Sprachvarianten werden als
<d:tag class="element">lt:t</d:tag>-Element
eingefügt
</d:para>
<xsl:template match="lt:d">
  <xsl:param name="t"/>
  <xsl:variable name="l" select="@l"/>
  <xsl:choose>
    <xsl:when test="$t and $t/lt:t[@l = $l]"/>
    <xsl:otherwise>
      <t l="{@l}" tr="{lt:trname}"><xsl:value-of select="lt:text"/></t>
      <xsl:value-of select="$txt.break"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>
