简体   繁体   中英

Transforming XLIFF to HTML table with XLST 1.0: group elements with same ID from different parent elements, side by side?

I'm trying to get my head around putting XLIFF's segmented source/target language elements side by side in an HTML table, like

<trans-unit id="/html[1]/head[1]/title[1]" resname="Title">
  <source xml:lang="de-DE">E-Mail: Vorlagen</source>
  <seg-source>
    <mrk mid="1" mtype="seg">E-Mail: Vorlagen</mrk>
    <mrk mid="2" mtype="seg">Vorlagen</mrk>
  </seg-source>
  <target state="translated">
    <mrk mid="1" mtype="seg">Email:</mrk>
    <mrk mid="2" mtype="seg">Templates</mrk>
  </target>
</trans-unit>

should become

<tr>
    <td class="mid">1</td> <td class="src">E-Mail:</td> <td class="tgt">Email:<td/>
</tr>
<tr>
    <td class="mid">2</td> <td class="src">Vorlagen</td> <td class="tgt">Templates<td/>
</tr>

XLIFF translation units can have any number of sub-segments, it thus must be a solution that, I don't know, counts how many MIDs there are in a TU and then jumps back and forth between the source-segment and target elements (with an incremental counter?) to put them side by side in the table?

I have seen XSLT 2.0 solutions working with "for-each-group" wizardry, but did not yet get an idea of how to do it in XSLT 1.0 (edit: with Xalan 1.11). Frankly, I'm a bit amazed that no one has whipped up a style sheet to show bilingual translations in XLIFF side by side as HTML table, so I thought I'd give it a try. Unfortunately, my XSLT hasn't been used in, like, years and I'm a bit rusty. Any pointers what keyword/topic/web source I should have a look at, if not a straight solution?

Thanks a lot, Christopher

PS: If I can work out a solution, I'll, of course, put it freely online for my fellow translator colleagues (or anyone who wants to get a look at what actually is these XLIFF files they send out to us translators).

Somehow along the following lines:

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
  
  <xsl:key name="tr" match="trans-unit/target/mrk" use="concat(../../@id, '|', @mid)"/>

  <xsl:output method="html" indent="yes" version="5" doctype-system="about:legacy-doctype"/>
  
  <xsl:template match="trans-unit[@id]">
    <table>
      <xsl:apply-templates select="source"/>
      <thead>
        <tr>
          <th>mid</th>
          <th>source</th>
          <th>target</th>
        </tr>
      </thead>
      <tbody>
        <xsl:apply-templates select="seg-source/mrk"/>
      </tbody>
    </table>
  </xsl:template>
  
  <xsl:template match="trans-unit/source">
    <caption>
      <xsl:apply-templates/>
    </caption>
  </xsl:template>
  
  <xsl:template match="seg-source/mrk">
    <tr>
      <td class="mid">
        <xsl:value-of select="@mid"/>
      </td>
      <td class="src">
        <xsl:apply-templates/>
      </td>
      <td class="tgt">
        <xsl:apply-templates select="key('tr', concat(../../@id, '|', @mid))/text()"/>
      </td>
    </tr>
  </xsl:template>

  <xsl:template match="/">
    <html>
      <head>
        <title>Example</title>
      </head>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM