<?xml version="1.0"?>
<?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:sk = "http://herbaer.de/xmlns/20150106/skeleton"
  xmlns:ti = "http://herbaer.de/xmlns/201500703/transinfo/"
  xmlns:xlf = "urn:oasis:names:tc:xliff:document:2.0"
  exclude-result-prefixes = "d sk xlf"
  version = "1.0"
>
<d:info xmlns="http://herbaer.de/xmlns/20051201/doc">
  <title>skeleton_merge_xliff.xslt</title>
  <subtitle>Skeleton und XLIFF zur übersetzten Datei zusammenfügen</subtitle>
  <date>2015-07-06</date>
  <author>
    <personname>
      <firstname>Herbert</firstname>
      <surname>Schiemann</surname>
    </personname>
    <email>h.schiemann@herbaer.de</email>
  </author>
</d:info>

<d:para>
Pfad der übersetzten XLIFF-Datei
</d:para>
<xsl:param name="p_xliff" select="''"/>

<d:para>
Quellsprache
</d:para>
<xsl:param name="p_srclang" select="/*/sk:lang"/>

<d:para>
Zielsprache
</d:para>
<xsl:param name="p_trglang"/>

<d:para>
Übersetzungsmaschine
</d:para>
<xsl:param name="p_machine" select="document($p_xliff)/xlf:xliff/ti:machine"/>

<d:para>
Das
<d:tag class="element">file</d:tag>-Element
der übersetzten XLIFF-Datei
</d:para>
<xsl:variable name="g_xlffile" select="document($p_xliff)/xlf:xliff/xlf:file[1]"/>

<d:para>
Das Wurzelelement der Gerüst-Datei
</d:para>
<xsl:variable name="g_sklroot" select="/*"/>

<xsl:output method="xml" encoding="utf-8" indent="no"/>

<d:para>
Wurzel
</d:para>
<xsl:template match="/">
  <xsl:if test="$g_sklroot">
    <xsl:apply-templates select="text() | processing-instruction() | *"/>
  </xsl:if>
</xsl:template>

<d:para>
Zum Wurzelelement wird ein Attribut eingefügt,
das die Übersetzungsmaschine bezeichnet.
</d:para>
<xsl:template match="/*">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates select="sk:lang"/>
    <xsl:apply-templates select="sk:attribute"/>
    <xsl:if test="string-length ($p_machine) &gt; 0">
      <xsl:attribute name="ti:machine">
        <xsl:value-of select="$p_machine"/>
      </xsl:attribute>
    </xsl:if>
    <xsl:apply-templates select="sk:contents"/>
  </xsl:copy>
</xsl:template>

<d:para>
Verarbeitungsanweisungen werden kopiert.
</d:para>
<xsl:template match="processing-instruction()">
  <xsl:copy-of select="."/>
</xsl:template>

<d:para>
Text wird kopiert.
</d:para>
<xsl:template match="text()">
  <xsl:copy-of select="."/>
</xsl:template>

<d:para>
Quelldokument-Elemente
</d:para>
<xsl:template match="*">
  <xsl:param name="hint"/>
  <xsl:copy>
    <xsl:if test="string-length ($hint) &gt; 0">
      <xsl:attribute name="ti:hint">
        <xsl:value-of select="$hint"/>
      </xsl:attribute>
    </xsl:if>
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates select="sk:lang"/>
    <xsl:apply-templates select="sk:attribute"/>
    <xsl:apply-templates select="sk:contents"/>
  </xsl:copy>
</xsl:template>

<d:para>
Angabe der Zielsprache
</d:para>
<xsl:template match="sk:lang">
  <xsl:attribute name="xml:lang">
    <xsl:choose>
      <xsl:when test=". = $p_srclang">
        <xsl:value-of select="$p_trglang"/>
        <xsl:if test="string-length ($p_machine) &gt; 0">
          <xsl:value-of select="concat ('-x-mtfrom-', $p_srclang)"/>
        </xsl:if>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="."/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:attribute>
</xsl:template>

<d:para>
Übersetzte Attribute
</d:para>
<xsl:template match="sk:attribute">
  <xsl:variable name="sid" select="concat ('s_', @xliffid)"/>
  <xsl:attribute name="{@name}">
    <xsl:choose>
      <xsl:when test="$g_xlffile//xlf:segment[@id = $sid]/xlf:target">
        <xsl:value-of select="$g_xlffile//xlf:segment[@id = $sid]/xlf:target"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="."/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:attribute>
</xsl:template>

<d:para>
Element-Inhalt.
Wenn bei der Übersetzung ein Problem aufgetreten ist,
bleibt der Inhalt in der Quellsprache erhalten.
</d:para>
<xsl:template match="sk:contents">
  <xsl:variable name="xliffid" select="@xliffid"/>
  <xsl:variable name="sid" select="concat ('s_', $xliffid)"/>
  <xsl:choose>
    <xsl:when test="sk:part">
      <xsl:apply-templates select="*"/>
    </xsl:when>
    <xsl:when test="$g_xlffile//xlf:segment [@id = $sid]/xlf:target [not (@ti:error)]">
      <xsl:apply-templates select="$g_xlffile//xlf:segment [@id = $sid]/xlf:target">
        <xsl:with-param name="xliffid" select="$xliffid"/>
      </xsl:apply-templates>
      <!-- Elemente ohne Textinhalt werden kopiert -->
      <xsl:apply-templates select="*" mode="copyemptyelts"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:if test="$g_xlffile//xlf:segment [@id = $sid]/xlf:target/@ti:error">
        <xsl:attribute name="xml:lang">
          <xsl:value-of select="$p_srclang"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:choose>
        <xsl:when test="text() [string-length(normalize-space(.)) &gt; 0]">
          <xsl:apply-templates select="text()|*">
            <xsl:with-param name="xliffid" select="$xliffid"/>
          </xsl:apply-templates>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="*">
            <xsl:with-param name="xliffid" select="$xliffid"/>
          </xsl:apply-templates>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<d:para>
Teil eines Element-Inhalts
Wenn bei der Übersetzung ein Problem aufgetreten ist,
wird ein sk:part-Element
mit dem Inhalt in der Quellsprache erzeugt.
</d:para>
<xsl:template match="sk:part">
  <xsl:variable name="xliffid" select="@xliffid"/>
  <xsl:variable name="sid" select="concat ('s_', $xliffid)"/>
  <xsl:choose>
    <xsl:when test="sk:part">
      <xsl:apply-templates select="*"/>
    </xsl:when>
    <xsl:when test="$g_xlffile//xlf:segment [@id = $sid]/xlf:target [not (@ti:error)]">
      <xsl:apply-templates select="$g_xlffile//xlf:segment [@id = $sid]/xlf:target">
        <xsl:with-param name="xliffid" select="$xliffid"/>
      </xsl:apply-templates>
      <!-- Elemente ohne Textinhalt werden kopiert -->
      <xsl:apply-templates select="*" mode="copyemptyelts"/>
    </xsl:when>
    <xsl:when test="$g_xlffile//xlf:segment [@id = $sid]/xlf:target/@ti:error">
      <sk:part xml:lang="{$p_srclang}">
        <xsl:choose>
          <xsl:when test="text() [string-length(normalize-space(.)) &gt; 0]">
            <xsl:apply-templates select="text()|*">
              <xsl:with-param name="xliffid" select="$xliffid"/>
            </xsl:apply-templates>
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates select="*">
              <xsl:with-param name="xliffid" select="$xliffid"/>
            </xsl:apply-templates>
          </xsl:otherwise>
        </xsl:choose>
      </sk:part>
    </xsl:when>
    <xsl:otherwise>
      <xsl:choose>
        <xsl:when test="text() [string-length(normalize-space(.)) &gt; 0]">
          <xsl:apply-templates select="text()|*">
            <xsl:with-param name="xliffid" select="$xliffid"/>
          </xsl:apply-templates>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="*">
            <xsl:with-param name="xliffid" select="$xliffid"/>
          </xsl:apply-templates>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<d:para>
Leere Elemente werden ohne die zusätzlichen skl-Elemente kopiert.
Sie enthalten vielleicht Attribute, die übersetzt werden sollen.
</d:para>
<xsl:template match="*" mode="copyemptyelts">
  <xsl:variable name="textcont">
    <xsl:apply-templates select="." mode="textcont"/>
  </xsl:variable>
  <xsl:if test="string-length (normalize-space ($textcont)) = 0">
    <xsl:apply-templates select="."/>
  </xsl:if>
</xsl:template>

<d:para>
Der Textinhalt eines Elements
</d:para>
<xsl:template match="*" mode="textcont">
  <xsl:apply-templates mode="textcont"/>
</xsl:template>

<d:para>
Textinhalt wird kopiert
</d:para>
<xsl:template match="text()" mode="textcont">
  <xsl:copy-of select="."/>
</xsl:template>

<d:para>
<d:tag class="element">sk:lang</d:tag>-
und
<d:tag class="element">sk:attribute</d:tag>-Elemente
enthalten keinen Textinhalt des Quell-Elements.
</d:para>
<xsl:template match="sk:lang" mode="textcont"/>
<xsl:template match="sk:attribute" mode="textcont"/>

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

<d:para>
Gerüst-Attribute werden nicht kopiert.
</d:para>
<xsl:template match="@sk:*"/>

<d:para>
Übersetzung
</d:para>
<xsl:template match="xlf:target">
  <xsl:apply-templates/>
</xsl:template>

<d:para>
Markierte Textstellen
</d:para>
<xsl:template match="xlf:mrk">
  <xsl:variable name="id" select="substring-before (substring (@id, 3), '_')"/>
  <xsl:choose>
    <xsl:when test="$g_sklroot//sk:part[@xliffid = $id]">
      <xsl:apply-templates select="$g_sklroot//sk:part[@xliffid = $id]"/>
    </xsl:when>
    <xsl:when test="$g_sklroot//sk:contents[@xliffid = $id]">
      <xsl:apply-templates select="$g_sklroot//sk:contents[@xliffid = $id]" mode="upwrap">
        <xsl:with-param name="hint" select="@ti:hint"/>
      </xsl:apply-templates>
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<d:para>
"Verpackte" verschachtelte Teilabschnitte werden "eingepackt".
</d:para>
<xsl:template match="sk:contents" mode="upwrap">
  <!-- Hinweis zum Übersetzer -->
  <xsl:param name="hint"/>
  <xsl:choose>
    <xsl:when test="../parent::sk:contents/@wrap = 'wrap'">
      <xsl:apply-templates select="../parent::sk:contents" mode="upwrap">
        <xsl:with-param name="hint" select="$hint"/>
      </xsl:apply-templates>
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select="..">
        <xsl:with-param name="hint" select="$hint"/>
      </xsl:apply-templates>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>
